I have a page of questions that have two options as radio buttons.Each Question has a form tag.Now i want to store the values checked by the user to a database through a single function in php.How to do it?
My html code is as follows:
<li>Question1 </li>
<br /><form>A.
<input type="radio" name="radio" id="1ARadioButton" value="1ARadioButton" />
<label for="1ARadioButton">Answer1</label><br /><br />
B.
<input type="radio" name="radio" id="1BRadioButton" value="1BRadioButton" />
<label for="1BRadioButton"> Answer2</label>
</form><br /><br />
<li>Question2</li>
<br /><form>A.
<input type="radio" name="radio" id="2ARadioButton" value="2ARadioButton" />
<label for="2ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="2BRadioButton" value="2BRadioButton" />
<label for="2BRadioButton">Answer2</label>
</form><br /><br />
<li>Question3</li>
<br /><form>A.
<input type="radio" name="radio" id="3ARadioButton" value="3ARadioButton" />
<label for="3ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="3BRadioButton" value="3BRadioButton" />
<label for="3BRadioButton">Answer2</label>
</form><br /><br />
You've got the same name on all your radio buttons, so you're only going to get the value of the LAST radio button that's checked off. You're going to be recording the same answer for ALL questions (which is usually C, right?).
element IDs are NOT used for form submissions. They're used only for DOM operations. On HTML forms, only the name and value type attributes are relevant for the form submission process.
What you should have is:
Question 1:
<input type="radio" name="question1" value="option_A" />
<input type="radio" name="question1" value="option_B" />
Question 2:
<input type="radio" name="question2" value="option_A" />
<input type="radio" name="question2" value="option_B" />
etc...
As for storing them in the database, that's the same as storing any other form-data in a database. Get the radio's value with $_POST['question1'] or whatever, and do the usual escaping/query building/inserting.
Related
I am using the code below to ask users to rank which programming language they are more comfortable with.
The users need to rank from 1-3 (1 being the one they are most comfortable with)
<form id="form1" name="form1" method="post" action="">
<input type="number" name="php" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="python" required="required" max="3" min="1"/>Python <br />
<input type="number" name="ruby" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
Once the user prioritizes the programming languages and hits submit, how can I on the next page echo the ranking selection? (e.g. Your first choice is x, your second choice is y and your third choice is z)
I would do it like so (Note that I've changed the the value of the name attributes on the form elements):
<form id="form1" name="form1" method="post" action="">
<input type="number" name="lang[php]" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="lang[python]" required="required" max="3" min="1"/>Python <br />
<input type="number" name="lang[ruby]" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
And in the php:
//Get the form results (which has been converted to an associative array) from the $_POST super global
$langs = $_POST['lang'];
//Sort the values by rank and keep the key associations.
asort($langs, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($langs as $lang => $rank)
{
//echo out here first, second, and third rank with each iteration respectively.
}
The asort function simply sorts the array by value while maintaining key association.
I am not sure that tag input type="number" exists.
you do better
<legend>
<label><input type="radio" name="php" value="1">1</label>
<label><input type="radio" name="php" value="2">2</label>
<label><input type="radio" name="php" value="3">3</label>
</legend>
<legend>
<label><input type="radio" name="python" value="1">1</label>
<label><input type="radio" name="python" value="2">2</label>
<label><input type="radio" name="python" value="3">3</label>
</legend>
you must not use 'required' attribute for radio tag or checkbox tag
so you make a check javascript function whether radio box is checked or not.
<form name..... onsubmit = "return check_submit();">
<script>
var check_submit = function(){
if($("input[name=php]:checked").val() =="")
return false;
...
return true;
}
</script>
or you can use
<input type="text" name="php">
then on next page you can do like this
$php = intval(trim($_POST['php']));
$python = intval(trim($_POST['python']));
$msg = "your first choice for php is '.$php;
$msg.="your second choice for phthon is '.$python;
.....etc..
I have a web form that updates a customer record. The entries are prefilled with the data stored in mysql when called. One field shows if equipment was returned as follows:
<font size=5>Returned:</font><input type="text" name="ud_Returned" value="<?php echo $rtrnd; ?>" /><br />
My question is I setup to convert this to radio button like:
<font size=5>Returned:</font><input type="radio" name="ud_Returned" value=Yes /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />
But I want it so the value stored in the $rtrnd variable fills in the existing radio button with the appropriate Yes/No for that customer when form is called. Any ideas?
Maybe something like this would do the trick:
<input type="radio" name="ud_Returned" value="Yes" <?php if($rtrnd) echo 'checked'; ?> />
<input type="radio" name="ud_Returned" value="No" <?php if(!$rtrnd) echo 'checked'; ?> />
Assuming that $rtrnd is a boolean value. If it isn't just use a comparison in the if statements like if($rtrnd == 'yes').
Assuming your store this information as 1 or 0 in the database you'll have to -
<?php
// Get stuff from database
$result = /* The resulting array from your query */;
if( $result["ud_Returned"] == 1 )
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" checked="checked" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />');
}
else
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" checked="checked" /> No<br />');
}
I'm not suggesting that this is the exact implementation you should pursue but this is the logic - get information from database, make a comparison, add the checked attribute to the appropriate radio button.
i have a html form that has a more then 30 checkboxes. checkboxes are used to select for different service.
I want to store those all value in 1 Database field seperated by , those could be numeric or string. Code for inserting checkbox values is as follows:
<form method="post" action="posted.php">
Option 1: <input type="checkbox" name="type" value="1" /><br />
Option 2: <input type="checkbox" name="type" value="2" /><br />
Option 3: <input type="checkbox" name="type" value="3" /><br />
Option 4: <input type="checkbox" name="type" value="4" /><br />
Option 5: <input type="checkbox" name="type" value="5" /><br />
Option 6: <input type="checkbox" name="type" value="6" /><br />
Option 7: <input type="checkbox" name="type" value="7" /><br />
Option 8: <input type="checkbox" name="type" value="8" /><br />
Option 9: <input type="checkbox" name="type" value="9"/><br />
<input type="submit" value="Submit" />
</form>
After successful submission off course i need to fetch those data in the corespondent check box while update.
Please response me as soon as possible with complete PHP code.
You can use JSON. From this:
Option 1: <input type="checkbox" name="myval[option_name1]" /><br />
Option N: <input type="checkbox" name="myval[option_nameN]" /><br />
Store:
$model->field = json_encode($_POST['myval']);
Or comma-seporated (if you really want):
$model->field = implode(",", $_POST['myval']);
I currently have a page which dynamically fills in a survey (Questions, Answers as Radio Buttons/Checkboxes) from a MySQL database. The generated HTML looks something like this :
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" name="radio[0]" id="radio[0]" value="Alien" />Alien
<br />
<input type="radio" name="radio[0]" id="radio[1]" value="Hobbit" />Hobbit
<br />
<input type="radio" name="radio[0]" id="radio[2]" value="Tree" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" name="radio[1]" id="radio[3]" value="Camel Collector" />Camel Collector
<br />
<input type="radio" name="radio[1]" id="radio[4]" value="sadasd" />sadasd
<br />
<input type="radio" name="radio[1]" id="radio[5]" value="Voolome" />Voolome
<br />
<input type="radio" name="radio[1]" id="radio[6]" value="31231235" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" name="radio[2]" id="radio[7]" value="Nobody Knows" />Nobody Knows
<br />
<input type="radio" name="radio[2]" id="radio[8]" value="Somebody Knows" />Somebody Knows
<br />
<input type="radio" name="radio[2]" id="radio[9]" value="Who Knows" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" name="radio[3]" id="radio[10]" value="Answer1" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" name="Check4" value="Bike">Answer One<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Two<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="Ans5" />
<br /><br/>
</form>
A few important things to note :
There are 3 types of questions, "Choice" - Single choice(Radio Button); "Multiple" - Multiple Choice(Check box); "Open" - User Input (Text Box).
Each element's name corresponds to the appropriate question number (The number shown next to the question is Question+1 (Since it starts at 0). [For example, Question 14 would have Radio[14] as the name.
My Main Question : How can you submit these fields to be stored into the Database? I am trying to figure out how to write code which will find out which option is selected for each question.
Side Question : Is it also possible to validate these questions to ensure atleast one option is selected for each question? (Checking that textbox!="" is easy, but how would I do this for Radio Button/Checkboxes?)
PHP Code used to generate this form can be provided if needed! It is essentially using one variable to store the question number ($qno), which is used as a counter while looping the statements to pull data from MySQL, Figure out the type of answer, and place the appropriate controls on the form.
Option that is selected , will be in your $_POST array and radio2 instead of radio[2] even if yours works too, or use name radio[] in all of your radio buttons ,you will get array that contains all radio buttons that are selected.
Also , options that are checked should be in an array that is in the same $_POST array
You use a simple name for checkbox,this will only send the last value checked to your php script and will work as radio even if more than one value is checked so:
Instead of name="Check4" it must be name="Check4[]".
And for displaying answers , you can iterate over values of $_POST simply like this :
<?php
if($_POST['submit']) {
foreach($_POST as $key=>$value){
echo "Input name : $key Value:$value";//add condition to exclude your button or hidden fields
}
}
?>
Do something like this:
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" id="radio[0]" value="Alien" name="question1" />Alien
<br />
<input type="radio" id="radio[1]" value="Hobbit" name="question1" />Hobbit
<br />
<input type="radio" id="radio[2]" value="Tree" name="question1" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" id="radio[3]" value="Camel Collector" name="question2" />Camel Collector
<br />
<input type="radio" id="radio[4]" value="sadasd" name="question2" />sadasd
<br />
<input type="radio" id="radio[5]" value="Voolome" name="question2" />Voolome
<br />
<input type="radio" id="radio[6]" value="31231235" name="question2" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" id="radio[7]" value="Nobody Knows" name="question3" />Nobody Knows
<br />
<input type="radio" id="radio[8]" value="Somebody Knows" name="question3" />Somebody Knows
<br />
<input type="radio" id="radio[9]" value="Who Knows" name="question3" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" id="radio[10]" value="Answer1" name="question4" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" value="Bike" name="question5[]">Answer One<br>
<br />
<input type="checkbox" value="Bike" name="question5[]">Answer Two<br>
<br />
<input type="checkbox" value="Bike" name="question5[]">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="question6" />
<br /><br/>
</form>
to validate radio button use this:
if($("#radio:checked").length==0)
{
alert("Please Select atleast one");
return false;
}
take reference of this Building a Simple Quiz
can you please used this code :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
1 . How do you classify yourself?
<br/>
<input type="radio" name="radio[]" id="radio[0]" value="Alien" />Alien
<br />
<input type="radio" name="radio[]" id="radio[1]" value="Hobbit" />Hobbit
<br />
<input type="radio" name="radio[]" id="radio[2]" value="Tree" />Tree
<br /><br/>
2 . Who are you?
<br/>
<input type="radio" name="radio1[]" id="radio[3]" value="Camel Collector" />Camel Collector
<br />
<input type="radio" name="radio1[]" id="radio[4]" value="sadasd" />sadasd
<br />
<input type="radio" name="radio1[]" id="radio[5]" value="Voolome" />Voolome
<br />
<input type="radio" name="radio1[]" id="radio[6]" value="31231235" />31231235
<br />
<br/>
3 . Test Question
<br/>
<input type="radio" name="radio2[]" id="radio[7]" value="Nobody Knows" />Nobody Knows
<br />
<input type="radio" name="radio2[]" id="radio[8]" value="Somebody Knows" />Somebody Knows
<br />
<input type="radio" name="radio2[]" id="radio[9]" value="Who Knows" />Who Knows
<br />
<br/>
4 . Test Question 2
<br/>
<input type="radio" name="radio3[]" id="radio[10]" value="Answer1" />Answer1
<br /><br/>
5 . First Multiple
<br/>
<input type="checkbox" name="Check4" value="Bike">Answer One<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Two<br>
<br />
<input type="checkbox" name="Check4" value="Bike">Answer Three<br>
<br /><br/>
6 . First Open!
<br/>
<input type="text" name="Ans5" />
<br /><br/>
<input type="submit" name="submit">
</form>
</body>
</html>
PHP Code
<?php if($_POST['submit']) {
echo "<pre>";
print_r($_POST);
echo "</pre>";
exit;
}
?>
Output
Array
(
[radio] => Array
(
[0] => Hobbit
)
[radio1] => Array
(
[0] => sadasd
)
[radio2] => Array
(
[0] => Somebody Knows
)
[Ans5] =>
[submit] => Submit Query
)
I have a forum the user fills out with 2 separate radio button categories they can fill out.
Permissions: private <input type="radio" name="permissions" value="private" /> public <input type="radio" name="permissions" value="public"/><br />
Category: default <input type="radio" name="cat" value"default" /> sport <input type="radio" name="cat" value"sport" />school <input type="radio" name="cat" value"school" />geeky <input type="radio" name="cat" value"geeky" />misc <input type="radio" name="cat" value"misc" /> funny <input type="radio" name="cat" value"funny" /><br />
For some reason the cat for category is not showing up in $_POST['cat']; Can you only have 1 set of radio buttons?
You have some mistypings.
value"default" should be value="default". same with that whole second line of code. There's no equal signs (=) between the attributes and the values.
Quick Tip: Don't put all those <input /> tags on one line. They'll still show up on the same line on your webpage, but it'll be easier to edit in your text editor. Speaking of which, if you had a text editor with syntax highlighting, you probably would've caught this problem. I suggest Notepad++ or Geany.
Category:
default <input type="radio" name="cat" value="default" />
sport <input type="radio" name="cat" value="sport" />
school <input type="radio" name="cat" value="school" />
geeky <input type="radio" name="cat" value="geeky" />
misc <input type="radio" name="cat" value="misc" />
funny <input type="radio" name="cat" value="funny" />
Make sure those fields are within the <form></form> tags.