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
Related
I have a drop down that's numbered 1-30. So if I select '2', my radio buttons will appear like this:
<input type="radio" id="yes-sdf-1" name="field[1][sdfradio]" value="Yes" />
<input type="radio" id="no-sdf-1" name="field[1][sdfradio]" value="No" />
<input type="radio" id="yes-sdf-2" name="field[2][sdfradio]" value="Yes" />
<input type="radio" id="no-sdf-2" name="field[2][sdfradio]" value="No" />
How do I get the individual values of the radio buttons because I want to set conditions that will only affect those with the same name.
Here is an example to give you an idea.
Try this:
$('input:radio').click(function(){
alert($(this).attr('name'));
});
This will return the name of the radio button.
JSFiddle
this is one radio button set
<input type="radio" name="image" id="100" value="a"/>
<input type="radio" name="image" id="100" value="b"/>
<input type="radio" name="image" id="100" value="c"/>
<input type="radio" name="image" id="200" value="d"/>
<input type="radio" name="image" id="200" value="e"/>
<input type="radio" name="image" id="300" value="f"/>
<input type="radio" name="image" id="300" value="g"/>
<input type="radio" name="image" id="400" value="h"/>
and so on...
and this is another radio button set
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>200
<input type="radio" name="number" value="300"/>300
<input type="radio" name="number" value="400"/>400
<input type="radio" name="number" value="500"/>500
<input type="radio" name="number" value="600"/>600
my question is how can i compare value of one with value="100" to other with id="100" in php
there is a comparison i want to make but for the field with id = 100 i have values as abcd.. so i cant assign it value = 100 cuz i m saving it in database
means..
there are other fields with id=100 but their values shall be diffrent otherwise i m not able to identify them after they are send to databse
still i have to do comparison of both.. and that too serverside
so what are my options?
more detailed explanation....
there are two sets of radio buttons..
in one set there are values..
and in other sets there are images related to those values..
when user clicks set of value... and user click on set of images
so php shall match that image to the value.. if user clicked on 100 as value and later he clicks on an image under the id 200
database shall not allow it to enter and gave him some error
Sorry Had to see what your talking about. Help to edit this so to understand your requirement, 'shall match that image to the value'
<input type="radio" name="image" class="100" value="a_100"/>
<input type="radio" name="image" class="100" value="b_100"/>
<input type="radio" name="image" class="100" value="c_100"/>
<br>
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>
<input type="radio" name="number" value="300"/>
<?php
$image = strstr($_POST['image'],'_');
$number = '_'.$_POST['number'];
if($image == $number) {
//Do something
}
The question is looking for to compare the two fields in PHP. Presumably this means that you want to examine them when the form is posted.
This means that you'll only have available to you the parts of the field that are sent via the POST -- ie the field name and the value. PHP will never see the id of any of the fields, nor the class, nor any other attributes in the HTML code other than name and value.
Therefore the discussion in the comments about whether or not to use id is a somewhat moot point -- even if it is a good point for you to know in general for your HTML code, you can't really use either id or class here anyway because PHP will never see them.
You need to take an entirely different approach.
[EDITED after question edit]
Add the 100 or 200, etc to the value of the image radio buttons, like so:
<input type="radio" name="image" value="a_100"/>
<input type="radio" name="image" value="b_100"/>
<input type="radio" name="image" value="c_100"/>
<input type="radio" name="image" value="d_200"/>
<input type="radio" name="image" value="e_200"/>
<input type="radio" name="image" value="f_300"/>
<input type="radio" name="image" value="g_300"/>
<input type="radio" name="image" value="h_400"/>
(The number radio button set remains unchanged from your question)
You can then read it in PHP as follows:
$number = $_POST['number'];
list($imgNumber, $imgValue) = explode('_',$_POST['image']);
You can now compare $number with $imgNumber.
Hope that helps.
It's worth asking a follow-up question though: What are you trying to achieve here? Is this a validation exersise? ie where you're checking that the user is selecting an image option that matches the number option he's picked? If that's what you're doing, you may find it's more user friendly to use Javascript to filter the options available when they select the number option so that the image radio button set only shows options that are valid for the selected number.
This would be an entirely different question, so I won't go into any detail here, but I would suggest that it might be a more user-friendly way of doing things if you did it like that.
I try to implement the follwing code:
<?php
$yesno1="";
$yesno2="";
$option1="";
$option2="";
$option3="";
if(isset($_POST['submit'])){
$yesno1=$_POST['yesno1'];
$yesno2=$_POST['yesno2'];
$option1=$_POST['option1'];
$option2=$_POST['option2'];
$option3=$_POST['option3'];
}
?>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<input type="radio" name="yesno1" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno1" value="no" style="outline:0;"/>No
<input type="radio" name="yesno2" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno2" value="no" style="outline:0;"/>No
<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>
<input type="submit" value="Submit" name='submit'>
</form>
Once I click submit button, normally the value I seleted in radio and marked in the checkbox are gone, so how can I keep the value even after the click the submit button or refrsh the page using php, javascript is fine as well, would be better if any one can help me with it on both php and javascript since I am not very good on both of them, thanks for kind help:)
An example below
<input type="checkbox" name="option1" value="Milk" <?php echo ($_POST['option1'] == 'Milk' ? 'checked' : '') ?>> Milk<br>
From MDN:
The input (<input>) element is used to create interactive controls for
web-based forms.
Attributes
type
[...]
radio: A radio button. You must use the value attribute to define the
value submitted by this item. Use the checked attribute to indicate
whether this item is selected by default. Radio buttons that have the
same value for the name attribute are in the same "radio button
group"; only one radio button in a group can be selected at one time.
This is a basic HTML question. Learn to walk before you run ;-)
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.
A very basic question...
How can I only allow the selection of one option in a list of radio buttons?
<form action="process_repair.php" method="POST">
<label for "repair_complete">Repair complete</label>
<input type="radio" name="Yes" value="true">
<input type="radio" name="No" value="false">
</form>
When this code runs, it's possible to select both radio buttons, but I'd like them to interact so you can only select one or the other.
Any help much appreciated! :)
Give them the same name.
<form action="process_repair.php" method="POST">
Repair complete
<input type="radio" name="complete" value="true" id="complete_yes" />
<label for="complete_yes">Yes</label>
<input type="radio" name="complete" value="false" id="complete_no" />
<label for="complete_no">No</label>
</form>
Labels must have a for attribute, directing to the corresponding input's id.
Every input should have same "name"