how to echo input text in For{} - php

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>

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>

form checkbox value php

I have a form created by a while loop in php like this :
<form action='#' method='post'>
<input type='hidden' name='form_valid' id='form_valid'>
<?php
$i=-1;
$result_array = array();
//the while is from a simple mysql query
while( $line = $results->fetch() )
{
$i++;
echo"<input type='checkbox' class='checkbox' value='".$line->nid."' id='".$i."'>";
echo $line->title;
echo'<br>';
$result_array[$i] = $line->nid;
}
<input type='submit'>
?>
</form>
Then later on the code I'd like to store the values of the checked checkboxes only in a new array :
if (isset($_REQUEST['form_valid'])) //checking is form is submitted
{
foreach($result_array as $result)
{
if($result == $_REQUEST[$j]) <<<< ERROR
{
$final_array[$j] = $result;
}
}
}
Surprisingly, this code does not work at all.
The error message is "Notice : Undefined offset: 0", then offset 1 then 2 etc ...
The line where the message says theres an error is the marked one.
I really have no idea how to do this. Someone ? =)
Don't try to do it this way, this just makes it hard to process, just use a grouping name array: name="checkboxes[<?php echo $i; ?>]", then on the submission, all values that are checked should simply go to $_POST['checkboxes']. Here's the idea:
<!-- form -->
<form action="" method="POST">
<?php while($line = $results->fetch()): ?>
<input type="checkbox" class="checkbox" name="nid[<?php echo $line->nid; ?>]" value="<?php echo $line->nid; ?>" /><?php echo $line->title; ?><br/>
<?php endwhile; ?>
<input type="submit" name="submit" />
PHP that will process the form:
if(isset($_POST['submit'], $_POST['nid'])) {
$ids = $_POST['nid']; // all the selected ids are in here
// the rest of stuff that you want to do with it
}

Checkbox array inside foreach form. Associate each checkbox with another variable

For the below example everything works as expected when ALL the checkboxes are checked. The problem occurs when one or more (but NOT all of them) are checked.
<form action="someaction" method="post">
<?php foreach ($fields as $field) { ?>
<input type="checkbox" name="checkpid[]" value="<?php echo $field['pid']; ?>">
<input type="hidden" name="checkprice[]" value="<?php echo $field['price']; ?>">
<input type="submit" name="submit" value="Submit">
<?php } ?>
</form>
<?php if (isset($_POST['checkpid'])) { ?>
<?php
$checkpid = $_POST['checkpid'];
$checkprice = $_POST['checkprice'];
?>
<?php foreach ($checkpid as $key => $checkpid) { ?>
<?php
$eachpid[] = $checkpid.",".$checkprice[$key];
?>
<?php } ?>
<?php print_r($eachpid), ?> // the $checkpid is always as expected, but the $checkprice does not match its row.
<?php } ?>
With my little knowledge I suspect it is something wrong in the declaration of the $key, but I am overwhelmed.
this is all i can suggest.
instead of adding two input how about adding the two data in value of a checkbox input separated with |. then when submitted just explode the value and receive array 1 for id and 1 for price.
<?php
if (isset($_POST['checkp'])) {
$checkp = $_POST['checkp'];
foreach ($checkp as $check) {
$c = explode("|", $check);
$eachpid[] = $c[0].",".$c[1];
}
print_r($eachpid);
}
?>
<form action="" method="post">
<?php foreach ($fields as $field) { ?>
<input type="checkbox" name="checkp[]" value="<?php echo $field['pid']; ?>|<?php echo $field['price']; ?>">
<?php } ?>
<input type="submit" name="submit" value="Submit">
</form>

PHP Multiple form in same page

I'm doing form in php but I have some problem.
First I will have 3 different form in the same page.
What I want is only 1 form appear and then with the answer a second form will appear and so on.
The answer of the form will be display on the same page.
For now my first form work and after get the answer go to the 2nd form but I want to submit the 2nd form the problem appear.
It delete the answer of my first form and don't do anything (everything start like I am in my first form).
I try to find the problem but can't have idea about how to solve it.
Here is my code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1?
<input type="number" name="nbtemplate" min="1" max="30">
<input type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php
if(!isset($submitbutton1)) {
if (!empty($_POST['nbtemplate']) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for ($i = 1; $i <= $Nnbtemplate; $i++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
if(isset($submitbutton1) && !isset($submitbutton2)) {
if (!empty($_POST['nbtime']) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST['nbtime'];
for ($j = 1; $j <= $nbtime; $j++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time" name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
}
}
?>
That is some gnarly code you got there, brother. This is a really simple task when handled with some javascript. Then you can have a single post to your php. I like using the jQuery framework so here's a couple links I found quickly: this one and this one
Example code in response to comment about building form elements dynamically:
<html>
<head>
<!-- load jquery library -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<form action="toyourpage.php">
How Many?:
<input type="text" name="number" id="number">
<div id="add"></div>
</form>
<!-- javascript go -->
<script type="text/javascript">
$(document).ready(function()
{
$('input#number').keyup(function()
{
var num = $(this).val(); // get num
if(!isNaN(num)) // check if number
{
$('div#add').html(''); // empty
for(i = 1; i <= num; i++) // add
{
$('div#add').append('New Field ' + i + ': <input type="text" name="next_' + i + '" id="next' + i + '"><br>');
}
}
else
{
alert('Valid number required');
}
});
});
</script>
</body>
</html>
I did some changes on Your code, and have tested, it works.
You had any mistakes in your {} brackets and if conditions. Also as I commented I added extract($_POST).
<?php
extract ( $_POST );
if (! isset ( $submitbutton1 ) && !isset($submitbutton2)) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1? <input type="number" name="nbtemplate" min="1" max="30"> <input
type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php ;
}
if (isset ( $submitbutton1 )) {
if (! empty ( $_POST ['nbtemplate'] ) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST ['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or
die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for($i = 1; $i <= $Nnbtemplate; $i ++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
}
}
if ( isset ( $submitbutton2 )) {
if (! empty ( $_POST ['nbtime'] ) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST ['nbtime'];
for($j = 1; $j <= $nbtime; $j ++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time"
name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
?>

when perfoming a multiple row updates, how do i access other form element values, apart from the checkbox?

i wrote a piece of code to do multiple row deletions based on ticked check boxes.
Now i need to mordify it to do multiple row updates.
i have been failing to access the textbox values for each row.
here's my code, in general.
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<input type="checkbox" <?php echo 'name="SaveEachComment['.$row["comment"].']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment" size="55" value="<?php echo $comment;?>" />
<input type="submit" name="SaveComments" value="submit" />
</form>
I just added a for loop to print multiple form fields. Obviously your iteration would be as many as number of rows, so you can change that part of the code. But try this:
<?php
if (isset($_POST["SaveComments"]) && isset($_POST["SaveEachComment"]))
{
while(list($key, $val) = each($_POST["SaveEachComment"]))
{
if($val == 'commentbox')
{
echo $_POST['rowcomment'][$key] . "<br />\n";
// do the update here. $val contains the ID
}
}
}
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php for ($i=0; $i<11; $i++) { ?>
<input type="checkbox" <?php echo 'name="SaveEachComment['.$i.']" ';?> value="commentbox" id="commentbox" />
<input type="text" name="rowcomment[<? echo $i?>]" size="55" value="<?php echo $comment;?>" />
<br />
<?php } ?>
<input type="submit" name="SaveComments" value="submit" />
</form>

Categories