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.
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>
I need to add 2 input types for 1 form field in my html form viz. type=email and type=number for the html form field Email/mobile. How do I get this done? Below is the code:
<label for="modlgn-username">Email/mobile</label>
<input type="email" name="email" class="inputbox" size="18" required />
Is it possible to add input type=number as well in the above code for the same field namely [Email/mobile]?
I'm a beginner and will apprecaite help in the matter a lot
No it is not possible but you can take input type="text" for email/number.
Just to clarify, are you asking if you can assign two input types to the same field?
I have never come across this and pretty sure it isn't possible. The best you can do to capture an email address or a phone number in the same field would be to make the field input type text. However, this would mean that your validation would get a bit harder.
Also, a side note. To ensure your form fields are accessible to users using assistive and adaptive technology (see https://www.w3.org/WAI/tutorials/forms/) you want to make sure that your label "for" and your input "id" values match exactly. In the example below "firstname" is used to do this.
<label for="firstname">First name:</label>
<input type="text" name="firstname" id="firstname">
This will help the screen reader associate the label with the form field.
Cheers,
Choppie
All HTML inputs Types
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
Check Zedxtra.com.ng for how to use HTML tags
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 have a form with about 150 fields, however the user can add more fields, reaching about 300 fields.
I'd like to know if exist a good way to put names and IDs to my fields, or i have to give each one a diferent name and ID. It's a hard job.
html with only one name for input
<form action="checkbox.php">
<input type="checkbox" name="data[]" value="1">1<br>
<input type="checkbox" name="data[]" value="2">2<br>
<input type="checkbox" name="data[]" value="3">3<br>
<input type="checkbox" name="data[]" value="4">4<br>
<input type="checkbox" name="data[]" value="5">5<br>
<input type="submit">
</form>
checkbox.php
$data = $_GET["data"]; //$data is an array with checked option
it works also for type text
<input type="text" name="text_data[1]"><br>
<input type="text" name="text_data[2]"><br>
<input type="text" name="text_data[3]"><br>
if i understand in right, then you can use same name,id for group of same types of control (group of textbox,checkbox,etc...) and then access as array from javascript or code behind ..
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.