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>
Related
I write below code and put an html input text in for :
for($i=0;$i<5;$i++)
echo '<input type="text" name="subject">';
but when i want to echo the that value I type in to the above text box
it returns "" nothing
echo $_POST['subject'];
I have
<form name="form1" method="post" action="index.php">
Tag top of this codes ?
Sorry if I had Mistake
Make the input as array and read the values in PHP using loop like below.
HTML code :
<form name='myForm' action='index.php' method="post">
<?php
for {$i=i;$i<5;$i++} {
echo "<input type='text' name='subject[]'>";
}
?>
</form>
PHP Code :
$subjectArray = $_POST['subject'];
foreach($subjectArray as $key => $val) {
if($val != "") {
echo $key."==".$val."<br>";
}
}
It's bad manner to echo your HTML code. It can become annoyingly hard for someone to read the code after you (for exemple, in a company)
I would suggest doing this:
for($i=0;$i<5;$i++) {
?><input type="text" name="subject"><?php
}
If you want to input back in the form what was just typed, then you're doing too much.
Is your form in index.php too?! If so, action="index.php" become action=""
• With your array:
$subjectArray= array();
// Gather data from $_POST if it's the subject form
if(!empty($_POST) aa isset $_POST['subjectForm']) {
// Remove the hidden input from the list
unset($_POST['subjectForm']);
// Add the remaining entry to $subjectArray
foreach($_POST as $key => $value) {
$subjectArray[$key]= $value;
}
}
<form name="myForm" action="" method="post">
<!-- hidden input to find our form after validation -->
<input type="hidden" name="subjectForm" value=1>
<?php
for($i=1; $i<=5; $i++) {
?><input type="text" name="subject<?= $i ?>" value="<?php if(isset(subjectArray['subject'.$i])) { echo $_POST['subjectArray'.$i]; } ?>"><?php
}
?>
</form>
• Without your array:
<form name="myForm" action="" method="post">
<?php
for($i=1; $i<=5; $i++) {
?><input type="text" name="subject<?= $i ?>" value="<?php if(isset($_POST['subject'.$i])) { echo $_POST['subjectArray'.$i]; } ?>"><?php
}
?>
</form>
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>
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>
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);
}
<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++;
}
}