Why Multiple check Radio button . I want To Single Checked in from - php

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>

Related

how to show validation error message for array element?

I have a form like this:
<form action="" method="post">
<input type="hidden" name="ndetails" value="<?php echo $ndetails; ?>"/>
<?php
for ($i=1; $i<=$ndetails; $i++)
{
?>
<textarea name="mydetails[]"></textarea>
<?php echo form_error('mydetails[]'); ?>
<?php
}
?>
</form>
and in the controller, I use a form validation like this:
for ($i=1; $i<=$this->input->post('ndetails'); $i++)
{
$this->form_validation->set_rules('mydetails[]', 'Day '.$i, 'trim|required');
}
the problem is when there's more than 1 textarea, the form_error('mydetails[]') only show the last error message. how can I show the errors individually after each textarea?
Use this : Code not tested
<form action="" method="post">
<input type="hidden" name="ndetails" value="<?php echo $ndetails; ?>"/>
<?php
for ($i=1; $i<=$ndetails; $i++)
{
$j=$i-1;
echo "<textarea name='mydetails[".$j."]'></textarea>";
echo form_error('mydetails['.$j.']');
}
?>
</form>

Multiple selects are not being submitted in the form using post?

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>

How to use a PHP variable which is obtained after a loop in HTML value?

This is my code :
NON WORKING CODE
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php json_encode($myarr); ?>
</form>
</div>
<?php
$myarr = array("foo");
for ($i = 1; $i <= 10; $i++) {
array_push($myarr,$i);
print "<div class='mylist'>$i</div>";
}
?>
</div>
WORKING CODE
<div class="container">
<?php
$myarr = array("foo");
for ($i = 1; $i <= 10; $i++) {
array_push($myarr,$i);
print "<div class='mylist'>$i</div>";
}
?>
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php json_encode($myarr); ?>
</form>
</div>
</div>
Now, this is more or less the situation.I cannot alter the position of the div tags as my whole website layout is based on it.How can I make sure the first one works because that ordering of the div tags keeps my web design and everything intact.
NOTE: Please don't suggest me to use jQuery or Javascript.I do not wish to use them unless ther is no other way out. :(
Thanks
With out executing of foreach you can't get that variable for encode.
<?php
$myarr = array("foo");
for ($i = 1; $i <= 10; $i++) {
array_push($myarr,$i);
}
?>
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php echo json_encode($myarr); ?>
</form></div>
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php echo json_encode($myarr); ?>
</form></div>
for ($i = 1; $i <= 10; $i++) {
print "<div class='mylist'>$i</div>";
}
</div>
<?php
$myarr = array("foo");
<div class="myform">
<form action="blah.php" method="post">
<input type="hidden" value="<?php json_encode($myarr); ?>
</form>
</div>
for ($i = 1; $i <= 10; $i++) {
array_push($myarr,$i);
print "<div class='mylist'>$i</div>";
}
</div>
?>
Try this maybe. Hope I understood you...

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);
}

value not recognized in php

<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++;
}
}

Categories