Php: on submit echo td fields and ad new td - php

I have the following code:
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$count]['0']) ? $_POST['field'][$count]['0'] : '';
$val1 = isset($_POST['field'][$count]['1']) ? $_POST['field'][$count]['1'] : '';
$val2 = isset($_POST['field'][$count]['2']) ? $_POST['field'][$count]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $count; ?>][0]" value="<?php $val0; ?>"/></td>
<td><input name="field[<?php echo $count; ?>][1]" value="<?php $val1; ?>"/></td>
<td><input name="field[<?php echo $count; ?>][2]" value="<?php $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />
</form>
The problem is when I press submit it add 3 other fields but it clears the other fields. How can I keep the content from the fields but make them uneditable?

You are not echoing your values and your $count should be $i, as you'll end up with same field names
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$i]['0']) ? $_POST['field'][$i]['0'] : '';
$val1 = isset($_POST['field'][$i]['1']) ? $_POST['field'][$i]['1'] : '';
$val2 = isset($_POST['field'][$i]['2']) ? $_POST['field'][$i]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $i; ?>][0]" value="<?php echo $val0; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][1]" value="<?php echo $val1; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][2]" value="<?php echo $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />

for($j=0;$j<3;$j++){
echo '<input type="hidden" name="field['.$i.'][0]" value="'.$_POST[field][$i][0].'" />';
}

Related

Input Fibonacci in table generate PHP

Hello i have the following script to generate a table
<?php error_reporting(null); ?>
<form action="" method="POST" autocomplete="off">
Row<br>
<input type="text" value="<?php echo $_POST['row'] ?>" name="row"><br/>
Kolom<br>
<input type="text" value="<?php echo $_POST['kolom'] ?>" name="kolom">
<input type="hidden" name="aksi" value="gen">
<br/><br/>
<input type="submit" value="Submit">
</form>
<?php
if($_POST['aksi']=="gen"){
echo "<table border=1>";
for ($i=1; $i<=$_POST['row']; $i++) { ?>
<tr>
<?PHP for ($y=1; $y<=$_POST['kolom']; $y++) { ?>
<td>Data</td>
<?php } ?>
</tr>
<?php }
echo "</table>";
}
?>
And i have a script to generate a fibonacci number
<?php
$first=0;
$second=1;
echo "$first $second";
for ($i=0; $i<10; $i++)
{
$third = $second + $first;
echo " $third";
$first = $second;
$second = $third;
}
?>
I want to join the 2 scripts.
when i a generate a table, Fibonacci needs to be placed inside the table, i don't know how to solve this.
This Result like this
This should do the trick. Cannot test so if there are any typos keep them :)
<?php error_reporting(null); ?>
<form action="" method="POST" autocomplete="off">
Row<br>
<input type="text" value="<?php echo $_POST['row'] ?>" name="row"><br/>
Kolom<br>
<input type="text" value="<?php echo $_POST['kolom'] ?>" name="kolom">
<input type="hidden" name="aksi" value="gen">
<br/><br/>
<input type="submit" value="Submit">
</form>
<?php
if($_POST['aksi']=="gen"){
$first=0;
$second=1;
?>
<table border=1>
<?php
for ($i=0; $i<$_POST['row']; $i++) {
?>
<tr>
<?PHP
for ($y=0; $y<$_POST['kolom']; $y++) {
/* first two should be 0 and 1*/
if($y < 2 && $i == 0) {
?>
<td>0</td>
<td>1</td>
<?php
$y += 2;
} else {
$third = $second + $first;
?>
<td><?php echo($third); ?></td>
<?php
$first = $second;
$second = $third;
}
}
?>
</tr>
<?php
}
}
?>
</table>

Show each array values in input box if they are set

If i had enter all value in say four input boxes with name prod[] and when i click on the submit button, i want to show each values without loosing it if the array values are isset.
<?php for($i=1; $i<=8; $i++) { ?> <tr>
<td style="width:50% !important;">
<input type="text" class="form-control" name="PprodName[]" />
</td>
#QaisarSatti
thanks bro but i think i just got the logic. here is my code :
<?php
$j = 0;
for($i = 1; $i<=8; $i++) {
?>
<tr>
<td style="width:50% !important;">
<input type="text" class="form-control" id="Ppr<?php echo $i; ?>" name="PprodName[]" value="<?php echo isset( $_POST['PprodName'][$j] ) ? $_POST['PprodName'][$j] : ''; ?>" />
</td>
</tr>
<?php $j++; } ?>
mark this checked if you found it useful. thanks !
$name = $_POST['PprodName'];
foreach( $name as $v ) {
echo $v;
}

How to Keep Radio Button Value after Next/Prev page?

## Keep Radio Button Value after Next/Prev page .. ##
This is for Student Online Exam
if(!isset($_GET['start'])){
$_GET['start'] = "undefined";
}
$start=$_GET['start']; /*To take care global variable if OFF*/
if(!($start > 0)) { /*This variable is set to zero for the first page*/
$start = 0;
}
$startpos = ($start -0);
$limit = 1;
// No of records to be shown per page.
$currentpos = $startpos + $limit;
$back = $startpos - $limit;
$next = $startpos + $limit;
$countquery=" SELECT * FROM quiz ";
$countresult=mysql_query($countquery);
echo mysql_error();
$totalrecords=mysql_num_rows($countresult);
//$quiz_table = mysql_query("select * from quiz Limit 1");
?>
<form method="POST" action="" onclick="">
<table>
<?php
$quiz_table=" SELECT * FROM quiz ORDER BY qid limit $startpos, $limit ";
$result=mysql_query($quiz_table);
echo mysql_error();
while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo "$row[qid]"; ?></td>
<td><?php echo "$row[Question]"; ?></td>
</tr>
<tr>
<td><input type="radio" name="a" id="a" value="a"/> <?php echo "$row[opt1]"; ?></td>
<td><input type="radio" name="a" id="a" value="b"/> <?php echo "$row[opt2]"; ?></td>
<td><input type="radio" name="a" id="a" value="c"/> <?php echo "$row[opt3]"; ?></td>
<td><input type="radio" name="a" id="a" value="d"/> <?php echo "$row[opt4]"; ?></td>
</tr>
<tr>
<td><!--<input type="submit" name="smt" value="check"/>-->
<?php
if($back >=0) {
print "<a href='onlineexam.php?start=$back'><font face='Verdana' size='2'><input type='button' name='prev' value='PREV'></font></a>";
}
if($currentpos < $totalrecords) {
## Keep Radio Button Value after Next/Prev page .. ##
print "<a href='onlineexam.php?start=$next'><font face='Verdana' size='2'><input type='button' name='nxt' value='NEXT'></font></a>";
}
?></td>
<td><input type="submit" name="smt" value="Finish"/></td>
</tr>
<?php
}
$quiz_table="SELECT * from quiz";
$result=mysql_query($quiz_table);
while ($row = mysql_fetch_array($result))
{
$id=$row['qid'];
$ques=$row['Question'];
$op1=$row['opt1'];
$op2=$row['opt2'];
$op3=$row['opt3'];
$op4=$row['opt4'];
$crt=$row['woptcode'];
if(isset($_POST['smt'])){
$ans= $_POST['a'];
if(isset($_POST['a'])){
if($crt == $ans ){
$mark=0;
$mark=$mark+1;
mysql_query("INSERT INTO stuans(qid,Question,opt1,opt2,opt3,opt4,woptcode,stuanswer,mark)VALUES('$id','$ques','$op1','$op2','$op3', '$op4','$crt','$ans','$mark')");
} else{
$mark=0;
mysql_query("INSERT INTO stuans(qid,Question,opt1,opt2,opt3,opt4,woptcode,stuanswer,mark)VALUES('$id','$ques','$op1','$op2','$op3', '$op4','$crt','$ans','$mark')");
}
}
}
}
..THANKS..
You can use the checked attribute.
<input type="radio" checked>
So if you had some variables (booleans), $opt1, $opt2, $opt3, $opt4, then you could do something similar to this:
<input type="radio" name="opt1" <?php if ($opt1) echo "checked"; ?> />
<input type="radio" name="opt2" <?php if ($opt2) echo "checked"; ?> />
<input type="radio" name="opt3" <?php if ($opt3) echo "checked"; ?> />
<input type="radio" name="opt4" <?php if ($opt4) echo "checked"; ?> />

How to disable individual checkbox?

I want to display array values followed by a checkbox inside a html table using a for loop :
<?php
for ($j = 0; $j < $data['user']['droit']['cnt']; $j++)
{
?>
<table>
<tr>
<td><?php echo $data['user']['droit'][$j]['menu_titre']; ?></td>
<?php
$checked = "";
if (is_array($data['user_droit']) && count($data['user_droit']) > 0 && in_array($data['user']['droit'][$j]['menu_code'], $data['user_droit']))
$checked = " checked ";
?>
<td><input type="checkbox" name="<?php echo $data['user']['droit'][$j]['menu_code']; ?>"
value="<?php echo $data['user']['droit'][$j]['menu_code']; ?>" <?php echo $checked ?> />
</td>
</tr>
</table>
<?php
}
?>
I don't know how to make the individual checkbox to be disabled if it is checked from the loop. How to achieve that ?
or set $checked = " checked disabled ";
<?php
for ($j = 0; $j < $data['user']['droit']['cnt']; $j++)
{
?>
<table>
<tr>
<td><?php echo $data['user']['droit'][$j]['menu_titre']; ?></td>
<?php
$checked = "";
if (is_array($data['user_droit']) && count($data['user_droit']) > 0 && in_array($data['user']['droit'][$j]['menu_code'], $data['user_droit']))
{
$checked = " checked disabled ";
}
?>
<td><input type="checkbox" name="<?php echo $data['user']['droit'][$j]['menu_code']; ?>"
value="<?php echo $data['user']['droit'][$j]['menu_code']; ?>" <?php echo $checked ?> />
</td>
</tr>
</table>
<?php
}
?>

On submit echo td dropdowns and ad new td

I have the following code:
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$i]['0']) ? $_POST['field'][$i]['0'] : '';
$val1 = isset($_POST['field'][$i]['1']) ? $_POST['field'][$i]['1'] : '';
$val2 = isset($_POST['field'][$i]['2']) ? $_POST['field'][$i]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $i; ?>][0]" value="<?php echo $val0; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][1]" value="<?php echo $val1; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][2]" value="<?php echo $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />
How can I make the fields into a dropdowns and when you press submit echo out the dropdown as text instead as a dropdown?
First: Fill in the following:
Make this to action="mytestpage.php"
Give this a name attribue like: name="send"
Give these fields types like: type="Text"
If you want a dropdown menu use <select> in combination with <option>...
Here is a start to learn how:
http://www.echoecho.com/htmlforms11.htm
To print your result do this:
if(isset($_POST['send'])
{
print($_POST['youroptionname']);
}
I hope it helped you.

Categories