Accessing multiple cloned fields from PHP - php

I have a form which has the ability to copy a row of several fields using jquery - my question is how do I access these form values in the target php page?

Any code, by chance? Anyway, you can make it add a name to the new field with square braces, so ti will be accessed as an array, like it happens with multiselect checkboxes
es: new field 1 <input type="text" name="added[]" value="">
new field 2 <input type="text" name="added[]" value="">
and so on...
Then you have everythin in the $_POST['added'] array

If they have the same 'name' attribute value, change that value to 'name[]' so that they look like
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
<input type="text" name="name[]" />
//etc...
and you should be able to access them by using:
$value = $_POST['name'][0];
where 0 is the index of the field, IE, the first field is 0, second is 1...
It is easier to access these using a for loop
for($i = 0; $i < count($_POST['name']; $i++)
// actions with $_POST['name'][$i]
or a foreach loop.
foreach($_POST['name'] as $value)
// actions with $value

Depends on how jquery is adding them. Do the following on the called page and see how they're being passed through.
var_dump( $_POST ); // Or maybe $_GET

Related

dynamically selecting all variables with the same prefix in php

I have a form and there is a button to append another set of input boxes if you wish to add more information. Everytime it adds a new set of boxes all the input boxes get a unqiue number added on for that set of input boxes.
Example:
If you have three sets of input boxes it would look like this:
name, age, gender, dob
name1, age1, gender1, dob1
name2, age2, gender2, dob2
However, when I send this information over to my php file I extract the information from the array so each one is a variable. So, name would be $name and name1 would be $name1 and so on. But my question is how can I sanitize and validate all the names at once and all the ages at once etc..
The reason I am asking is because I have googled this alot and I can't find an answer on how to do this.
Try to create sets as given in sample below:
For first set:
<input type="text" name="name[]" id="name1" />
<input type="text" name="gender[]" id="gender1" />
<input type="text" name="age[]" id="age1" />
<input type="text" name="dob[]" id="dob1" />
For second set:
<input type="text" name="name[]" id="name2" />
<input type="text" name="gender[]" id="gender2" />
<input type="text" name="age[]" id="age2" />
<input type="text" name="dob[]" id="dob2" />
and set all the further sets accordingly.
Now, to get posted data you can use
<?php
echo "<pre>".print_r($_POST, true)."</pre>";
?>
You may use something like this for each entity:
<input type="text" name="age[]" id="age1" />
Here, id should be in incremental order with JavaScript or jQuery and name should be same which will give you an array for all the attributes in $_POST or $_REQUEST
Print $_REQUEST and you will come to know how exactly you can get all the data.
You are already getting the array for the name, age, etc.
To sanitize use array_map() function in php. It will sanitize the array.
EXAMPLE
$result = array_map("stripslashes", $result);
Here $result is an array

Creating an Array with PHP

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
?>

Inserting paired input fields into mysql

I need to insert pairs of input values from a form into mysql database. For example here are my input boxes:
<input type="text" name="roomType1" size="30" />
<input type="text" name="roomRate1" size="30" />
<input type="text" name="roomType2" size="30" />
<input type="text" name="roomRate2" size="30" />
<input type="text" name="roomType3" size="30" />
<input type="text" name="roomRate3" size="30" />
etc..
And my sql database is set up as follows:
RoomType
RoomRate
HID
So basically I need to figure out how to pass the two input fields into each field together in the same row. I am not sure if I should do a for loop or how I can get each two and insert it with the same ID. I hope this makes sense. and any help would be GREATLY appreciated!
Those inputs must be part of a form with method="post". Then, on the php side use this:
<?php
$i = 1;
while(isset($_POST['roomType'.$i]))
{
$roomType = $_POST['roomType'$i];
$roomRate = $_POST['roomRate'$i];
// perform your sql statements here
$i++;
}
?>
Since your inputs are paired, it's enough to check if only one of them exists;

Combining form textfield values using php implode

I have 3 text fields and I want to pass the values after combining them using a hyphen.
<input type="text" name="val[]" />
<input type="text" name="val[]" />
<input type="text" name="val[]" />
Preferably help me with php implode option.
How do i retrieve it after submit ?
Thanks.
After sending the form, your values will be in $_POST['val'] or $_GET['val'] as an array, depending on the method of your form.
You can combine them simply by:
$hyphenated = implode("-", $_POST['val']); // or $_GET['val']
thanks. how do i change focus to next field once a field has max values:
See if this works:
<input type="text" name="val[]" onkeyup='checkVals("field1", "field2");' id='field1'>
<input type="text" name="val[]" onkeyup='checkVals("field2", "field3");' id='field2'>
<input type="text" name="val[]" id='field3'>
<script>
function checkVals(this_field, next_field){
var fieldval = document.getElementById(this_field).value;
var fieldlen = fieldval.length;
if(fieldlen > 10){ // you can change 10 to something else
document.getElementById(next_field).focus();
document.getElementById(next_field).select();
}
}
</script>

reading two form elements with same name

<form action="test.php" method="post">
Name: <input type="text" name="fname" />
<input type="hidden" name="fname" value="test" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
How can I read the values of both the fields named fname?
On my action file(test.php) under $_POST, I am getting only hidden field value.
Is there any PHP setting through which I can read both values?
I believe you want to name the fields as:
Name: <input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
to make PHP understand them as an Array.
In case someone wants to do this and doesn't want to change the name of the form elements, or can't, there is still one way it can be done - you can parse the $_SERVER['QUERY_STRING'] or http_get_request_body() value directly.
It would be something like
$vals=explode('&',http_get_request_body());
$mypost=Array();
foreach ($vals as $val) {
list($key,$v)=explode('=',$val,2);
$v=urldecode($v);
$key=urldecode($key);
if ($mypost[$key]) $mypost[$key][]=$v;
else $mypost[$key]=Array($v);
}
This way $mypost ends up containing everything posted as an array of things that had that name (if there was just one thing with a given name, it will be an array with only one element, accessed with $mypost['element_name'][0]).
For doing the same with query strings, replace http_get_request_body() with $_SERVER['QUERY_STRING']
If you want to pass two form inputs with the same name, you need to make them an array. For example:
<input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
You can then access it using the following:
$_POST['fname'][0]
$_POST['fname'][1]
You might want to rethink whether you really need to use the same name though.
Solutions are
1) Try using different name for textbox and hidden value
2) Use an array as mentioned above for field name
3) Its not possible as the values will be overwritten if the names are same

Categories