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
}
Related
I have a form with multiple checkboxes and hidden inputs, which I'm passing to a second page using GET.
I'm then trying to retrieve the value of each checkbox and the input in a loop and echo out the combined value.
HTML:
<form action="criteria.php" method="GET">
<input name="id[]" type="hidden" value="<? echo $criteria_id; ?>" />
<input type="checkbox" name="checked[]" class="checkbox-md" id="<? echo $criteria_id; ?>" value="Y">
<button type="submit" class="btn btn-lilac" role="button">Complete</button>
</form>
PHP:
$criteria_id = $_GET['id']; //get all criteria id
$criteria_checked = $_GET['checked']; //get checked criteria id
foreach($criteria_id as $id) //get id of all checkboxes {
echo "<BR>Criteria = ".$id."Checked = ".$criteria_checked; //returns id + array?
if ($checked='Y')//check if checked {
echo "<BR>Criteria =".$id." Checked = Y";
} else {
echo "<BR>Criteria =".$id." Checked = N";
}
}
You will need to make sure that the inputs have matching array keys:
<input name="id[0]" type="hidden" . . .
<input name="checked[0]" type="checkbox" . . .
<input name="id[1]" type="hidden" . . .
<input name="checked[1]" type="checkbox" . . .
Depending on how you create these you could use the $criteria_id:
<input name="id[<? echo $criteria_id; ?>]" type="hidden" . . .
<input name="checked[<? echo $criteria_id; ?>]" type="checkbox" . . .
This way the id and checked array keys will match. All hidden inputs will be passed from the form but only the checked checkboxes, so check if the key of the id is set in the checked array:
foreach($_GET['id'] as $key => $id) {
if (isset($_GET['checked'][$key])) {
echo "<BR>Criteria =".$id." Checked = Y";
} else {
echo "<BR>Criteria =".$id." Checked = N";
}
}
<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>";
}
}
?>
I have a list a checkboxes with accompanying input text fields. If the user checks a box, the accompanying text field will be add to an array.
I am new to PHP and was wondering if anyone can help me in the right direction.
Should I use a for loop, foreach, while, unique "name" for each input, or something else?
Below is what I have so far.
<?php
if(isset($_POST['submit'])){
$array = array();
while(isset($_POST['check'])){
if(!isset($_POST[$some_text]) || empty($_POST[$some_text])){
echo "Please include your text for each checkbox you selected.";
exit();
}
$array[] = $_POST['some_text];
}
}
?>
<form>
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<!-- I might have around 100 of these -->
<!-- submit button here -->
</form>
You first need a way to associate your checkboxes with their corresponding text fields, and a way to tell them apart. For example:
<form>
<input type="checkbox" name="check[]" value="1"><input type="text name="text1">
<input type="checkbox" name="check[]" value="2"><input type="text name="text2">
<input type="checkbox" name="check[]" value="3"><input type="text name="text3">
</form>
Now you can loop through it as follows:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['check']) && is_array($_POST['check']) && !empty($_POST['check'])) {
$array = array();
foreach ($_POST['check'] as $value) {
$text_field = 'text' . $value;
if (!isset($_POST[$text_field]) || trim($_POST[$text_field]) == '') {
echo "Please include your text for each checkbox you selected.";
exit;
}
$array[] = $_POST[$text_field];
}
}
}
Try this:
$select = array();
foreach($_POST['check'] as $key => $selected){
$select[$key] = $selected;
}
Please try this:
<?php
if(isset($_POST['submit'])){
for($i=0; $i<100; $i++)
{
if($_POST['check_'.$i])
{
$array[] = $_POST['input_'.$i];
}
}
}
?>
#################### HTML
<form method="post" action="">
<?php for($i=0; $i<100; $i++) { ?>
<input type="checkbox" name="check_<?php echo $i ?>"><input type="text" name="input_<?php echo $i ?>">
<?php } ?>
<input type="submit" name="submit">
</form>
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 />';
}
}
}
?>
I want two radio buttons on a webpage (written in php) representing "yes" and "no". When I load the page I want it to fetch a value from a mysql db and set the corresponding radio button. And when I click on the other button, I want it to update the database and reload the page.
I'm trying to do this with a simple html form, but no luck. The code I have so far (that is not working at all :( is:
if (!isset($_POST['submit'])) {
$sql = "SELECT challenge_me FROM contestants WHERE id=$id";
$res = (mysql_fetch_assoc(mysql_query($sql, $db)));
$challenge_me = $res["challenge_me"];
}else{
$sql = "UPDATE contestants SET challenge_me='" . $_POST['YesNo'] . "' WHERE id='$id'";
if(!mysql_query($sql, $db))
echo mysql_error(), "<br/>Query '$sql'";
$challenge_me = $_POST['YesNo'];
}
echo'<form method="post" action="' . $PHP_SELF . '">';
echo '<input type="hidden" name="submit" value="submit">';
if($challenge_me == 1){
echo'<input type="radio" name="YesNo" value="1" onClick="this.form.submit();" checked>Yes ';
echo'<input type="radio" name="YesNo" value="0" onClick="this.form.submit();">No ';
}else{
echo'<input type="radio" name="YesNo" value="1" onClick="this.form.submit();">Yes ';
echo'<input type="radio" name="YesNo" value="0" onClick="this.form.submit();" checked>No ';
}
echo'</form>';
Your script doesnt seem to define $id, where does $id get its value from? That could be the source of your problem. Your script might not be passing any value in $id