function show in url after submit form in codeigniter - php

When I submit this form all things are well n good but I want the controller function not shows in url. I want all things are done in same page. Still the url show
localhost/Naveen/CodeIgniter/welcome/insertform
but I don't want the form_open('') show's in url so how it is possible?
controller welcome.php
public function insertform()
{
if (isset($_POST['mysmt']))
{
$this->form_validation->set_rules('fname', 'Name', 'required');
$this->form_validation->set_rules('femail', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('fmobile', 'Mobile', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('welcome_view');
}
else
{
$_POST['fname'];
$_POST['femail'];
$_POST['fmobile'];
if($this->test_model->insert('user_accounts',array('',$_POST['fname'],$_POST['femail'],$_POST['fmobile'])))
{
$success['success']="Thanks For Join Us";
$this->load->view('welcome_view',$success);
}
}
}
else
{
$this->load->view('welcome_view');
}
}
view welcome_view.php
<?php echo form_open('welcome/insertform'); // ?>
<div class="form-group">
<?php
if(isset($success))
{?>
<input type="button" class="form-control btn-success" value="<?php echo $success; ?>">
<?php }
else
{
echo "";
}?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Name <?php echo form_error('fname'); ?></label>
<?php
$entername = array(
'name' => 'fname',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Name',
'class' => 'form-control',
);
echo form_input($entername); ?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Email <?php echo form_error('femail'); ?></label>
<?php
$enteremail = array(
'name' => 'femail',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Email',
'class' => 'form-control',
);
echo form_input($enteremail); ?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Mobile <?php echo form_error('fmobile'); ?></label>
<?php
$entermobile = array(
'name' => 'fmobile',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Mobile',
'class' => 'form-control',
);
echo form_input($entermobile); ?>
</div>
<div class="form-group">
<?php
$f_formsmt = array(
'name' => 'mysmt',
'value' => 'Submit Form',
'class' => 'form-control btn btn-success',
);
echo form_submit($f_formsmt); ?>
</div>
<?php echo form_close(); ?>

You handle it through applications/config/routes.php
consider you url: localhost/Naveen/CodeIgniter/welcome/insertform
$route['add'] = 'welcome/insertform';
now you can use localhost/Naveen/CodeIgniter/add
You can change whatever the string you want for any controller/function using routes.
Hope you understood :)

You need to add the url in route and add link to your link href
In route.php add:
$route['custom_url'] = 'controller/method';

I used Header("location:../{whatever-filename}")
Then gave that "whatever-filename" the proper routing in the config/routes.php.
Routing without using the redirect function would load the appropriate page but the url would still show the controller and method you're using.
Hope it helps

Related

Codeigniter form-validation is always returning false

I am trying to get form values and passing it in my model to store in the database. Whenever I end up submitting the form, I always get the values as NULL.
form.php
<form action="http://localhost/hbp/review/submit/" method="post">
<input type="hidden" name='<?php echo $this->security->get_csrf_token_name(); ?>' value='<?php echo $this->security->get_csrf_hash(); ?>'>
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<!-- <input type="text" name="title">-->
<?php echo form_input(['id' => 'title', 'name' => 'title', 'class' => 'form-control', 'placeholder' => 'Enter your review title']); ?>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Review</label>
<!-- <textarea rows="10" cols="100" name="message"></textarea>-->
<?php echo form_textarea(['id' => 'message', 'name' => 'message', 'class' => 'form-control', 'placeholder' => 'Enter your review title', 'rows' => '10', 'cols' => '100']); ?>
</div>
<button name="submit">Hello</button>
</form>
I have tried using both form_input() and simple HTML syntax for rendering form fields. None of them worked.
controller.php
if ($gClient->getAccessToken()) {
$userProfile = $google_oauthV2->userinfo->get();
$name = $userProfile['name'];
$email = $userProfile['email'];
$picture = $userProfile['picture'];
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'title', 'required');
$this->form_validation->set_rules('message', 'message', 'required');
if ($this->form_validation->run() == FALSE){
echo "error in your form ";
$this->session->set_flashdata('review_error', 'Please fill all the fields!');
$this->load->view('pages/review');
} else {
$this->session->set_flashdata('review_success', 'Thank you for you ');
$this->review_model->set_review($name, $email, $picture);
redirect(base_url() . 'review/');
}
echo "just dies";
model.php
class Review_model extends CI_Model
{
public function set_review($google_name, $google_email, $google_image)
{
$data = array('review_title' => $this->input->post('title'),
'review_content' => $this->input->post('message'),
'email' => $google_email,
'name' => $google_name,
'image_url' => $google_image);
return $this->db->insert('reviews', $data);
}
public function get_review(){
$query = $this->db->get('reviews');
return $query->result_array();
}
}
I keep getting the review_title can't be NULL.

cakephp keep data after validation error

user data has been lost if there is a validation error and error validation messages are also not showing on front end.
Here is my controller
public function add() {
$this->theme = 'Front';
if (!empty($this->logged_in)) {
return $this->redirect(Router::url('/', true) . $this->logged_in['Role']['alias']);
}
$this->set('title_for_layout', __('Create An Account'));
// hanlde post data
if ($this->request->is("post")) {
if (!empty($this->request->data)) {
$this->User->set($this->request->data);
if (!$this->User->validates()) {
$this->User->validationErrors;
$this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
$this->redirect(Router::url('/', true) . 'sign-up/');
}
}
// die('sdsddsaf');
// if (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 2) {
unset($this->request->data['User']['specialization_id']);
$randomSt = $this->Custom->randomString(28);
$this->request->data['User']['activation_key'] = $randomSt;
// }
if ($this->User->save($this->request->data)) {
$user = $this->request->data;
if (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3) {
$this->Session->setFlash(__('Thank you for registering with us. Please check your email inbox/junk.'), 'message', array('class' => 'successs'));
// set mail variable
$to = $this->request->data['User']['email'];
$fname = $this->request->data['User']['first_name'];
$lname = $this->request->data['User']['last_name'];
//$activelink = SITEURL . 'activate?code=' . $randomSt;
$siteurl = Configure::read('Site.title');
$replace = array($fname, $lname, $siteurl);
// Send mail on Registration
$this->sendMail($to, 'RegisterDoctor', $replace);
} else {
$this->Session->setFlash(__('Thank you for registering with us. Please check your e-mail inbox/junk as your e-mail confirmation has just been sent.'), 'message', array('class' => 'successs'));
$activelink = Router::url('/', true) . 'activate/' . $this->User->id . '/' . $randomSt;
// set mail variable
$to = $this->request->data['User']['email'];
$fname = $this->request->data['User']['first_name'];
$lname = $this->request->data['User']['last_name'];
//$activelink = SITEURL . 'activate?code=' . $randomSt;
$siteurl = Configure::read('Site.title');
$replace = array($fname, $lname, $activelink, $siteurl);
// Send mail on Registration
$this->sendMail($to, 'Register', $replace);
}
$this->redirect(array('controller' => 'users', 'action' => 'login'));
}
}
$reponse = array();
$roles = $this->User->rolesSignup();
$genders = $this->User->genders();
$specializations = $this->User->specializations();
$this->set(compact('roles', 'genders', 'specializations'));
}
And below is my view
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array('div' => false, 'label' => false, 'fieldset' => false),
'class' => 'ac p10',
// 'data-togglesd' => "validator",
'role' => 'form',
'type' => 'file',
'onSubmit' => 'return checksubmitPass()'
)
);
?>
<div class="ph10">
<div class="btn-group radio_group w100" data-toggle="buttons">
<label class="btn btn-primary col-md-6 <?php echo (empty($this->request->data['User']['role_id']) || #$this->request->data['User']['role_id'] == 2 ) ? 'active' : ""; ?> btn_roles">
<input type="radio" name="data[User][role_id]" value="2" class="role_id" <?php echo (empty($this->request->data['User']['role_id']) || #$this->request->data['User']['role_id'] == 2 ) ? 'checked="checked"' : ""; ?>> User
</label>
<label class="btn btn-primary col-md-6 <?php echo (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3 ) ? 'active' : ""; ?> btn_roles">
<input type="radio" name="data[User][role_id]" value="3" class="role_id" <?php echo (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3 ) ? 'checked="checked"' : ""; ?>> Doctor
</label>
</div>
</div>
<div class="form-group p10 cr">
<div class="cm upload_img round"><img src="<?php echo $this->webroot; ?>img/user_avtar(upload).png" class="user_pic_upload">
<?php echo $this->Form->file('image', array('class' => 'user_img', 'data-fv-file' => 'true', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-field' => 'data[User][image]')); ?>
</div>
<small style="display: none;" class="help-block" data-fv-validator="notEmpty" data-fv-for="data[User][image]" data-fv-result="VALID">This field cannot be left blank.</small>
</div>
<div class="form-group mt10 cr">
<div class="clearfix">
<div class="col-lg-6 first_name"><?php echo $this->Form->input('first_name', array('class' => 'form-control', 'placeholder' => 'First Name', 'data-stripe' => "alphabet", 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-regexp' => 'true', 'data-fv-regexp-regexp' => "^[a-zA-Z\s]+$", 'data-fv-regexp-message' => "The first name can consist of alphabetical characters and spaces only")); ?></div>
<div class="col-lg-6 last_name"><?php echo $this->Form->input('last_name', array('class' => 'form-control', 'placeholder' => 'Last Name', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-regexp' => 'true', 'data-fv-regexp-regexp' => "^[a-zA-Z\s]+$", 'data-fv-regexp-message' => "The last name can consist of alphabetical characters and spaces only", 'required')); ?></div>
</div>
</div>
<div class="form-group ph10 mt10">
<?php echo $this->Form->input('email', array('class' => "form-control", 'placeholder' => 'Email Address', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-emailaddress-message' => __('validemail'))); ?>
</div>
<div class="form-group mt10 ph10 row_org">
<div id="dropdown-menu">
<?php echo $this->Form->input('specialization_id', array('empty' => 'Select Speciallization', 'class' => 'selectpicker form-control', 'data-fv-notempty-message' => __('notEmpty'), 'required')); ?>
</div>
</div>
<div class="form-group ph10 mt10">
<?php echo $this->Form->input('password', array('class' => "form-control", 'placeholder' => 'Password', 'type' => 'password', 'data-fv-notempty-message' => __('notEmpty'))); ?>
</div>
<span id="result"></span>
<div class="form-group ph10 mt10">
<?php
echo $this->Form->input('verify_password', array(
'class' => "form-control",
'type' => 'password',
'placeholder' => 'Confirm Password',
'data-fv-notempty-message' => __('notEmpty'),
'data-fv-identical' => "true",
'data-fv-identical-field' => "data[User][password]",
'data-fv-identical-message' => __('passwordNotMatch')
)
);
?>
</div>
<div class="form-group mt10">
<div class="clearfix">
<div class="col-lg-6 mobile-custom">
<?php echo $this->Form->input('mobile', array('class' => 'form-control phonenumber', 'placeholder' => 'Mobile number', 'maxlength' => '14', 'minlength' => '10', 'data-fv-stringlength-message' => 'The mobile number must be 10 to 14 characters long', 'data-fv-numeric-message' => __('Please enter valid mobile numbers'), 'data-fv-numeric' => "true")); ?>
</div>
<div class="col-lg-6" id="dropdown-menu">
<?php //echo $this->Form->input('gender', array('class' => 'form-control selectpicker', 'data-fv-notempty-message' => __('notEmpty'),'empty' => 'I Am','error' => false, 'required')); ?>
<select id="UserGender" title="I Am" required="required" data-fv-notempty-message="This field cannot be left blank." class="form-control" name="data[User][gender]" style="display: none;" data-fv-field="data[User][gender]">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group ph10 mt10"><button type="submit" class="btn btn-info w100">Sign Up</button></div>
<p class="mt10"><small>By Signing up you are agree with <br>Terms & Conditions and Privacy Policy</small></p>
<p class="mt10 signup-text"><strong>Already on HealthDrop?</strong></p>
<div class="ph10">
<?php echo $this->Html->link('Sign In', Router::url('/', true) . 'login', array('class' => 'btn deep btn-primary w100')); ?>
</div>
<?php echo $this->Form->end(); ?>
it was working before bu suddenly stop to showing error and start loosing data.
I have check with this question but it is pointing out at field names in input and I have correct ones.
Any help appreciate.
You lost your data because there is redirect to sign-up url.
$this->redirect(Router::url('/', true) . 'sign-up/');
And after redirection
$this->request->data
is empty because you perform new request, the same with errors
According to your code there are two mistakes I found
you are using ! for validating the validations
Save method itself validate the data so no need to validate again.
So your code should be look like
if ($this->User->save($this->request->data)) {
//your code
}else{
$this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
$this->redirect(Router::url('/', true) . 'sign-up/');
}
and validation error messages are automatically assign to view, no need to worry about them

div tags not displayed in codeigniter viewer

I'm new to CI and confused about 2 issues while trying to display a simple login form...
controller :
function login()
{
$this->data['title'] = "Login";
// render the header
$data['css'] = 'login.css';
$this->load->view('layouts/header', $data);
//validate form input
$this->form_validation->set_rules('identity', 'Identity', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
//the user is not logging in so display the login page
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['identity'] = array('name' => 'identity',
'id' => 'identity',
'type' => 'text',
'required' => 'required',
'value' => $this->form_validation->set_value('identity'),
);
$this->data['password'] = array('name' => 'password',
'id' => 'password',
'required' => 'required',
'type' => 'password',
);
//$this->load->template('login');
$this->load->view('auth/login', $this->data);
}
// render the footer
$this->load->view('layouts/footer');
}
and here is my viewer (auth/login.php):
<div class="container">
<div class="form-signin">
<h1>Login</h1>
<p>Please login with your email/username and password below.</p>
<div id="infoMessage"><?php echo $message;?></div>
<?php
$attributes = array('class' => 'form-signin', 'id' => 'form-signin');
echo form_open('auth/login', $attributes);
?>
<p>
<label for="identity">Email/Username:</label>
<?php echo form_input($identity);?>
</p>
<p>
<label for="password">Password:</label>
<?php echo form_input($password);?>
</p>
<p>
<label for="remember">Remember Me:</label>
<?php echo form_checkbox('remember', '1', FALSE, 'id="remember"');?>
</p>
<p><?php echo form_submit('submit', 'Login');?></p>
<?php echo form_close();?>
<p>Forgot your password?</p>
</div>
</div>
When I look at the page source in my browser, the form does not have the 'class' attribute set to 'form-signin' (I can't figure out why??). That's why I tried to add a DIV to hold the form but the div can't be found in the page source neither.... Any suggestion will be highly appreciated.

Login with SSL activated with cakephp?

I have activate ssl with my app and everything is going well... well, exept login. It is impossible to login to my app. Here is my form code that used to work with nude http :
<div class="users form">
<div class="form-login-box">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User', array(
'class' => 'form-login',
'inputDefaults' => array(
'label' => false
)
)); ?>
<fieldset>
<?php echo $this->Form->input('username', array(
'class' => 'form-login-username',
'placeholder' => __('Login')
));
echo $this->Form->input('password', array(
'class' => 'form-login-password',
'placeholder' => __('Password')
));
?>
</fieldset>
<input type="submit" value="<?php echo __('Login') ?>" class="form-login-submit">
</div>
</div>
And my controller :
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array('class' => 'authMessage-error'), 'auth');
}
}
}
It looks like your not actually closing the form the Cake way. Try this.
Replace the
<input type="submit" value="<?php echo __('Login') ?>" class="form-login-submit">
With
<?php echo $this->Form->end('Submit'); ?>

recaptcha with codeigniter, i can put it in the view .. but can't validate it

I'm trying to use the recaptcha with CodeIgniter. I followed some online instructions and finally I have done with only one step, just to pass the recaptcha to the view, but I can't validate the user input.
Here is my controller:
function download_application()
{
//load the libraries
$this->load->library('form_validation');
$this->load->library('recaptcha');
$this->lang->load('recaptcha');
//common data
$data['title'] = $_POST['application_name'];
$data['header'] = $_POST['application_name'];
$data['sub_header'] = 'تحميل استمارة قبول المشروع';
$data['title'] = $_POST['application_name'];
$data['recaptcha'] = $this->recaptcha->get_html();
//form validation
$this->form_validation->set_error_delimiters('<span class="notification">', '</span>');
$this->form_validation->set_message('required', 'هذا الحقل مطلوب ولا يمكن تجاهله');
$this->form_validation->set_rules('name', 'لابد من ادخال اسمك بالكامل', 'required');
$this->form_validation->set_rules('email', 'لابد من ادخال بريدك الالكترونى', 'required|email');
$this->form_validation->set_rules('country', 'لابد من ادخال بلدك', 'required');
$this->form_validation->set_rules('phone', 'لابد من ادخال رقم تليفونك', 'required');
//form submitted
if($this->input->post('recaptchasubmit')){
if($this->form_validation->run() == FALSE)
{
$this->load->view('header', $data);
$this->load->view('download', $data);
$this->load->view('footer', $data);
}
else
{
$this->load->view('header', $data);
$this->load->view('download', $data);
$this->load->view('footer', $data);
}
}
else{
$this->load->view('header', $data);
$this->load->view('download', $data);
$this->load->view('footer', $data);
}
}
and here is my view
<?php
$form_attributes = array(
'class' => 'form'
);
$btn_download = array(
'type' => 'image',
'src' => base_url().'images/download.gif',
'name' => 'recaptchasubmit',
'width' => '103',
'height' => '33',
'value' => 'تحميل'
);
$name = array(
'type' => 'text',
'name' => 'name',
'id' => 'name',
'value' => set_value('title')
);
$email = array(
'type' => 'text',
'name' => 'email',
'id' => 'email',
'value' => set_value('email')
);
$country = array(
'type' => 'text',
'name' => 'country',
'id' => 'country',
'value' => set_value('country')
);
$phone = array(
'type' => 'text',
'name' => 'phone',
'id' => 'phone',
'value' => set_value('phone')
);
?>
<?php echo form_open($base_url . 'arabia/download_application', $form_attributes); ?>
<fieldset>
<div class="input_container">
<label class="required">الاسم بالكامل</label>
<div class="input"><?php echo form_input($name); ?></div>
<?php echo form_error('name'); ?>
</div>
<div class="input_container">
<label class="required">البريد الالكترونى</label>
<div class="input"><?php echo form_input($email); ?></div>
<?php echo form_error('email'); ?>
</div>
<div class="input_container">
<label class="required">البلد</label>
<div class="input"><?php echo form_input($country); ?></div>
<?php echo form_error('country'); ?>
</div>
<div class="input_container">
<label class="required">التليفون</label>
<div class="input"><?php echo form_input($phone); ?></div>
<?php echo form_error('phone'); ?>
</div>
<?php echo $recaptcha; ?>
<?php echo form_error('recaptcha_response_field'); ?>
<?php echo form_hidden('application_name', $title); ?>
<?php echo form_hidden('generated_id', $title); ?>
</fieldset>
<span class="download"><?php echo form_submit($btn_download);?></span>
<?php echo form_close();?>
What do you mean you can't validate the user input? What is happening when you submit the form? Where are you redirected? What do you see?
The only thing I can see from your question thus far is that you are passing
$data['recaptcha'] = $this->recaptcha->get_html();
regardless if validation passes or not - so you'll see the recaptcha letters/numbers box either way. You need to overwrite that if validation passes to something like:
$data['recaptcha'] = "validation passed";

Categories