I've got a bunch of <input type='text' name='input0' /> fields.
The user can add them dynamically so we'd get something like:
<input type='text' name='input0' />
<input type='text' name='input1' />
<input type='text' name='input2' />
...etc. We don't know how many there will be in total. When the form gets submitted, I want to loop through each of these input fields and assign them to a $_POST variable. These are NOT the only input fields in the form, there are other elements such as radio buttons, checkboxes and other text fields. My problem is I need to somehow identify these particular dynamically-generated text fields, and loop through them. How can I do this, when all I get on the server side are the names of the input fields?
Use:
<input type='text' name='input[]' />
then you can:
foreach($_POST['input'] as $input){
echo $input;
}
Related
The simplified form in the html code below contains a number of repetitive form fields which must be both validated by means of some jquery validate code and processed by means of some php code...I have set up the form field below to facilitate the php processing, but then again get stuck with writing the proper jquery validate code as the 'name' tags are supposed to be unique. If I'd make the name tags unique I think the php coding becomes more complex.
Just wondering what a proper structure of repetitive form fields would be which facilitates both the jquery validation coding and php coding?
Any suggestions?
Thank you in advance.
html code:
<input type="checkbox" id="check0" name="productselection[]" value="productselected0">
<input id="nrofparts0" type="text" name="nrofparts[]">
<input type="checkbox" id="check1" name="productselection[]" value="productselected1">
<input id="nrofparts1" type="text" name="nrofparts[]">
<input type="checkbox" id="check2" name="productselection[]" value="productselected2">
<input id="nrofparts2" type="text" name="nrofparts[]">
heres one way you can do it:
HTML
<form id="theForm" method="post">
<input type="checkbox" name="checky[0]" />
<input type="checkbox" name="checky[1]" />
<input type="checkbox" name="checky[2]" />
<input type="submit" name="submit" value="submit"/>
</form>
I add a key to the array because it makes it easier to determine which checkboxes were clicked. you wont get anything for an unclicked one. that can make it confusing
jquery:
$('#theForm input[type="checkbox"]').each(function(){
// if($(this))... some validation
});
php:
if(isset($_POST['checky'])){
foreach($_POST['checky'] as $key => $val){
echo $key.' '.$val; // this will give you the key of the checkbox, and the value
}
}
you could also have a hidden input that counts the checkboxes. That way when you loop, you can use it to set the empty checkboxes in your php code.
hope this helps!
Is there a way to get only a certain set of information from a form? Eg
<form>
<input name='I want this' value='one' type=text />
<input name='and this' value='one' type=text />
<input name='but not this' value='one' type=text />
</form>
Where, obviously, i only want the first two fields but not the third one? I've got a user inventory on my website that looks like this:
<form action="" method="POST">
<input name='item_id' value='1' type='hidden'>
<input type='button' name='slot1' value='1'>
<input type='button' name='slot2' value='2'>
<input name='item_id' value='2' type='hidden'>
<input type='button' name='slot1' value='1'>
<input type='button' name='slot2' value='2'>
</form>
I want the users to be able to select, item 1 and equip it to slot 1 but The only way i can think of doing this right now is to have them all be separate forms. and i feel like that would be bad coding.
Yes, using jquery, select only the first element and second elements value, post it using ajax, and retrieve and process data server side.
var i1 = $('form').eq(0).find('[name="item_id"]').val() //Values from first form only
var i2 = $('form').eq(0).find('[name="slot1"]').val()
$.ajax({
url: "test.php",
data: {i1:i1, i2:i2}, //Send this to php file.
}).done(function() {
$(this).addClass("done");
});
When you submit a form the values of all inputs associated with that form will be added to the $_POST (array) variable. You can always choose to ignore values when certain conditions apply. If that's not an option, I think you should opt for separate forms.
Another thing you could do—I do not understand the context of your problem, so I'm not sure if it applies to your situation—is have a user choose between "Item 1" and "Item 2" via radio buttons in your form. You can then base your form handling logic on the choice people made in the form.
If I make a long story very short, I have a short form I've made (an input, a select, and three checkboxes). I've made a function on a button that can dynamically add multiple instances of this form on my page. It saves it as an array (i.e. the input name is name="checkbox[]") which will save fine in my database. The problem I run into is I may have 6 instances of this form, but only some of the boxes are checked. So, I may have 6 text inputs, 6 select inputs, but maybe only 3 checkbox inputs. Since it only has 3 inputs, the array is only 3 pieces of data and when I run a for() statement it doesn't accurately save this information and tie it to the correct record.
I thought that maybe I could have a hidden input that will get its value assigned through javascript, but I don't know how to reference the checkboxes appropriately (you can't do id="blahblah[]" right?)
Sad and Confused,
ImmortalFirefly
I am not sure I have caught your drift on this one, but consider this:
<?php
var_dump( $_POST )
?>
<form name=form0 method= post action = "">
<input type=checkbox name=checkbox[0][0] />
<input type=checkbox name=checkbox[0][1] />
<input type=checkbox name=checkbox[0][2] />
<input type = submit>
</form>
Then another form is added
<form name=form1 method= post action = "">
<input type=checkbox name=checkbox[1][0] />
<input type=checkbox name=checkbox[1][1] />
<input type=checkbox name=checkbox[1][2] />
<input type = submit>
</form>
Mock that up in html and POST it back to a webpage and see how it works, you can iterate through th post value to see which form was sent and which box checked, or put it all in one single form.
<?php
var_dump( $_POST )
?>
<form name=form0 method= post action = "">
<input type=checkbox name=checkbox[0][0] />
<input type=checkbox name=checkbox[0][1] />
<input type=checkbox name=checkbox[0][2] />
<input type = submit>
Then another series of checkboxes is added :
<input type=checkbox name=checkbox[1][0] />
<input type=checkbox name=checkbox[1][1] />
<input type=checkbox name=checkbox[1][2] />
close off the form
<input type = submit>
</form>
<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
The case :
<form id='frm' name ='frm' action='test.php'>
<div style='display:none'>
<input type='text' name='name' value ='' />
</div>
<input type='submit' value='Submit' />
</form>
For example , How can i submit the from given above with its inputs ?
Issue : The "name" input wont be passed !
Programatically, you can just trigger the submit event on the form element:
$('#frm').submit();
Edit: Actually, after reading your markup more carefully, you are using an input named name, this element can cause "clashing" problems with the form's name attribute.
Consider changing the name of your input.
See also:
Unsafe Names for HTML Form Controls
As a user, you'd just click the submit button. The visibility of a form element doesn't change the fact that it gets submitted with the form.
Programmatically:
document.getElementById('frm').submit();
If you're not trying to show the input, why not use type="hidden" and dispense with the style?
document.getElementById('frm').submit();
Here is how to submit hidden variables in a FORM:
<input type='hidden' name='name' value ='' />