How to post checkbox items to the next page? - php

I'm new to PHP. I have a doubt about posting values of checkbox items to another page as an array. Can anyone tell me a solution for this.
<form method="get">
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" value="Go!" />
</form>
and the php code is
<?php
$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++)
{
echo "Selected " . $checked[$i] . "<br/>";
}
?>

Before doing php operation, check whether form is submitted and options[] value is set. If it is then iterate through array as you have [] as name.
Try this:
<?php
if (isset($_REQUEST['submit']) && $_REQUEST['submit'] !== null) {
if (isset($_REQUEST['options'])) {
$options = $_REQUEST['options'];
$str = '';
for ($i = 0; $i < count($options); $i++) {
$str .= "Selected " . $options[$i] . "<br/>";
}
echo $str;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
Testing
</title>
<script src="js/jquery.min.js"></script>
</head>
<body>
<form method="get" name="form">
<input type="checkbox" name="options[]" value="Politics"/>
Politics
<br/>
<input type="checkbox" name="options[]" value="Movies"/>
Movies
<br/>
<input type="checkbox" name="options[]" value="World "/>
World
<br/>
<input type="submit" name="submit" value="Go"/>
</form>
<script>
</script>
</body>
</html>

the action/answer can also be on different page.(as i have done)
Index Page:
<!DOCTYPE html>
<html>
<head>
<title>
Testing
</title>
<script src="js/jquery.min.js"></script>
</head>
<body>
<form method="get" action="display.php">
<input type="checkbox" name="options[]" value="Politics"/>
Politics
<br/>
<input type="checkbox" name="options[]" value="Movies"/>
Movies
<br/>
<input type="checkbox" name="options[]" value="World "/>
World
<br/>
<input type="submit" name="submit" value="Go"/>
</form>
<script>
</script>
</body>
</html>
Display Page:
<?php
if (isset($_REQUEST['submit']) && $_REQUEST['submit'] !== null) {
if (isset($_REQUEST['options'])) {
$options = $_REQUEST['options'];
$str = '';
for ($i = 0; $i < count($options); $i++) {
$str .= "Selected " . $options[$i] . "<br/>";
}
echo $str;
}
}
?>

<form method="post">//changed get to post
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" value="Go!" />
</form>
<?php
$checked = $_POST['options'];//changed get to post
for($i=0; $i < count($checked); $i++)
{
echo "Selected " . $checked[$i] . "<br/>";
}
?>

Please add action attribute in your form tag for redirecting on another page then post your following code on that page.
<?php
$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++)
{
echo "Selected " . $checked[$i] ;
}
?>

Related

Multiple Checkboxes

Found this on the internet. How can I put this output into an email?
Normally I use
$name = $_POST['name'];
$email_body = "$name";
I would like the output of this code into my $email_body.
Is this posible?
<?php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any buildings.");
}
else
{
$N = count($aDoor);
echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++)
{
echo($aDoor[$i] . " ");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<form action="test.php" method="post">
Which buildings do you want access to?<br />
<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
<input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br />
<input type="checkbox" name="formDoor[]" value="D" />Drake Commons<br />
<input type="checkbox" name="formDoor[]" value="E" />Elliot House
<input type="submit" name="formSubmit" value="Submit" />
</form>
</body>
$name = $_POST['name'];
$email_body ='';
<?php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
$email_body = "You didn't select any buildings.";
}
else
{
$N = count($aDoor);
$email_body .= "You selected $N door(s): ";
for($i=0; $i < $N; $i++)
{
$email_body .= $aDoor[$i] . " ";
}
}
?>

PHP get first four item in array on first loop then get next four item on second loop?

//First file
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<div>
<form action="questions.php" method="post">
Quiz Name: <input type="text" name="quizname" /><br/><br/>
Number of M.C.Q questions: <input type="number" name="mcq" min="0" /><br/><br/>
Number of True/False questions: <input type="number" name="truefalse" min="0" /><br/><br/>
<input type="submit" name="submit" value="Start Generating Questions" />
</form>
</div>
</body>
</html>
//Second file
<?php
if(isset($_POST['submit'])){
$quizName = $_POST['quizname'];
$mcq = $_POST['mcq'];
$truefalse = $_POST['truefalse'];
echo '<form action="finalquestions.php" method="post">';
for($i=1;$i<=$mcq;$i++){
echo 'Question:'.$i.' <input type="text" name="question1[]" /> </br></br>';
echo '<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>';
echo '</br></br>';
}
for($i=1;$i<=$truefalse;$i++){
echo 'Question:'.$i.' <input type="text" name="question2[]" /> </br></br>';
echo '<input type="radio[]" name="question2">True</br>
<input type="radio[]" name="question1">False</br>';
echo '</br></br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>
//Third file
<?php
if(isset($_POST['submit'])){
//array of input elements named "question1"
$question1 = $_POST['question1'];
//array of input elements named "radiooptions"
$radiooptions = $_POST['radiooptions'];
//get the length of those array
$question1length = count($question1);
$radiooptionslength = count($radiooptions);
//loop through array to get first input with four radiooptions inputs
for($x = 0; $x < $question1length; $x++) {
echo $question1[$x];
echo "<br>";
echo "<br>";
for($i = 0; $i < $radiooptionslength; $i++) {
echo $radiooptions[$i];
echo "<br>";
echo "<br>";
}
}
guys in first loop of the array [question1] I want to get first input in array of [question1] and in second loop I want to get the first 4 inputs of array [radiooptions] and when the loop gets second time I want it to give the second input of array [question1] and in the [radiooptions] array loop I want it to give me the next 4 inputs for example next 4 inputs will be 5,6,7,8.....But its giving me different [question] inputs but same first 4 item of array [radiooptions]
You would need to do something like the following using a simple pagination function on an array.
<?php
$array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
for($i = 1; $i < 5; $i++){
$utility = new Utility();
$utility->getItems($i, $array);
echo '<br/>';
}
class Utility{
public function getItems($index, $array, $count = 4){
$min = ($index - 1) * $count;
$max = $index * $count;
for($k = $min; $k < $max; $k++){
echo $array[$k] . ' ';
}
}
}
?>

PHP - Loop number of selected check boxes on form submit

I have an issue where I need to loop the number of check boxes on a form submit. Foreach check box that is looped I need to then insert data into the database.
How Would I go about looping over the amount of check boxes that have being passed via form submit?
My code is as follows:
Form:
<form action="createChallenge.php" method="post" name="chalCreate">
Challenge Name:<input type="text" name="chalName" />
<br />
Challenge Target:<input type="text" name="chalTarget"/>
<br />
End Date:<input type="text" name="chalDate">
<br />
<!-- Needs a jquery datepicker -->
Select Friends: <br />
<?php
$selFriend = $conn->prepare("SELECT * FROM Friends WHERE UserID = '$userID' AND Friend = 'y' ORDER BY FriendName ASC");
$selFriend->execute();
foreach($selFriend as $row){
?>
<input type="checkbox" name="test" value="<?php echo $row['FriendID'] ?>"><?php echo $row['FriendName'] ?><br>
<?php
}
?>
<br />
<button type="submit">Create Challenge</button>
</form>
PHP to handle the form:
<?php
if(isset($_POST['test']))
{
$i = 0;
foreach($_POST['test'] as $checked)
{
echo $friend = $checked;
$i++;
}
echo $name = $_POST['chalName'];
echo $target = $_POST['chalTarget'];
echo $date = $_POST['chalDate'];
echo $friend = $_POST['test'];
echo $setby = $_COOKIE['userID'];
$create = $conn->prepare("INSERT INTO Challenge ( chalSetBy, chalName, chalTarget, chalDate ) VALUES ('$setby', '$name', '$target', '$date') ");
$create->execute();
if($create)
{
echo "Challenge made successfully";
}
else
{
echo "There was a problem";
}
}
?>
I thought doing the following would echo out data, but it didn't, it only selected the last check box:
$i = 0;
foreach($_POST['test'] as $checked)
{
echo $friend = $checked;
$i++;
}
Make an array of your checkbox in HTML page like as below,
<form name="frm" method="post">
<input type="checkbox" value="1" name="test[]">
<input type="checkbox" value="2" name="test[]">
<input type="checkbox" value="3" name="test[]">
<input type="checkbox" value="4" name="test[]">
<input type="checkbox" value="5" name="test[]">
<input type="checkbox" value="6" name="test[]">
<input type="submit">
</form>
<?php
foreach($_POST['test'] as $key=>$value)
{
echo $value."<br>";
}

get array of checkbox values from post

Here is my code
<?php
if(isset($_POST['type'])){
if (is_array($_POST['type'])) {
echo "IS ARRAY!!!!!!!!";
}
else {
echo "IS NOT ARRAY!!!";
}
}
?>
and..
<div id="player" class="group">
<form action=<?php echo $_SERVER['SCRIPT_NAME']; ?> id="playerform" method="post">
<?php
for($j = 0; $j < sizeof($_SESSION['playercharacter']->defendAgainst); $j++) {
?>
<input type="checkbox" name="type[]" value=<?php echo $_SESSION['playercharacter']->
defendAgainst[$j]; ?> />
<?php
}
?>
</form>
</div>
Thing is....$_POST['type'] is just a single value rather than an array.. How do i get ALL checked values? Thanks for your time...
I hope it will help you
<form action="" method="post" >
<?php
for($i=0;$i<10;$i++){
echo '<input type="checkbox" name="type[]" value="'.$i.'">'.$i.'<br/>';
}
?>
<input type="submit" name="submit" >
</form>
if(isset($_POST['submit'])){
$arr=array();
foreach($_POST['type'] as $key=>$value)
{
$arr[$key]=$value;
}
var_dump($arr);
}

In PHP how to output the contents of a radiobox array using POST?

I want to output the values of the checkbox in the same way I'm outputting the values from the text fields. Since the checkbox can have multiple inputs I'm using an array for that but trying to cycle through the array for values hasn't worked for me.
tried foreach($type as $item) and echoing $item within the HTML like it says in the PHP book I have but that hasn't worked.
How should I do and where should the code be? I'm also unable to use PHP within the HTML for some reason, I'm not sure why that is or if its something to do with the echo<<<_END or not. Help appreciated.
<?php // formtest.php
if (isset($_POST['game'])) $game = $_POST['game'];
else $game = "(Not entered)";
if (isset($_POST['genre'])) $genre = $_POST['genre'];
else $genre = "(Not entered)";
if (isset($_POST['type'])) $type = $_POST['type'];
else $type = "(Not entered)";
echo <<<_END
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your game is: $game in the $genre genre and of the type<br />
<form method="post" action="formtest.php">
What is your game?
<input type="text" name="game" />
<br />
What is your genre?
<input type="text" name="genre" />
<br />
Type?
Retail <input type="checkbox" name="type[]" value="Retail" />
Downloadable <input type="checkbox" name="type[]" value="Downloadable" />
Free <input type="checkbox" name="type[]" value="Free" />
<br />
<input type="submit" />
</form>
</body>
</html>
_END;
?>
With the form as it is now, $_POST['type'] will be an array since it's using checkboxes (and named appropriately), not radios. Here I just implode it for display, but you can loop through it like any array. It should be worth noting that any time you're wondering what a form is giving you, you can var_dump($_POST) or var_dump($_GET) depending on where the data is coming from. It helps a lot with debugging.
Here's what I got, I switched from heredoc, but your heredoc should work fine if you add $type back in somewhere, I didn't notice it in the original code:
<?php // formtest.php
if (isset($_POST['game'])) $game = $_POST['game'];
else $game = "(Not entered)";
if (isset($_POST['genre'])) $genre = $_POST['genre']; //Edit: Fixed line, oops
else $genre = "(Not entered)";
if (isset($_POST['type'])) $type = implode(', ',$_POST['type']);
else $type = "(Not entered)";
//Normally I'd specify a charset, but for simplicity's sake I won't here.
$type = htmlspecialchars($type);
$game = htmlspecialchars($game);
$genre = htmlspecialchars($genre);
?>
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your game is: <?php echo $game; ?> in
the <?php echo $genre; ?> genre and of the type <?php echo $type; ?><br />
<form method="post" action="">
What is your game?
<input type="text" name="game" />
<br />
What is your genre?
<input type="text" name="genre" />
<br />
Type?
Retail <input type="checkbox" name="type[]" value="Retail" />
Downloadable <input type="checkbox" name="type[]" value="Downloadable" />
Free <input type="checkbox" name="type[]" value="Free" />
<br />
<input type="submit" />
</form>
</body>
</html>
Addendum:
If you switched and used radios like
<input type="radio" name="type" value="Downloadable" />
$_POST['type'] would be a simple string since you can only select one of the set.
To the file you post it the type[] will be saved as an array. For example
$a=$_POST['type'];
Although I don't find any point in doing this to radio-buttons, because their purpose is to pass only 1 value(unless you want specifically to).
Ok, first you don't need to echo the entire html output. Second your questions says radio buttons, but the html shows checkboxes. A radio field will only produce one result so you don't need [] after then name. Checkboxes will return an array when named with a []. So if you are using checkboxes you will need to process the result as an array. If you change the field to radio it should work fine.
<?php // formtest.php
if (isset($_POST['game'])) {
$game = $_POST['game'];
}
else { $game = "(Not entered)"; }
if (isset($_POST['genre'])) {
$genre = $_POST['genre'];
}
else { $genre = "(Not entered)"; }
if (isset($_POST['type'])) {
$type = $_POST['type'];
}
else { $type = "(Not entered)"; }
?>
<html>
<head>
<title>Form Test</title>
</head>
<body>
Your game is: <?php echo $game; ?> in the <?php echo $genre; ?> genre and of the type <?php echo $type; ?><br />
<form method="post" action="test.php">
What is your game?
<input type="text" name="game" <?php if ($game != "(Not entered)") { echo "value='" . $game . "'"; } ?> />
<br />
What is your genre?
<input type="text" name="genre" <?php if ($genre != "(Not entered)") { echo "value='" . $genre . "'"; } ?> />
<br />
Type?
Retail <input type="radio" name="type" value="Retail" <?php if ($type == "Retail") { echo "checked"; } ?> />
Downloadable <input type="radio" name="type" value="Downloadable" <?php if ($type == "Downloadable") { echo "checked"; } ?> />
Free <input type="radio" name="type" value="Free" <?php if ($type == "Free") { echo "checked"; } ?> />
<br />
<input type="submit" />
</form>
</body>
</html>

Categories