view
<form action="/user/edit" method="POST">
<?php $fuser = $this->session->userdata('fname');?> <input name="firstname" class="input" type="text" value="<?php echo $fuser->firstname;?>" placeholder="Enter First Name"><br><br>
<?php $luser = $this->session->userdata('lname');?> <input name="lastname" class="input" type="text" value="<?php echo $luser->lastname;?>" placeholder="Enter Last Name"><br><br>
<?php $grd = $this->session->userdata('grade');?> <input name="grade" class="input" type="text" value="<?php echo $grd->grade;?>" placeholder="Enter Grade"><br><br>
<?php $euser = $this->session->userdata('email');?><input name="email" class="input" type="text" value="<?php echo $euser->email;?>" placeholder="Enter Email"><br><br>
<input type="submit" value="Update" name="submit" class="btn btn-default "/>
</form>
controller
public function edit()
{
$this->load->library('session');
$this->load->view('edit');
}
after running i get the following errors:
Severity: Notice
Message: Trying to get property 'firstname' of non-object
Filename: views/edit.php
Line Number: 33
Your post is really not so clear but the non-object error comes when you try to access an object with the [] like obj_var["index"] or an array with -> like $arr_var->index. I suggest you use var_dump($variable) on the variable you tried to access and see how to access the variable properly.
You can also cast the variable to an object or array depending on how you wish to use it.
To convert to array use the below.
$array_cast = (array) $your_variable;
or
To convert to object use the below.
$object_cast = (object) $your_variable;
Related
<?php
$role_class = new RoleModel;
$role_name = "";
foreach ($role_class->getInfoById("role_id") as $row) {
# code...
$role_name = $row->role_name;
}
?>
<fieldset class="g12 go pb_s">
<legend>Basic Info</legend>
<div class="g12 go">
<div class="g7 go">
<div class="g2">
<label for="groupName">Role Name:</label>
</div>
<div class="g4">
<input type="text" placeholder="Type Role Name here..." name="role_name" value="<?php echo $role_name; ?>" required="required">
</div>
</div>
</div>
</fieldset>
You can use PHP's $post to retrieve the value of the input tag via the name of the HTML tag.
For Example, change the method in your form and then echo out the value by the name of the input:
In your Html
<form name="form" action="" method="post">
<input type="text" name="role_name" id="role_name" value="<?php echo $role_name;?>">
</form>
In your .php, you can get the value like below
$role_name=$post['role_name'];
Since the editor edited his questions, i will add more example.
For example, i got a list of data want to show in the html, in controller, i will put into an array first.
$form_fields = array('username', 'ref', 'password', 'password_confirm')
after that in html, i will do like this
<?php foreach($__form_fields as $field) {?>
<input type="text" placeholder="<?php echo $field?>" name="<?php echo $field? >" value="" required="required">
<?php }?>
which the above code will loop and the input field will have he name
In this view, I want to post login data to controller but it didn't work and return this url: http://[::1]/ci/index.php/verifylogin
View:
<?php echo form_open('verifylogin'); ?>
<label for="username">Username:</label>
<input type="text" size="20" id="username" name="username"/>
<br/>
<label for="password">Password:</label>
<input type="password" size="20" id="passowrd" name="password"/>
<br/>
<input type="submit" value="Login"/>
I think below code help u.
<?php echo form_open(base_url('verifylogin')); ?>
verifylogin may be your method name so pass your controller name after.
ex. : base_url('controller_name/verifylogin')
Problem is about "base_url" settings, I set it $config['base_url'] = 'localhost:83/ci/';; and it worked.
I tried store data in a session using CI in my register function.
public function register(){
$firstname = $this->input->post('firstname');
$email= $this->input->post('email');
$dev_info = array('fname'=>$firstname, 'eaddress'=>$email);
$this->session->set_userdata($dev_info);
}
and in my other function verification I want to get the session data and pass to the view
public function verification(){
$data['fname'] = $this->session->userdata('fname');
$data['eaddress'] = $this->session->userdata('eaddress');
$this->load->view('index', $data);
}
and I want to save its value in input type
in the view
<input type="hidden" id="firstname" value="<?php echo $fname?>">
<input type="hidden" id="email" value="<?php echo $eaddress?>">
but i am having a hard time saving its value in an input everytime I check using view source. Please help!
you can get session variables one by one also
$first_name = $this->session->userdata('fname');
$email_address = $this->session->userdata('eaddress');
in view you can use these variables or you can write code for view like this
<input type="hidden" id="firstname" value="<?php echo $this->session->userdata('eaddress'); ?>">
<input type="hidden" id="email" value="<?php echo $this->session->userdata('fname')?>">
in controller create session for ur input data
$data['fname'] = $this->session->userdata('fname');
$data['eaddress'] = $this->session->userdata('eaddress');
in view retrieve the input data from the session by adding the value by set_value
<input type="hidden" id="firstname" value="<?php echo set_value('fname'); ?>">
<input type="hidden" id="email" value="<?php echo set_value('eaddress'); ?>">
<form id="login-form" action="brand-dashboard" method="post">
<span class"email">email</span>
<input type="email" name="email">
<span class"email">password</span>
<input type="password" name="password"><br><br>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="submit" value="login">
</form>
this is in my view page..then in my route..
Route::get('/brand-dashboard','BrandsController#auth_brand_admin');
in my Brands controller..i use the method like
public function auth_brand_admin()
{
return ('sample text');
}
but i got error when submiting the form ..the error is..
MethodNotAllowedHttpException in RouteCollection.php
Change your code to this
Route::post('/brand-dashboard','BrandsController#auth_brand_admin');
It's because you register route with GET method but send POST request.
I have the following form:
<label>One</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<label>Two</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<label>Three</label>
Product ID:<input type="text" name="productid[]" value="">
Product Quantity: <input type="text" name="quantity[]" value=""> <br>
<!-- there may be more inputs like above (users can create new inputs
as many as they want) // I have a jquery function to create new rows-->
<input type="submit" value="submit">
Now My question is how to validate the form using Codeigniter when I have input names like this- name="productid[]" instead of name="productid" in my form.
Usually I use to validate my form like this way but this time it won't work for the form above.
How to validate it?
You would use the literal field names, with brackets:
$this->form_validation->set_rules('product_id[]', 'Product', 'required');
$this->form_validation->set_rules('quantity[]', 'Quantity', 'required');
This will run the validation on every field with that name. If you have to validate only a specific index, once again - use the literal field name (and specify the index in your HTML):
// <input name="product_id[3]">
$this->form_validation->set_rules('product_id[3]', 'Product', 'required');
It's all in the documentation for Codeigniter's Form Validation class:
Using Arrays as Field Names
The Form Validation class supports the use of arrays as field
names. Consider this example:
<input type="text" name="options[]" value="" size="50"
/>
If you do use an array as a field name, you must use the EXACT
array name in the Helper Functions that
require the field name, and as your Validation Rule field name.
For example, to set a rule for the above field you would use:
$this->form_validation->set_rules('options[]', 'Options',
'required');
Or, to show an error for the above field you would use:
<?php echo form_error('options[]'); ?>
Or to re-populate the field you would use:
<input type="text" name="options[]" value="<?php echo
set_value('options[]'); ?>" size="50" />
You can use multidimensional arrays as field names as well. For
example:
<input type="text" name="options[size]" value="" size="50"
/>
Or even:
<input type="text" name="sports[nba][basketball]" value=""
size="50" />
As with our first example, you must use the exact array name in the
helper functions:
<?php echo form_error('sports[nba][basketball]'); ?>
If you are using checkboxes (or other fields) that have multiple
options, don't forget to leave an empty bracket after each option, so
that all selections will be added to the POST array:
<input type="checkbox" name="options[]" value="red" />
<input type="checkbox" name="options[]" value="blue" />
<input type="checkbox" name="options[]" value="green" />
Or if you use a multidimensional array:
<input type="checkbox" name="options[color][]" value="red"
/> <input type="checkbox" name="options[color][]"
value="blue" /> <input type="checkbox"
name="options[color][]" value="green" />
When you use a helper function you'll include the bracket as
well:
<?php echo form_error('options[color][]'); ?>
Did You try this ...? A few lines below in the guide ...