I have a form that dynamically prints out names based on a unique name tag.
To be used for responding on invitations. There can be multiple users for each name tag, i.e. couples, couples with children, etc.
For each name there is a radio-button where the invitee can select whether he/she is coming to the party or not (has the value 1=not coming, 2=coming). It can look like this, for a couple:
Lady Mary S
I am not coming - radio - value 1
I am coming - radio - value 2
Mister Hans S
I am not coming - radio - value 1
I am coming - radio - value 2
Child of Mary & Hans S
I am not coming - radio - value 1
I am coming - radio - value 2
Each of these invitees has a user id - I need the reponse to be with the user id.
The HTML for it looks like this:
<div>
<h1>Lady Mary S</h1>
<label for="no-1">I am not coming</label>
<input type="radio" value="1" id="no-1" name="response[1]" /><br />
<label for="yes-1">I am coming</label>
<input type="radio" value="2" id="yes-1" name="response[1]" />
<input type="hidden" name="userid[]" value="1" />
</div>
<div>
<h1>Mister Hans S</h1>
<label for="no-2">I am not coming</label>
<input type="radio" value="1" id="no-2" name="response[2]" /><br />
<label for="yes-2">I am coming</label>
<input type="radio" value="2" id="yes-2" name="response[2]" />
<input type="hidden" name="userid[]" value="2" />
</div>
<div>
<h1>Child of Mary & Hans S</h1>
<label for="no-3">I am not coming</label>
<input type="radio" value="1" id="no-3" name="response[3]" /><br />
<label for="yes-3">I am coming</label>
<input type="radio" value="2" id="yes-3" name="response[3]" />
<input type="hidden" name="userid[]" value="3" />
</div>
These names will be fetched from MySQL with a unique nametag for these 3 people, f.x. MARHAN
This is a dynamic form - for some nametags there will only be two invitees and for other nametags there will be 3 or 4, etc.
My problems is that I cannot come up with a solution on how to handle the posted data so I can make sure the reponse is for the right user id.
All you need is
foreach($_POST['response'] as $userid => $value) {
echo $userid.'-'.$value."<br>";
}
Because you already have userid in key of radio button name name="response[1]"
Just an idea: How about making the value of each input something like 'answer-userId'. e.g.:
<input type="radio" value="1-333" id="no-3" name="response[]" /><br />
Then you can split the posted value in php by '-' and get the answer (1) and user id (333).
Related
goal: post several groups of radio buttons to array (as a message, to database, etc.)
Hi there,
I was wondering how i can post my selections of several groups of radio buttons.
Here is the code:
<body>
<form action="" method="post">
<select name="module 1">
<input type="radio" name="radioA" value="Radio 1">Radio 1
<input type="radio" name="radioA" value="Radio 2">Radio 2
<input type="radio" name="radioA" value="Radio 3">Radio 3
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 2">
<input type="radio" name="radioB" value="Radio 21">Radio 21
<input type="radio" name="radioB" value="Radio 22">Radio 22
<input type="radio" name="radioB" value="Radio 23">Radio 23
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 3">
<input type="radio" name="radioC" value="Radio 31">Radio 31
<input type="radio" name="radioC" value="Radio 32">Radio 32
<input type="radio" name="radioC" value="Radio 33">Radio 33
<input type="submit" name="submitC" value="Get Selected Values" />
</select>
<br>
</form>
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['radioA']))
{
echo "You have selected :".$_POST['radioA']; // Displaying Selected Value
}
}
?>
</body>
...But I can't make it display the list of my selections, but only one instructions, instead of 3x (one for each).
Long story short... How can I treat this as a 1 global set of 3 groups, having only 1 "submit" button to process all 3 groups at once?
Txs a lot, and have a nice weekend.
You just need to remove the other two submit buttons and only display one. As long as it is inside the <form> it will submit all the radio button "groups" to the $_POST.
Also, remove the select tags as you are using them incorrectly. Only an option or optgroup should be nested inside a select element.
You will need to reference each one individually to see the selected response:
echo $_POST['radioA'];
echo $_POST['radioB'];
echo $_POST['radioC'];
I am creating a quiz application...
I have created a question text area, and a number of input fields for options..
Each input field has a radio button, if user clicks it, it will consider the associated input field as correct answer..
I have created some input fields and radio button with each input field
<div id="items">
<input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct" value="yes"/> <br><br>
<input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct" value="yes"/>
</div>
when i Submit this form it stores yes with each Option. I am unable to understand how to store the value of only 1 input field.
Kindly guide me
Thanks.
if you get value of options is input1 answer is input1 or if options value is input2 answer is input2
<input type="radio" name="options" value="input1">Is Correct<br>
<input type="radio" name="options" value="input2">Is Correct
input1 is name of textbox 1
input2 is name of textbox 2
Get value on form post :-
$nam=$_POST['options'];
$answer=$_POST[$nam];
You can use input as below :
<div id="items">
<input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct[]" value="yes"/> <br><br>
<input class="form-control" type="text" name="options[]" required /> Is Correct: <input type="radio" name="is_correct[]" value="yes"/>
</div>
I have a form that has a set of radio buttons, and I am sending the value of the checked button to another page with php. The form has the option to add another set of the same radio buttons, so if I use the following method of giving name attributes:
<div>
<input type="radio" name="food[]" value="apple" /> Apple
<input type="radio" name="food[]" value="orange" /> Orange
</div>
And add a clone of the above,
$_POST['food'];
This would give me the value of each input, and that would total 4. I'm trying to use the bracket incrementing method but still keep the functionality of returning the value of the radio button that was checked.
So my question is, is there a way to use the square brackets to do auto incrementing on the name attribute, but when sending the values to php make sure each div grouping of radio buttons only returns one of the values for whichever one is checked? Please let me know if this question is too vague, this is my first question here.
May i don't understand your question. You can not Auto Incremented the name cause you use radio buttons. By Choosing apple on the first page and orange on the second you will loose your first choose (apple). You can do it with checkboxes instead of radio buttons:
Page 1:
<form method="post" action="page2.php">
<div>
<input type="checkbox" name="numberofapples" value="1" /> Apple
<input type="checkbox" name="numberoforanges" value="1" /> Orange
</div>
<input type="submit" value="go">
</form>
Page 2:
<form method="post" action="result.php">
<div>
<?if($_POST['numberofapples']){?><input type="hidden" name="numberofapples" value="1" /> <?}?>
<input type="checkbox" name="numberofapples" value="<?=($_POST['numberofapples'])?2:1?>" /> Apple
<?if($_POST['numberoforanges']){?><input type="hidden" name="numberoforanges" value="1" /> <?}?>
<input type="checkbox" name="numberoforanges" value="<?=($_POST['numberoforanges'])?2:1?>" /> Orange
</div>
<input type="submit" value="go">
</form>
Result page:
Apples: <?=($_POST['numberofapples'])?$_POST['numberofapples']:0?>
Oranges: <?=($_POST['numberoforanges'])?$_POST['numberoforanges']:0?>
But why don't you do the counting after posting your second page? (add the post values from page 1 as hidden fields to the form of page 2)
If you want to use several div groups with the same input names, I would create them like this:
<div>
<input type="radio" name="food[0][]" value="apple" /> Apple
<input type="radio" name="food[0][]" value="orange" /> Orange
</div>
<div>
<input type="radio" name="food[1][]" value="apple" /> Apple
<input type="radio" name="food[1][]" value="orange" /> Orange
</div>
<div>
<input type="radio" name="food[2][]" value="apple" /> Apple
<input type="radio" name="food[2][]" value="orange" /> Orange
</div>
So, in your PHP, you will have each group in separated keys. For example
print_r( $_POST['food'] );
Should return something like this:
array(
[0] => array(
[0] => 'apple'
),
[1] => array(
[0] => 'orange'
),
[2] => array(
[0] => 'apple',
[1] => 'orange'
)
)
)
I am developing a PHP website which will generate several approval requests.
My approval page has only one form and one save button, and I want to use 2 radio buttons for each request.
Here is a link to how I imagine it to look like: link
On the PHP side I have no problem with generating the form and assigning different names to the radio buttons. My problem is what names I should give to the radio buttons in order for me to be able to associate them to a specific member on the PHP side? Or do I need to set values for the radio buttons?
How would I accomplish this?
If you have an even better solution to handle such a case I am open to that as well.
Thanks
Every User will have a unique user_id,
As you are displaying radiobuttongroup for each other user in the page, and want to access then in php,
The base way to name them in a array with indices as their user_id
<input type="radio" value="1" name="acceptance['uid1']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid1]"/>Deny
<input type="radio" value="1" name="acceptance['uid2']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid2]"/>Deny
<input type="radio" value="1" name="acceptance['uid3']"/>Approve<br />
<input type="radio" value="0" name="acceptance[uid3]"/>Deny
in php
foreach($_POST['acceptence'] as $uid=>$acpt)
{
..Do whatever
}
I hope this would help..If something else is needed plz comment..
Here you are:
<input type="radio" value="approve" name="acceptance"/>Approve<br />
<input type="radio" value="deny" name="acceptance"/>Deny
With radio buttons, they should named the same and have different values for grouping them.
UPDATED
To me, I will add row id to each radio group buttons. For example
<input type="radio" value="19" name="acceptance1"/>Approve<br />
<input type="radio" value="0" name="acceptance1"/>Deny
...
<input type="radio" value="21" name="acceptance2"/>Approve<br />
<input type="radio" value="0" name="acceptance2"/>Deny
...
<input type="radio" value="23" name="acceptance5"/>Approve<br />
<input type="radio" value="0" name="acceptance5"/>Deny
Assuming we have 5 rows per page and each approve radio button holds id of a records in database.
<?php
$approve = array();
for($i=1;$i<=5;$i++)
$approve[] = $_POST['acceptance'.$i];
?>
Then you will get what row has approve selection (i.e value !=0). Hope this helps
Is it possible to conditionally add hidden input fields into a form?
eg I have a php form that is adding values to a table and if the appleID = 1 or 2 then I want 1 added to the fruits column of my table and if appleID =3 I want 1 added to the sweets column of my table. I thought I might be able to do something like the below but it is adding all hidden values no matter what I select. Or should I approach this a different way?
<input type="radio" value="1" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="2" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="3" name="appleID" />
<input type="hidden" value="0" name="fruits" />
<input type="hidden" value="1" name="sweets" />
Thanks I haven't done much with php so I will need to explore that option further. Thanks for the feedback.
I was also looking at something like the below. But PHP sounds likes the better option.
change field value when select radio buttons
You can either use all values in the radio buttons (1,1,0 - 2,1,0 - 3,0,1) and split them after receiving them in your PHP script or add/delete the hidden fields via JavaScript.
Split Example:
HTML:
<input type="radio" value="1,1,0" name="appleID" />
<input type="radio" value="2,1,0" name="appleID" />
<input type="radio" value="3,0,1" name="appleID" />
PHP:
if (!empty($_POST['appleID']))
{
list($appleID, $fruits, $sweets) = explode(",", $_POST['appleID']);
}
It is better to put that logic into the PHP script instead of using Javascript - because you have to do the validation anyway.