I have an array input in a form. I want to validate the form using php and not loosing the input if any error occurs. I read previous questions and used the following codes:
<input type="text" name="name[]" value="<?php echo (isset($_POST['name']))?$_POST['name']:'';?>">
<input type="text" name="name[]" value="<?php echo (isset($_POST['name']))?$_POST['name']:'';?>">
As you see the input is an array.
The problem is when an error occurs, the inputs become Array not the previous text in the field.
How can I solve this problem?
When you used echo $_POST['name'] while $_POST['name'] is an array, you will echo "Array". You should echo each element in this array
<?php
if (is_array($_POST['name'])){
foreach($_POST['name'] as $name){
echo '<input type="text" name="name[]" value="' . htmlspecialchars($name) . '"\n";
}
}
Also make it a habit to escape untrusted values with htmlspecialchars if you are going to inject them into the HTML, to protect against XSS attacks.
Since the input is an array, so is $_POST['name'], so you need to index into it to get the name, using indexes in the same order as the inputs in your file:
<input type="text" name="name[]" value="<?php echo (isset($_POST['name'][0]))?$_POST['name'][0]:'';?>">
<input type="text" name="name[]" value="<?php echo (isset($_POST['name'][1]))?$_POST['name'][1]:'';?>">
Related
I generate a form, which mostly consists of input fields that are already populated with values from the db.
So I do this currently like so:
<input id="misc" name="misc" value="<?php echo $workout['misc']; ?>" />
But when I try and do this:
<input id="misc" name="misc" value="<?php echo set_value($workout['misc']); ?>" />
along with a validation rule, the form does reload itself, the error message does display BUT the form is reset
What am I doing wrong?
As per the manual:
Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
The above form will show "0" when loaded for the first time.
Hence in your case:
<input id="misc" name="misc" value="<?php echo set_value('misc', $workout['misc']); ?>" />
OR
<input id="misc" name="misc" value="<?php echo set_value('misc'); ?>" />
Documentation:
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
I have a form with multiple text inputs that all have the same name. How would I process that with my PHP when the user submits the form?
HTML:
<input type="text" name="interest"/>
I've assumed you're using POST.
You would use
<input type="text" name="interest[]">
Then on the post page, you could use:
foreach($_POST['interest'] as $i){
echo $i. "<br>";
}
or whichever method you wanted to use to get the POST data.
You could also do something like:
<input type="text" name="interest[music]"/>
<input type="text" name="interest[food]"/>
You can then call this data by using:
<?php echo $_POST['interest']['music']; ?>
<input type="text" name="interest[]"/>
You should add square brackets. This triggers PHP to put them in an array like this:
HTML
<input type="text" name="interest[]"/>
<input type="text" name="interest[]"/>
PHP
//Get values
var_dump($_POST['interest']);
Use brackets in your input field to create an array of POST values:
<input type="text" name="interest[]"/>
<?php
var_dump($_POST['interest']); // will be an array
?>
I would like to check the type of an input field text is. Cause i only want to make thos fields read only. I know i can do this in javascript, but is there any way that this is possible in php?
best regrads.
same as javascript use this
<input name="address" type="text" readonly="readonly">
this make field read only you can't change it.
<input name="address" type="text" value="<?php echo "your value"; ?>" readonly="readonly">
try this code
To disable an input field add disabled attribute:
disabled="disabled"
This has to be done when generating the html or with javascript after generation.
This is a clean way of doing it and the user sees directly that it is just readable but not editable.
When you submit a form, in php you take the name attribute to get the posted value. Say you have
<input name="address" type="text">
In php:
if($_POST['address']){
//don't process address cause you know this is of type text
}
This is how you can do it in php,if $field is false then set input disabled
<input type="text" name="" <?php echo ($field == false)? 'disabled="disabled"' : '';?> value="" />
I have a form that is being generated on the fly in PHP using the below ($theFeatures is a multidimensional array):
<?php
foreach($theFeatures as $theKey => $theValues) {
?>
<input type="text" value="<?php echo $theValues['Key']; ?>" name="theFeatures[<?php echo $theKey; ?>]['Key']" size="6" />
<input type="text" value="<?php echo $theValues['Value']; ?>" name="theFeatures[<?php echo $theKey; ?>]['Value']" /> <br />
<?php
}
?>
This should create two input boxes that look like this:
<input value="Type" name="theFeatures[4]['Key']" size="6" type="text" />
<input value="Bird Table" name="theFeatures[4]['Value']" type="text" />
How can I get the first part of the array ( [4] in the above example ) in jQuery so I can store that as a JavaScript variable and use it else where in my jQuery??
Cheers,
Andy
You could try just storing the value in a separate attribute on the input and then retrieving that attribute with jQuery.
Adding the new attribute to the input
<input type="text" value="<?php echo $theValues['Key']; ?>" name="theFeatures[<?php echo $theKey; ?>]['Key']" size="6" key="<?php echo $theKey; ?>"/>
Retrieving the attribute with jQuery
var key = $("input").attr("key");
Certainly T B's suggestion could work. You can also do the following to get all input boxes that have "Key" in the name and get the value in the first set of square brackets. This will get any input object with the name containing Key.
$("input[name*=Key]").each(function() {
var name = $(this).attr("name");
var key = name.substring(name.indexOf("[") + 1, name.indexOf("]");
//do whatever is needed with key
});
So I have this empty textboxes in a registrationg page. The user enters some data, hits continue and then there's a confirmation page. If the data is incorrect, the user hits go back to go correct whatever was wrong. However, when he goes back, all the textboxes are empty. So the first thing that comes to my mind is to store the user data in a Session (I have a User class that holds all this data so I store the class in the session). When the user goes back I am able to retrieve the data.
I do something like this:
if($_SESSION['UserInfo'])
{
$user = $_SESSION['UserInfo'];
$firstName = $user->FirstName;
$lastName = $user->LastName;
}
How would I put these variables in a textbox?
To set the value, you can just echo out the content inside the value attribute:
<input type="text" name="firstname" value="<?php echo htmlentities($firstName); ?>" />
<input type="text" name="lastname" value="<?php echo htmlentities($lastName); ?>" />
Of course you will want to escape it but...
<input type="text" value="<?php echo $firstName ?>" />
or if the form is posted, it would be easier to do:
<input type="text" name="firstName" value="<?php echo $_POST['firstName'] ?>" />
fine... even though it was out of the scope of the question here is the escaped version:
<input type="text" name="firstName" value="<?php echo htmlentities($_POST['firstName']) ?>" />
smth like
<input type="text" value="<?php echo $first_name;?>">
Don't forget to escape with htmlentities() or smth similar. If you don't know why - google XSS.