<form action="" method="post">
<?php
$i=0;
while(i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
}
?>
<input type="submit" name="btnsubmit"/>
</form>
if(isset($_POST['btnsubmit']))
{
$i=0;
while($i<4)
{
echo $i;
$chek=$_POST['chkApprove_'.$i];// Error Undefined Index
$i++;
}
}
Error is displayed as Undefined index: chkApprove_0...chkApprove_3. Am i doing something wrong here.
$ was missing in you while loop before "i". and $i was not increamenting.
<?php
$i=0;
while($i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
?>
try this : you start from $i=0 but not increment $i++.
<form action="" method="post">
<?php
$i=0;
while($i < 4)
{
?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
?>
<input type="submit" name="btnsubmit"/>
</form>
<?php
if(isset($_POST['btnsubmit']))
{
$i=0;
while($i < 4) {
echo $i;
$chek=$_POST['chkApprove_'.$i];// Error Undefined Index
$i++;
}
}
?>
well you have a syntax error and you forget to increment $i in this loop:
$i=0;
while(i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
}
should be
$i=0;
while($i<4){ ?>
<input type="checkbox" name="<?php echo 'chkApprove_'.$i; ?>"/>
<?php
$i++;
}
Because you don't increment $i in the intial loop, but in the latter looop you do, you get the undefined index
Your corrected code:
<form action="" method="post">
<?php
$i=0;
while($i<4){
?>
<input type="checkbox" name="<?php echo 'chkApprove_' . $i; ?>"/>
<?php
$i++;
}
?>
<input type="submit" name="btnsubmit"/>
</form>
<?php
if (isset($_POST['btnsubmit'])) {
$i = 0;
while ($i < 4) {
echo $i;
$chek = $_POST['chkApprove_' . $i];
// Error Undefined Index
$i++;
}
}
Related
I am trying to create a form where I need to allow user to select different values (from select tag) against some labels.
I have two different array in php.
the standard array that contains the labels for select.
a set of values to be selected against the labels.
My problem is when I press the submit button, the form is submitted but $_POST does not show any value selected by the select tag. I want to get the selected values against the labels.
here is my code:
<?php
$data = array ('name', 'phone', 'address');
$values = array('a','2344','xyz');
?>
<html>
<head></head>
<body>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<?php for($i = 0; $i < count($data); $i++){ ?>
<label for='<?php $data[$i]?>'> <?php echo $data[$i]?></label>
<select name='<?php $data[$i]?>' id = '<?php $data[$i]?>'>
<?php foreach($values as $val){ ?>
<option value='<?php $val ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
<br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
echo$_POST['name'];
}
?>
When I press the submit button the error I get is "Notice: Undefined index: name". I have extensively searched in the questions already posted about multiple select statements but none of the answers matched my criteria. Thanks for the help.
You are forgot to print variables
<html>
<head></head>
<body>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<?php for($i = 0; $i < count($data); $i++){ ?>
<label for='<?php echo $data[$i]?>'> <?php echo $data[$i]?></label>
<select name='<?php echo $data[$i]?>' id = '<?php echo $data[$i]?>'>
<?php foreach($values as $val){ ?>
<option value='<?php echo $val ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
<br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
echo$_POST['name'];
}
?>
You have not written echo before each variable in selectbox:
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<?php for($i = 0; $i < count($data); $i++) { ?>
<label for='<?php echo $data[$i]; ?>'> <?php echo $data[$i]?></label>
<select name='<?php echo $data[$i]; ?>' id = '<?php echo $data[$i]; ?>'>
<?php foreach($values as $val){ ?>
<option value='<?php echo $val; ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
</form>
i want to make a multiple choice that if we choose wrong answer, then a text 'incorrect' will be displayed in side of the answer that we choose and if we choose right answer, then a text 'correct' will be displayed in side of the answer that we choose. i using radio button. please help.
this is my index.php:
<?php
include 'koneksi.php';
session_start();
$query = mysql_query("select * from t_soal") or die (mysql_error());
//$_SESSION['soal'] = mysql_fetch_array($query);
$_SESSION['soal'] = array();
$_SESSION['no'] = 1;
$_SESSION['score'] = 0;
$_SESSION['option'] = array();
$_SESSION['jawab'] = array();
$i=0;
while($row = mysql_fetch_assoc($query)){
$_SESSION['soal'][] = $row;
$_SESSION['option'][] = array($_SESSION['soal'][$i]['a'], $_SESSION['soal'][$i]['b'], $_SESSION['soal'][$i]['c'], $_SESSION['soal'][$i]['d']);
$i++;
}
if(isset($_SESSION['soal'])){
header("location:test.php");
}
this is test.php:
<?php
session_start();
$soal = $_SESSION['soal'];
$no = $_SESSION['no'];
if(isset($_POST['next'])){
$_SESSION['jawab'][] = $_POST['option'];
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
}
if(isset($soal[$no-1])){
?>
<!DOCTYPE html>
<html>
<head>
<title>Latihan Soal</title>
</head>
<body>
Kembali ke soal 1
<form action="" method="POST">
<p>
<?php
echo $no.". "; $_SESSION['no']++;
echo $soal[$no-1]['soal'];
$jawaban = $_SESSION['option'][$no-1];
shuffle($jawaban);
?>
</p>
<?php
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
?>
<input type="submit" name="next" value="next">
</form>
</body>
</html>
<?php
}else{
header("location:result.php");
}
?>
Assuming all your script is working already, then:
In PHP:
You will need to assign a new parameter on this script:
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
to
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
$_SESSION['correctAnswer'] = $soal[$no-2]['kunci']; // add sthing like this
}
and then, from
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
to
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
echo $jawaban[$i] == $_SESSION['correctAnswer'] ? " correct" : " incorrect";
}
In JS: (much easier)
HTML files will looks like this:
<div class="correctAnswer">
<input type="radio" name="theanswer" id="choice1" />
<label for="choice1">Choice 1</label>
</div>
<div class="incorrectAnswer">
<input type="radio" name="theanswer" id="choice2" />
<label for="choice2">Choice 2</label>
</div>
and the JS will looks like this:
$(".correctAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> correct</span>");
});
$(".incorrectAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> incorrect</span>");
});
If you want a working example, please check this fiddle link here.
Hope it helps !
My Code I want to single checked. but It is multiple check . How can i single selection in this types of situation ?
This is my code
<?php
$i=5;
for($i=1; $i<6; $i++)
{
?>
<form name="form" id="myform" class="frmclass">
<input type="radio" name="rdo" value="<?php echo $i; ?>"> <?php echo $i; ?> <br>
</form>
<?php }
?>
You have created 5 seperate forms. Place the <form> and </form> tags outside the loop
<?php
$i=5;
?>
<form name="form" id="myform" class="frmclass">
<?php
for($i=1; $i<6; $i++) {
{
?>
<input type="radio" name="rdo" value="<?php echo $i; ?>"> <?php echo $i; ?> <br>
<?php
}
?>
</form>
Just place your form outside of loop. like below..
<form name="form" id="myform" class="frmclass">
<?php
$i=5;
for($i=1; $i<6; $i++)
{
?>
<input type="radio" name="rdo" value="<?php echo $i; ?>"> <?php echo $i; ?> <br>
<?php
}
?>
</form>
I have following code (it's piece of bigger code):
<?php
include_once 'init/init.funcs.php';
$x = $_SESSION['answering']['index'];
echo $_SESSION['answering']['questions'][$x-1];
$result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused
where kysimus= "' .$_SESSION['answering']['questions'][$x] . '"');
$question_id = mysql_result($result4, 0);
$result5 = mysql_query('SELECT * from katse_valik_vastused
where kysimus_id="'. $question_id . '"');
if($result5 === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
//foreach($options as $option=>$option_value) {
//echo $option_value;
$count=count($options);
?>
<html>
<br>
<form method="post" action="answering.php">
<input type="radio" name="1"><?php echo $options[0]?><br>
<input type="radio" name="2"><?php echo $options[1]?><br>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
Right now there are two fixed radio buttons. But I want it to have as many buttons, as many elements are in array "options" and each of them to have a value of one element written next to it. How could I do it?
Use a for loop for this: http://www.php.net/manual/en/control-structures.for.php
for ($i = 1; $i < count($options); $i++) {
?>
<input type="radio" name="<?php echo $i; ?>"><?php echo $options[$i]?><br>
<?php
}
Try like this:
<?php
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
?>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach($options as $option=>$option_value) {
?>
<input type="radio" name="<?= $option; ?>"><?php echo $option_value?><br>
<?php }?>
<input name= "submit" type="submit" value="Vasta">
</form>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach ($options as $index=>$option) {
echo "<input type='radio' name='{$index}'>{$option}<br>";
}
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
Try using the below code.
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach ($options as $key => $value) {
?>
<input type="radio" name="<?php echo $key; ?>"><?php echo $options[$key] ?><br>
<?php
}
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
you can do this using for each, like this:
<form method="post" action="answering.php">
<?php foreach ($options as $key => $value): ?>
<input type="radio" name="<?php echo $key; ?>" /><?php echo $value; ?><br />
<?php endforeach; ?>
<input name= "submit" type="submit" value="Vasta">
</form>
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);
}