<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>";
}
}
?>
Related
I have a form in html with several fields, one of which is a radio button.When I submit the form into a php page I want to display the contents of each field and the name of the radio button selection. However the radio button doesn't display the name property but echoes "on" when selected and nothing when not, what can I do to show the name of the selection?
this is the html page
<html>
<body>
<form action="confirmdata.php" method="post">
Name <input type="text" name="name"><br><br>
Gender <input type="radio" name="gender" value="Male">Male<input type="radio" name="gender" value="Female">Female<br><br>
Email <input type="text" name="email"><br><br>
Inform email <input type="checkbox" name="inform"><br><br>
<input type="submit">
</form>
</body>
</html>
and this is the php page
<html>
<body>
<?php
foreach($_POST as $data){
echo $data; ?>
<br>
<?php
}
?>
Return to registration form
</body>
</html>
I'm guessing that, maybe this might help you to figure it out:
$html = '';
foreach ($_POST as $data) {
$html .= '<input type="radio" name="gender" value="' . $data . '"> ' . $data . '<br>';
}
echo $html;
It seems you have your form values in the $data. When you loop through it, it will fill the values back in the input radio elements that you have. You might also need to add the keys to name attributes:
$html = '';
foreach ($_POST as $key => $data) {
$html .= '<input type="radio" name="' . $key . '" value="' . $data . '"> ' . $data . '<br>';
}
echo $html;
I think you want display the selected values after submit the form. so $_POST['gender'] is holding the value either 'Male' or 'Female', so after submit the form add condition to check $gender is male or female then use checked attribute inside a radio button tag.
Please follow the below simple steps.
$gender = $_POST['gender'];
if ($gender == 'Male') {
?>
<input type="radio" name="gender" value="Male" checked>Male
<?php
}
else {
?> <input type="radio" name="gender" value="Female" checked>Female<br><br>
<?php
}
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.
Hello i'm at linking my form data to my mysql database server so far so good i have a little problems here
my prcoes.php. code :
$db_selected = mysql_select_db (DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' / mysql_error());
}
$value = $_POST['name'];
$value2 = $_POST['surname'];
$value3 = $_POST['email'];
$value4 = $_POST['phone'];
$value4 = $_POST['activity'];
$value5 = $_POST['ltype'];
With checkboxes $_POST['ltype'];i get only Array on Mysql as result ?
I get the right values with this code by sending form data to my email :
'LType : ' . implode(',', $_POST['ltype']). "\n" .
Any help will be welcomed , thanks in advance
Update here my chekbox code :
<div class="thumb1" >
<label for="word" ><img class="img" src="images/my1.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="word" value="word" /><hr> <p><strong>Word Mark Logo</strong></p>
</div>
<div class="thumb1" >
<label for="letter"><img class="img" src="images/my2.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="letter" value="letter" /><hr> <p><strong>Letter Mark Logo</strong></p>
</div>
<div class="thumb1">
<label for="emblerm"><img class="img" src="images/my3.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="emblerm" value="emblerm" /><hr> <p><strong>Emblerm Logo</strong></p>
</div>
Why don't you:
$value5 = implode(',', $_POST['ltype']);
if you want the array back just explode the database value.
Depends on how you define the checkbox if
<form action='XXX.php' method='POST'>
Football: <input type="checkbox" name="sports[]" value="football" />
Baseball: <input type="checkbox" name="sports[]" value="baseball" />
</form>
And on the XXX.php it return as array
if ( $_POST['sports'] ) {
$arySports = $_POST['sports'];
foreach( $arySports AS $value ) {
echo $value ."<br>";
}
}
If you did
Subscribe Now <input type="checkbox" name="subscribe" />
And on the XXX.php it return as on
If you did
Subscribe Now <input type="checkbox" name="subscribe" value="now" />
Then it return string "now". So try with this and see what you want for your checkbox
<form action="XXX.php" method="POST">
<input type='checkbox' name='A'/>
<input type='checkbox' name='B' value='this is array'/>
<input type='checkbox' name='B' value='the second one'/>
<input type='checkbox' name='C[]' value='A'/>
<input type='checkbox' name='C[]' value='B'/>
<input type='checkbox' name='C[]' value='C'/>
<input type='checkbox' name='D' value='string'/>
</form>
on the XXX.php do
print_r( $_POST['A'] );
echo "<br>";
print_r( $_POST['B'] );
echo "<br>";
print_r( $_POST['C'] );
echo "<br>";
print_r( $_POST['D'] );
echo "<br>";
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! :-)
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 />';
}
}
}
?>