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.
Related
I understand that when accessing values of HTML form elements in PHP, we must use the name attribute within the $_POST variable. However, I'm not sure I understand why that's the case. It seems to me like it may have been better for $_POST to use id attributes and take advantage of the insured uniqueness, rather than potentially finding multiple values if more than 1 form elements share the same name. I'm sure there's a good reason why name attributes are used instead of id, but I haven't been able to dig that up and curious to understand why.
Simple example:
index.html
<html>
<form action="./some_script.php" method="post">
<input type='text' id='text_input_id' name ='text_input_name'>
<input type='submit'>
</form>
</html>
some_script.php
<?php
$value_from_id = $_POST["text_input_id"]; // will be null
$value_from_name = $_POST["text_input_name"]; // will contain text input from form
?>
The primary reason is that forms were added to HTML before IDs were.
However, an ID must be unique in an HTML document but a name doesn't. You can have multiple form controls with the same name.
Sometimes this is required e.g. in order to create a radio group:
<form>
<input type="radio" name="availability" value="yes" id="availability_yes">
<label for="availability_yes">Yes</label>
<input type="radio" name="availability" value="no" id="availability_no">
<label for="availability_no">No</label>
<input type="radio" name="availability" value="if need be" id="availability_inb">
<label for="availability_inb">If Need Be</label>
</form>
and sometimes it is just useful (e.g. when picking from a list of things).
<form>
<fieldset>
<legend>Acceptable Colours</legend>
<input type="checkbox" name="colours[]" value="red" id="colours_red">
<label for="colours_red">Red</label>
<input type="checkbox" name="colours[]" value="green" id="colours_green">
<label for="colours_green">Green</label>
<input type="checkbox" name="colours[]" value="blue" id="colours_blue">
<label for="colours_blue">Blue</label>
</fieldset>
</form>
OK, this is probably elementary, but I'm having a mental block.
I have a set of radio buttons and I need to submit the value to a URL in the following format:
mypage/myValue
and not
mypage/?name=myValue
Here's what I have:
<form action="mypage/" method="get">
<input type="radio" name="name" value="myValue_1"> label 1
<input type="radio" name="name" value="myValue_2"> label 2
<input type="radio" name="name" value="myValue_3"> label 3
<input type="submit">
</form>
Perhaps I'm overthinking but my initial reaction is to form the URL with jQuery on click, and than just redirect... Sounds a bit too much, isn't it?
It shouldn't be mush code (jQuery) if it's only the radios - something like:
<form>
<input type="radio" name="rname" value="myValue_1"> label 1
<input type="radio" name="rname" value="myValue_2"> label 2
<input type="radio" name="rname" value="myValue_3"> label 3
<input type="button" id="btncl">
</form>
and in the script:
$("#btncl").click(function(){
window.location="mypage/"+$('input[name=rname]:radio:checked').val();
});
Otherwise, I don't think there is a way of doing it with regular html, since the standards say that GET is ...?name=val&n2=v2....
The server side redirection should be more time-consuming (for both writing and using)
I am wondering how I can extract the title variable from my forms posted fields array.
I know $_POST['name'] is the field name but can I do something like $_POST[title] to get the title?
I ask because I have a dynamic form with variable lengths. The dynamics is group_one contains 5 fields, group_two contains 12 fields, group_three contains 2 fields for example.
I am hoping to loop through these groups and post the title of the form field to a column in a DB and its value. Any help appreciated with understanding if I can use the 'title' variable in the form field element.
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesYes" value="Yes" title="Dog at premises" />Yes
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesNo" value="No" title="Dog at premises" />No
<input type="text" name="txtGroupOne[]" id="txtNextOfKinName" title="Next of kin name" />
<input type="text" name="txtGroupOne[]" id="txtNextOfKinContact" title="Next of kin contact" />
No, only the name="x" attribute is sent by default in an HTML Form. title="x" is not sent. You can technically get around it with some crazy Javascript and an Ajax POST, but I would avoid that if I were you.
What do you want the title sent to your server side for? There surely probably a better alternative to achieve your goal.
You can add hidden field to each of fields that need titles to be sent to server besides their values.
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesYes" value="Yes" />
<input type="hidden" name="txtGroupOneTitles[]" value="Dog at premises" >
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesNo" value="No" />
<input type="hidden" name="txtGroupOneTitles[]" value="Dog at premises" >
<input type="text" name="txtGroupOne[]" id="txtNextOfKinName" />
<input type="hidden" name="txtGroupOneTitles[]" value="Next of kin name" >
<input type="text" name="txtGroupOne[]" id="txtNextOfKinContact" />
<input type="hidden" name="txtGroupOneTitles[]" value="Next of kin contact" >
You can do something like this:
foreach($_POST as $form_var){
// Compile your code here, etc.
}
Anything that does not have a "name" attribute in your form will not be part of the $_POST array.
ie,
<input type='text' id='bar'>
<input type='text' name='foo' id='foo'>
Only $_POST['foo'] would exist.
Misread the question slightly, but you could also prepend your input names to include the "group title" upon generation.
<input type='text' name='title1_foo' id='title1_foo'>
Then you can manipulate the information you want from the $_POST loop.
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.