Sorry if this is a really simple question, I am just learning php, but I've been writing a script that can create quizzes, and right now html hates me. When I click a radio button and submit my form the result is always that the button is on, not the value I assign it. Here is the excerpt of my code:
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . ' value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . ' value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . ' value="Checkboxes">Checkboxes <br>';
The html of my page, which is very incomplete, generated by my php is this:
<html>
<body>
<form action="index.php" method="post">
Number of questions:
<input type="number" name="fsize">
<input type="submit">
</form>
<form action="index.php" method="get">
1:
<br>
<input type="radio" name="f0 value="radio">
Radio Buttons
<br>
<input type="radio" name="f0 value="text">
Text area
<br>
<input type="radio" name="f0 value="Checkboxes">
Checkboxes
<br>
1:
<br>
<input type="radio" name="f1 value="radio">
Radio Buttons
<br>
<input type="radio" name="f1 value="text">
Text area
<br>
<input type="radio" name="f1 value="Checkboxes">
Checkboxes
<br>
1:
<br>
<input type="radio" name="f2 value="radio">
Radio Buttons
<br>
<input type="radio" name="f2 value="text">
Text area
<br>
<input type="radio" name="f2 value="Checkboxes">
Checkboxes
<br>
<input type="submit">
\n
</form>
</body>
</html>
I specifically switched to get rather than post so I could see my results, and this is the important part of my url /index.php?f0+value%3D=on&f1+value%3D=on&f2+value%3D=on
Does anyone know how I can get it to submit properly? and if you want to see the full php script here it is:
<html>
<body>
<?php
session_start();
echo '<form action="index.php" method="post"> Number of questions: <input type="number"
name="fsize">';
echo '<input type="submit"></form>';
if (isset($_POST["fsize"]))
{
$_SESSION["fsize"] = $_POST["fsize"];
}
if (isset($_SESSION["fsize"]))
{
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . ' value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . ' value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . ' value="Checkboxes">Checkboxes <br>';
if (isset($_GET["f" . $i]))
{
$_SESSION["f".$i] = $_GET["f".$i];
echo "I exist!";
}
if (isset($_SESSION["f".$i]))
{
echo '<textarea rows="4" cols="50"> What is the question? </textarea>';
switch($_SESSION["f".$i])
{
case("radio"):
echo "you chose a radio button!";
break;
case("text"):
echo "You chose a text area!";
break;
case("Checkboxes"):
echo "You chose checkboxes!";
break;
default:
break;
}
}
else if (isset($_GET["f".$i]))
{
echo '<textarea rows="4" cols="50"> What is the question? </textarea>';
switch($_GET["f".$i])
{
case("radio"):
echo "you chose a radio button!";
break;
case("text"):
echo "You chose a text area!";
break;
case("Checkboxes"):
echo "You chose checkboxes!";
break;
default:
break;
}
}
}
echo '<input type="submit"> </form>';
}
?>
</body>
</html>
I have a few other problems with it too I know, but this is the most infuriating.
Sorry about the length, Hovestar.
You've forgot a closing " in each name attribute of your radio controls:
<input type="radio" name="f0" value="radio">
<!-- ^ this here is important -->
Therefore, your browser thinks this radio's name attribute is "f0 value=" and is missing a value attribute. Therefore it adds the default attribute on.
Try this.
echo '<form action="index.php" method="get">';
for ($i=0; $i<$_SESSION["fsize"];$i++)
{
echo 'Type of Answer for Question ' . $i + 1 . ": <br> ";
echo '<input type="radio" name="f' . $i . '" value="radio">Radio Buttons <br>';
echo '<input type="radio" name="f' . $i . '" value="text">Text area <br>';
echo '<input type="radio" name="f' . $i . '" value="Checkboxes">Checkboxes <br>
if you have more statements make sure to close it properly using ".
This will be the answer from the Captain Obvious School of Web Design.
Are you ready for it? ... Be sure that you actually assign each radio button a value. It took me 2.5 hours of (frustrating and confusing) research to realize that I had neglected to do this. Each button had a unique id assigned which I was expecting to be returned using:
showAnswerWhen = $('input:radio[name="score"]:checked').val();
Which (of course) had no effect on this HTML:
<div class="pull-right score-bar">
Show answers:
<input type="radio" name="score" id="immediately"/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="immediately">Immediately</label>
<input type="radio" name="score" id="when" checked/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="when">When I answer</label>
<input type="radio" name="score" id="end"/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="end">At the end</label>
</div><br/>
showAnswerWhen finally had the correct value (not just 'on') with the following HTML:
<div class="pull-right score-bar">
Show answers:
<input type="radio" name="score" id="immediately" value="immediately"/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="immediately">Immediately</label>
<input type="radio" name="score" id="when" value="when" checked/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="when">When I answer</label>
<input type="radio" name="score" id="end" value="end"/>
<label form="w1 w2 w3 w4 w5 w6 w7 w8 w9 w10" for="end">At the end</label>
</div>
Turns out an id is not a value. So unreasonable! :-)
Related
<h1> Hotel kamer reservering </h1>
<br><br>
<form method="POST">
<input name="radio" type="radio" value="éénpersoonskamer">éénpersoonskamer</input><br><br>
<input name="radio1" type="radio" value="tweepersoonskamer">tweepersoonskamer</input><br><br>
<input name="radio2" type="radio" value="ontbijt">ontbijt</input><br><br><br>
<input name="radio3" type="radio" value="lunch">lunch</input><br><br><br>
<input name="radio4" type="radio" value="diner">diner</input><br><br><br>
<input name="submit" type="submit" <value="Klik"></input>
</form>
<?php
if(isset($_POST['submit']) and ! empty($_POST['submit'])) {
if(isset($_POST['radio']) || ($_POST['radio1']) || ($_POST['radio2']) || ($_POST['radio3']) || ($_POST['radio4']) ) {
$radio = $_POST['radio'] . ' ' . $_POST['radio1'] . ' ' . $_POST['radio2'] . ' ' . $_POST['radio3'] . ' ' . $_POST['radio4'];
echo $radio;
}
}
?>
I am a beginner with PHP, am learning it now but i can't seem to lose the undefined index message. If i choose all then it won't give a bad message. Can someone explain me how i can fix this with this code and how i can resolve it with another code in the future.
I am thankfull for your time.
You're only calling isset() on the $_POST variable for the first radio button. You need to check that all the radio buttons are set. If any of them aren't set, you'll get that warning.
if(isset($_POST['radio'], $_POST['radio1'], $_POST['radio2'], $_POST['radio3'], $_POST['radio4']) ) {
$radio = $_POST['radio'] . ' ' . $_POST['radio1'] . ' ' . $_POST['radio2'] . ' ' . $_POST['radio3'] . ' ' . $_POST['radio4'];
echo $radio;
}
1) Remove typo "<" before "value" in <input name="submit" type="submit" <value="Klik"></input>
2) This is pointless: "empty($_POST['submit']"
3) All radio buttons should have the same name, not "radio1", "radio2" etc., perhaps it would be better to use checkboxes here if you want to be able to select/deselect more than one option
4) The only reason you're getting an error is because you're checking "radio" variable if it exists with isset($_POST['radio']) as for other radio inputs you're only checking if they have any kind of value: ($_POST['radio1']), you should be checking if all of them exist before checking their values :)
A better approach, as I guess you really are looking for a combination of radio- and checkboxes and that you want to output the checked options:
<h1> Hotel kamer reservering </h1>
<br><br>
<form method="POST">
<input name="roomtype" type="radio" checked="checked" value="éénpersoonskamer">éénpersoonskamer <br><br>
<input name="roomtype" type="radio" value="tweepersoonskamer">tweepersoonskamer <br><br>
<input name="food[]" type="checkbox" value="ontbijt">ontbijt <br><br><br>
<input name="food[]" type="checkbox" value="lunch">lunch <br><br><br>
<input name="food[]" type="checkbox" value="diner">diner <br><br><br>
<input name="submit" type="submit">
</form>
<?php
if(!empty($_POST['submit'])) {
echo "roomtype: " . $_POST['roomtype'] . "<br>";
$food = $_POST['food'];
foreach ($food AS $k => $v) {
echo "Food $k = $v<br>";
}
}
?>
New guy here - I have three choices and want to let the user only pass to the next page "4.html" if they select A, else send them to google.com. This is where I've gotten so far :(
if(empty($_POST['choice'])){
echo "Please select at least one choice..!!";
//this should send them to google.com if they select none or the wrong one
}
else{
foreach($_POST['choice'] as $choice){
header('Location: /4.html');
}
}
<form action="multichoice.php" method="post">
<input id="checkbox" type="checkbox" name="choice[]" value="A" />A)Choice A
<input id="checkbox" type="checkbox" name="choice[]" value="B" />B)Choice B
<input id="checkbox" type="checkbox" name="choice[]" value="c" />C)Choice C
<input id="input"onclick="return myFunction()" type="submit" value="Submit"></input>
</form>
I really appreciate your guys' help!
Sidenote: You can't use echo and header together, otherwise you will be outputting before header.
Consult the link following this (footnotes) and is intended to be run inside the same file:
Checkbox method: (which differs from the radio buttons below)
<?php
if(isset($_POST['submit'])){
if(isset($_POST['choice'])){
foreach($_POST['choice'] as $choice){
if($choice == "A"){
echo "You chose A" . "\n";
// header('Location: /4.html');
// exit;
}
if($choice == "B"){
echo "You chose B" . "\n";
}
if($choice == "C"){
echo "You chose C" . "\n";
}
} // brace for foreach
} // brace for if(isset($_POST['choice']))
// else for if(isset($_POST['choice']))
else{
echo "Please make a choice.";
}
} // brace for if(isset($_POST['submit']))
?>
<form action="" method="post">
<input id="checkbox" type="checkbox" name="choice[]" value="A" />A)Choice A
<input id="checkbox" type="checkbox" name="choice[]" value="B" />B)Choice B
<input id="checkbox" type="checkbox" name="choice[]" value="C" />C)Choice C
<input id="input" onclick="return myFunction()" name="submit" type="submit" value="Submit">
</form>
Radio buttons method: (edited) and added a name attribute to the submit button. Sidenote: </input> isn't a valid tag; it's been removed.
<?php
$choice = $_POST['choice'];
if(isset($_POST['submit'])){
if($choice == "A"){
// echo "You chose A" . "\n";
header("Location: /4.html");
exit; // stop further execution
}
if($choice == "B"){
echo "You chose B" . "\n";
}
if($choice == "C"){
echo "You chose C" . "\n";
}
if(empty($_POST['choice'])){
header("Location: http://www.google.com/");
exit; // stop further execution
}
} // submit conditional
?>
<form action="" method="post">
<input id="radio" type="radio" name="choice" value="A" />A)Choice A
<input id="radio" type="radio" name="choice" value="B" />B)Choice B
<input id="radio" type="radio" name="choice" value="C" />C)Choice C
<input id="input" onclick="return myFunction()" name="submit" type="submit" value="Submit">
</form>
Footnotes:
See the following on the subject of outputting before header:
How to fix "Headers already sent" error in PHP
Also, you can use radio buttons for single choices and without the array.
If you're faced with errors/notices/warnings, somewhere down the line:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im wanting to repeat radio buttons using php. Below is the html form
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
Basically, i have 4 groups of questions with rating 1-6 (how would i do it so i dont need to write out all this html code over and over, is there a shorthand method using php so that the selected one also goes into a mysql database too?
Completely confused and new to php, any help would be great.
Look at loop for
//$i => groups (/4)
//$a => radio buttons (/6)
for($i = 1; $i <= 4; $i++){
for($a = 1; $a <= 6; $a++){
echo '<input type="radio" name="RadioGroup'.$i.'" value="'.$a.'">'.$a.'<br/>';
}
echo '<br/><br/>';
}
And the function
function loopMe($group, $answer){
for($i = 1; $i <= $group; $i++){
for($a = 1; $a <= $answer; $a++){
echo '<input type="radio" name="RadioGroup'.$i.'" value="'.$a.'">'.$a.'<br/>';
}
echo '<br/><br/>';
}
}
For use it
loopMe(3, 4);
As others have suggested, the FOR loop is your best bet here however, you can expand a little by adding simplicity and expanding the commands.
$rating = 6; //This sets the highest rating number
for($i=0 $i<$rating; $i++){
//This echos the input by rating, if rating is 6, it'll repeat 6 times.
echo "<input type=radio name=RadioGroup1 value='" . $i . "'>\r\n";
}
Now for the other half of the problem.
When posting information to a database, you need to send it to another page for processing and storage (or the same page if you handle it correctly).
This is a basic way to store THIS radio group (this is in procedural style):
$link = mysqli_connect('localhost', 'username', 'password', 'dbname');
$query = "INSERT INTO myTable (RadioGroup1) VALUES ($_POST['RadioGroup1'])";
mysqli_query($link, $query); //replace link with your database connections variable
Try a FOR loop function...as below:
<?php
for ($x=0; $x<=6; $x++)
{
echo '<input type="radio" name="RadioGroup1" value="'.$x.'">'.$x;
}
?>
To learn more about this function, visit:
http://php.net/manual/en/control-structures.for.php
see this example below... hope this helps you...
page1.php
<html>
<body>
<form id="frmQuestion" method="post" action="page2.php">
<p>Question 1</p>
<input type="radio" name="question1" value="1" checked="checked" />1
<input type="radio" name="question1" value="2" />2
<input type="radio" name="question1" value="3" />3
<input type="radio" name="question1" value="4" />4
<input type="radio" name="question1" value="5" />5
<input type="radio" name="question1" value="6" />6
<br/>
<p>Question 2</p>
<input type="radio" name="question2" value="1" checked="checked" />1
<input type="radio" name="question2" value="2" />2
<input type="radio" name="question2" value="3" />3
<input type="radio" name="question2" value="4" />4
<input type="radio" name="question2" value="5" />5
<input type="radio" name="question2" value="6" />6
<br/>
<?php
for($i=3; $i<=20; $i++)
{
echo "<p>Question " . $i . "</p>";
for ($j=1; $j<=6; $j++)
{
if($j == 1)
{
echo "<input type='radio' name='question". $i ."' value='". $j."' checked='checked' />" . $j;
}
else
{
echo "<input type='radio' name='question". $i ."' value='". $j."' />". $j;
}
}
echo "<br/>";
}
?>
<input type="submit" value="Send" />
</form>
</body>
</htm>
page2.php
<?php
echo "question 1:" . $_POST["question1"] . "<br/>";
echo "question 2:" . $_POST["question2"] . "<br/>";
echo "question 3:" . $_POST["question3"] . "<br/>";
echo "question 4:" . $_POST["question4"] . "<br/>";
// for more.
for ($n = 5; $n<=20; $n++)
{
$question = "question" . $n;
echo "question". $n . ":" . $_POST[$question] . "<br/>";
}
?>
I am getting the value for the single tsid for each record, however the checked radio button value is not returned in the array, I just get 0? Any help is appreciated.
PHP:
// Set the timesheets to set status approved/rejected
// find out how many records there are to update
$size = count($_POST['tsid']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$tsid = intval($_POST['tsid'][$i]);
$personnelid = intval($_POST['personnel'][$i]);
print "TSID: " . $tsid . "<br>";
print "TSuser: " . $personnelid . "<br>";
if ($tsid > 0 && $personnelid > 0) {
// do the update and print out some info just to provide some visual feedback
$query = "Update timesheets set status='1' where id= '$tsid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
}
++$i;
}
mysql_close();
HTML:
<input type="hidden" name="tsid[]" value="<?PHP echo $row['id']; ?>">
<li data-role="fieldcontain">
<a href="tsapprove.php?id=<?PHP echo $row['id']; ?>"><p><?PHP echo $row['personnel']; ?></p>
<p><?PHP echo $row['name']; ?></p>
<p class="ui-li-aside"><strong><?PHP echo $row['totalhrs']; ?> H</strong></p>
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="radio" name="personnel[]" id="1" value="<?PHP echo $row['personnel']; ?>" />
<label for="1">Approve</label>
<input type="radio" name="personnel[]" id="2" value="<?PHP echo $row['personnel']; ?>" />
<label for="2">Reject</label>
</fieldset>
</a>
View Details
</li>
Hopefully you can piece this together:
<?
$size = count($_POST['tsid']);
echo "<pre>";print_r($_POST);echo "</pre>";
$i = 0;
while ($i < $size) {
$tsid = $_POST['tsid'][$i];
$pid = $_POST['personnel_'.$i];
print "TSID: " . $tsid . "<br />";
print "TSuser: " . $pid . "<br />";
$i++;
}
?>
<form method="post">
<!--tsid[0] - $i = 0-->
<input type="hidden" name="tsid[]" value="1" />
<input type="radio" name="personnel_0" value="111" />
<input type="radio" name="personnel_0" value="222" />
<!--tsid[1] - $i = 1-->
<input type="hidden" name="tsid[]" value="2" />
<input type="radio" name="personnel_1" value="333" />
<input type="radio" name="personnel_1" value="444" />
<input type="submit" />
</form>
Basically, the way you are returning personnel doesnt really work. So I simplified this a bit, and basically set the radios for personnel differently. Instead of an array, its setting the actual name with the TSID value.
I hard coded 1 and 2, but you would replace with your $row['id'], as well has the number after "personnel_" would be 0..1..2 etc
Hopefully this helps
edit: few issues with code
I have the following html code:
<form method="post" action="arrayplay.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
And the following PHP script:
//arrayplay.php
foreach ($_POST['todelete'] as $id)
{
echo $id . "<br/>";
}
?>
It is supposed to echo out each element value but instead I get an error. I am getting really frustrated. If I use:
<form method="post" action="arrayplay.php">
<?php
$dbc= //connection
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
echo $row['first_name'];
echo ' ' . $row['last_name'];
echo ' ' . $row['email'];
echo '<br />';
}
mysqli_close($dbc);
?>
<input type="submit" name="submit" value="Remove" />
</form>
It works perfectly fine! Why? The first (hard coded html) holds the exact same value as the one that retrieves them from the database. I am having a real hard time understanding retrieving values from an array with $_POST. Why does name=foo[] create an array? Is it an associative or numeric array? I'm sorry for all of the questions, I'm just really ready to pull my hair out.
if you just named the input foo it would only get one value. because square brackets are commonly used for arrays, foo[] is how in the html form, you indicate an array. of course on the PHP side you just call it foo as you are aware from your working example.
I've tested this and it should work:
<?php
if ($_POST['delete']) {
foreach ($_POST['todelete'] as $id) {
echo $id.' selected<br />';
}
}
?>
<form method="post" action="arrayplay.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
If you're still having troubles, you can try:
<?php
if ($_POST['delete']) {
for ($i = 0; $i < 4; $i++) {
if (isset($_POST['todelete'][$i])) {
echo $_POST['todelete'][$i].' selected<br />';
}
}
}
?>