Note that headers should be sent before anything else. Make sure that there is no code/html or even space/indentation before the header function and there is nothing before the first opening php tag <?php as well as ending tag ?> in your view.
---
PHP : How can I retain values after an invalid form submission?
If you are looking to just repopulate the fields with the values that were posted in them, then just echo the post value back into the field, like so:
<input type="text" name="myField1" value="<?php echo isset($_POST['myField1']) ? $_POST['myField1'] : '' ?>" />
---
CodeIgniter: How can I retain values after an invalid form submission?
Open your myform.php view file and update the value in each field using the set_value() function:
Don’t forget to include each field name in the :php:func:`set_value()` function calls!
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
Now reload your page and submit the form so that it triggers an error. Your form fields should now be re-populated
----
PHP htmlspecialchars Function. The htmlspecialchars function in PHP is used to convert 5 characters into corresponding HTML entities where applicable. It is used to encode user input on a website so that users cannot insert harmful HTML codes into a site. ENT_COMPAT is the default if quote_style is not specified.
---
PHP : How can I retain values after an invalid form submission?
If you are looking to just repopulate the fields with the values that were posted in them, then just echo the post value back into the field, like so:
<input type="text" name="myField1" value="<?php echo isset($_POST['myField1']) ? $_POST['myField1'] : '' ?>" />
---
CodeIgniter: How can I retain values after an invalid form submission?
Open your myform.php view file and update the value in each field using the set_value() function:
Don’t forget to include each field name in the :php:func:`set_value()` function calls!
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
Now reload your page and submit the form so that it triggers an error. Your form fields should now be re-populated
----
PHP htmlspecialchars Function. The htmlspecialchars function in PHP is used to convert 5 characters into corresponding HTML entities where applicable. It is used to encode user input on a website so that users cannot insert harmful HTML codes into a site. ENT_COMPAT is the default if quote_style is not specified.
This comment has been removed by the author.
ReplyDeleteUNSET POST VARIABLES
ReplyDeleteif (!isset($_SESSION)) {
session_start();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_SESSION['postdata'] = $_POST;
unset($_POST);
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
https://stackoverflow.com/questions/37127361/codeigniter-pass-array-of-arrays-from-view-to-controller
ReplyDeleteI found the solution to my problem. Here is what worked for me. I was passing my array of arrays as a string value in a hidden input field using
json_encode($array)
but the problem was that my keys was double quoted and as a result the
value=""
was breaking down...
The solution was to escape the characters, so I had to replace the above line with
value=""
And in the controller I had to get my array from json with the following lines
$dataJson = $this->input->post('array');
$dataArray = json_decode(htmlspecialchars_decode($dataJson), true);