I have checkbox inside looping like this.
<?php
for($i=0;$i<$jumlah;$i++) {
echo "<tr>
<td align='center'><input type='text' name='PROSES[]' placeholder='Input Proses $i'/></td>
<td align='center'><input type='checkbox' name='NOTIF[]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='REMINDER[]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='DECISION[]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='AUTHOR[]' value='Yes'/></td>
<td align='center'><input type='text' name='SIAPA[]' size='40' placeholder='dari siap ke siapa' /></td>
</tr>";
}//end for
echo "</table>";
?>
and the query to input like this
if(isset($_POST['SUBMIT'])) {
$proses = $_POST[PROSES];//value from selectbox
$jumlah = count($proses);
$max_id = $data_max_id[ID];
for($i=0;$i<$jumlah;$i++) {
$query_input = "INSERT INTO data_proses SET ID_input = '$max_id',
proses = '$proses[$i]',
notifikasi = '$notif[$i]',
reminder = '$reminder[$i]',
decision = '$decision[$i]',
authorization = '$author[$i]',
dari_siapa = '$siapa[$i]'";
$hasil_input = mysqli_query($mysqli, $query_input);
}//end for
}
it will Produce table like this
In Short,
After I have submit to mysql is successfull But stil wrong. IF I have Check like that image the database have wrong result like this
input :
[x][x][ ][ ]
[ ][x][x][ ]
[ ][ ][x][x]
On database :
[x][x][x][x]
[ ][x][x][ ]
[ ][ ][ ][ ]
Can anyone help to fix my problem?
Thanks in advance
Include the row number in the names of all the checkboxes. Only the checked boxes get submitted, and PHP will index them all from 0 if they don't have an explicit index -- you won't see empty values for the checkboxes that are skipped.
for($i=0;$i<$jumlah;$i++) {
echo "<tr>
<td align='center'><input type='text' name='PROSES[]' placeholder='Input Proses $i'/></td>
<td align='center'><input type='checkbox' name='NOTIF[$i]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='REMINDER[$i]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='DECISION[$i]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='AUTHOR[$i]' value='Yes'/></td>
<td align='center'><input type='text' name='SIAPA[]' size='40' placeholder='dari siap ke siapa' /></td>
</tr>";
}//end for
Related
I am stuck with this code, trying to post 2 different radio group my problem is I want to make the user to check 1 radio button for ervery 4 results , I mean how can I know when user has choose other, with this code I always can choose more than one radio , any help?
$sql = "SELECT * FROM tbl1 WHERE object = '".$obj."' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql);
echo "<form action='' method='post'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "<div align='center'>
<table class='demo' dir='rtl'>
<tbody>
<tr>
<td>".$row2['ojs']."</td>
<td> </td>
</tr>
<tr>
<td>".$row2['txt1']."</td>
<td><input type='radio' name='chk1' value='1' >
</tr>
<tr>
<td>".$row2['txt2']."</td>
<td><input type='radio' name='chk2' value='2' >
</tr>
<tr>
<td>".$row2['txt3']."</td>
<td><input type='radio' name='chk3' value='3' >
</tr>
<tr>
<td>".$row2['txt4']."</td>
<td><input type='radio' name='chk4' value='4' >
</tr>
</tbody>
</table><br>
</div>
";
}
echo "<input type='submit' name='submit'>
</form>";
if (isset($_POST['submit'])) {
$chk1 = $_POST['chk1'];
$chk2 = $_POST['chk2'];
$chk3 = $_POST['chk3'];
$chk4 = $_POST['chk4'];
echo $chk4."|".$chk3."|".$chk2."|".$chk1;
//insert into mysql , just the Selected radio button for each different question
$insQry = "insert into tbl2 (id,sel1,sel2,sel3,sel4) VALUES('$chk1','$chk2','$chk3','$chk4')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
You need to set the name of the entire adio input group to one name. Since you have them set to four different names, it treats them as as four different entities.
You need to give them the same name then get it in PHP like
$sql = "SELECT * FROM tbl1 WHERE object = '".$obj."' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql);
echo "<form action='' method='post'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "<div align='center'>
<table class='demo' dir='rtl'>
<tbody>
<tr>
<td>".$row2['ojs']."</td>
<td> </td>
</tr>
<tr>
<td>".$row2['txt1']."</td>
<td><input type='radio' name='chk' value='1' >
</tr>
<tr>
<td>".$row2['txt2']."</td>
<td><input type='radio' name='chk' value='2' >
</tr>
<tr>
<td>".$row2['txt3']."</td>
<td><input type='radio' name='chk' value='3' >
</tr>
<tr>
<td>".$row2['txt4']."</td>
<td><input type='radio' name='chk' value='4' >
</tr>
</tbody>
</table><br>
</div>
";
}
echo "<input type='submit' name='submit'>
</form>";
if (isset($_POST['submit'])) {
$chk = $_POST['chk'];
echo $chk;
Then simply set the adjacent one in the database to selected number and others to 0
iam working with question page , every user will show him 2 or more questions each question have 4 answers and 4 (radio button) , every question must be separated from other questions , which basically means ever radio button group must be like so , my problem is like the image bellow (i can't post them all )
i have use this code (wasn't work)
$sql2 = "SELECT * FROM questions WHERE subject = 'web lang' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql2);
echo "<form action='' method='POST'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "<div align='center'>
<table>
<thead>
<tr >
<th >Q & A</th>
<th>Choose</th>
</tr>
</thead>
<tbody>
<tr>
<td>".$row2['question']."</td>
<td></td>
</tr>
<tr>
<td>".$row2['ans1']."</td>
<td><input type='radio' name='chk' value='1' >
</tr>
<tr>
<td>".$row2['ans2']."</td>
<td><input type='radio' name='chk' value='2' >
</tr>
<tr>
<td>".$row2['ans3']."</td>
<td><input type='radio' name='chk' value='3' >
</tr>
<tr>
<td>".$row2['ans4']."</td>
<td><input type='radio' name='chk' value='4' >
</tr>
</tbody>
</table><br> <br>
</div>
";
}
echo "<input type='submit' name='submit'>
</form>";
if (isset($_POST['submit'])) {
$chk = $_POST['chk'];
Echo $chk;
}
even if i echo like this
print_r(array_values($chk));
and if i use different name each time i cant get the name , i use this code inside while loop , its ok works but how to get value of other tables ?
$new = 0;
<input type='radio' name='chk".$new."' value='some value' >
$new++;
Try this:
$sql2 = "SELECT * FROM questions WHERE subject = 'web lang' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql2);
echo "<form action='' method='POST'>";
$i=0;
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "<div align='center'>
<table>
<thead>
<tr >
<th >Q & A</th>
<th>Choose</th>
</tr>
</thead>
<tbody>
<tr>
<td>".$row2['question']."</td>
<td></td>
</tr>
<tr>
<td>".$row2['ans1']."</td>
<td><input type='radio' name='chk".$i."' value='1' >
</tr>
<tr>
<td>".$row2['ans2']."</td>
<td><input type='radio' name='chk".$i."' value='2' >
</tr>
<tr>
<td>".$row2['ans3']."</td>
<td><input type='radio' name='chk".$i."' value='3' >
</tr>
<tr>
<td>".$row2['ans4']."</td>
<td><input type='radio' name='chk".$i."' value='4' >
</tr>
</tbody>
</table><br> <br>
</div>
";
$i++;
}
echo "<input type='submit' name='submit'>
</form>";
if (isset($_POST['submit'])) {
$chk = $_POST['chk'];
Echo $chk;
}
Assuming the fact the you have a questionID column associated with each question, change your while loop in the following way,
// your code
echo "<form action='' method='POST'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
?>
<div align='center'>
<table>
<thead>
<tr >
<th >Q & A</th>
<th>Choose</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row2['question']; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo $row2['ans1']; ?></td>
<td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='1' >
</tr>
<tr>
<td><?php echo $row2['ans2']; ?></td>
<td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='2' >
</tr>
<tr>
<td><?php echo $row2['ans3']; ?></td>
<td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='3' >
</tr>
<tr>
<td><?php echo $row2['ans4']; ?></td>
<td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='4' >
</tr>
</tbody>
</table>
<br> <br>
</div>
<?php
}
echo "<input type='submit' name='submit'>
</form>";
// your code
Later when the user selects the radio options and hit submit button, fetch the question IDs and the corresponding (given) answer in the following way,
if (isset($_POST['submit'])) {
foreach($_POST['chk'] as $questionID => $answer){
// $questionID is the question ID and $answer is
// the corresponding given answer of that quesrtion
}
}
I have checkbox inside looping like this.
<?php
for($i=0;$i<$jumlah;$i++) {
echo "<tr>
<td align='center'><input type='text' name='PROSES[]' placeholder='Input Proses $i'/></td>
<td align='center'><input type='checkbox' name='NOTIF[]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='REMINDER[]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='DECISION[]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='AUTHOR[]' value='Yes'/></td>
<td align='center'><input type='text' name='SIAPA[]' size='40' placeholder='dari siap ke siapa' /></td>
</tr>";
}//end for
echo "</table>";
?>
and the query to input like this
if(isset($_POST['SUBMIT'])) {
$proses = $_POST[PROSES];//value from selectbox
$jumlah = count($proses);
$max_id = $data_max_id[ID];
for($i=0;$i<$jumlah;$i++) {
$query_input = "INSERT INTO data_proses SET ID_input = '$max_id',
proses = '$proses[$i]',
notifikasi = '$notif[$i]',
reminder = '$reminder[$i]',
decision = '$decision[$i]',
authorization = '$author[$i]',
dari_siapa = '$siapa[$i]'";
$hasil_input = mysqli_query($mysqli, $query_input);
}//end for
}
it will Produce table like this
In Short,
After I have submit to mysql is successfull But stil wrong. IF I have Check like that image the database have wrong result like this
input :
[x][x][ ][ ]
[ ][x][x][ ]
[ ][ ][x][x]
On database :
[x][x][x][x]
[ ][x][x][ ]
[ ][ ][ ][ ]
Can anyone help to fix my problem?
Thanks in advance
Include the row number in the names of all the checkboxes. Only the checked boxes get submitted, and PHP will index them all from 0 if they don't have an explicit index -- you won't see empty values for the checkboxes that are skipped.
for($i=0;$i<$jumlah;$i++) {
echo "<tr>
<td align='center'><input type='text' name='PROSES[]' placeholder='Input Proses $i'/></td>
<td align='center'><input type='checkbox' name='NOTIF[$i]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='REMINDER[$i]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='DECISION[$i]' value='Yes'/></td>
<td align='center'> <input type='checkbox' name='AUTHOR[$i]' value='Yes'/></td>
<td align='center'><input type='text' name='SIAPA[]' size='40' placeholder='dari siap ke siapa' /></td>
</tr>";
}//end for
I am trying to make a web application that stores and retrieves race information. I am creating the text inputs dynamically by using a php function and index looping.
Here is the code:
<?php
function table_head()
{
if ( func_num_args() > 0 )
{
$args = func_get_args();
foreach($args as $value)
{
echo "<th>" . $value . "</th>";
}
}
}
function display_fields()
{
$i = 0;
$a = 19;
$name="hi";
while($i++ < $a)
{
array_push($numbers,$i);
if($i==1)
{
$name = "time";
} else if($i > 1 && $i < 19){
$name = "lap" + strval($i);
} else {
$name = "avgspd";
}
echo "<td><input type='text' size='7' name='" . $name . "'/></td>";
}
}
echo "<form action='' method='POST'>";
echo "<table><tr>";
table_head("Time","lap1","lap2","lap3","lap4","lap5","lap6","lap7","lap8","lap9","lap10","lap11","lap12","lap13","lap14","lap15","lap16","lap17","Avg Spd");
echo "</tr><tr>";
display_fields();
echo "</tr></table><br><input type='submit'/></form>";
?>
Why is it that the name of the textfield is called "1","2" etc. instead of "lap1","lap2"?
Is there a way how I can fix this? If so how?
This is what the browser (Safari 8.0.7 OS X version) shows for the source code:
<form action='' method='POST'>
<table>
<tr>
<th>Time</th>
<th>lap1</th>
<th>lap2</th>
<th>lap3</th>
<th>lap4</th>
<th>lap5</th>
<th>lap6</th>
<th>lap7</th>
<th>lap8</th>
<th>lap9</th>
<th>lap10</th>
<th>lap11</th>
<th>lap12</th>
<th>lap13</th>
<th>lap14</th>
<th>lap15</th>
<th>lap16</th>
<th>lap17</th>
<th>Avg Spd</th>
</tr>
<tr>
<td><input type='text' size='7' name='time'/></td>
<td><input type='text' size='7' name='2'/></td>
<td><input type='text' size='7' name='3'/></td>
<td><input type='text' size='7' name='4'/></td>
<td><input type='text' size='7' name='5'/></td>
<td><input type='text' size='7' name='6'/></td>
<td><input type='text' size='7' name='7'/></td>
<td><input type='text' size='7' name='8'/></td>
<td><input type='text' size='7' name='9'/></td>
<td><input type='text' size='7' name='10'/></td>
<td><input type='text' size='7' name='11'/></td>
<td><input type='text' size='7' name='12'/></td>
<td><input type='text' size='7' name='13'/></td>
<td><input type='text' size='7' name='14'/></td>
<td><input type='text' size='7' name='15'/></td>
<td><input type='text' size='7' name='16'/></td>
<td><input type='text' size='7' name='17'/></td>
<td><input type='text' size='7' name='18'/></td>
<td><input type='text' size='7' name='avgspd'/>
</td>
</tr>
</table><br><input type='submit'/>
</form>
Note: This code I found via safari's web inspector was all on one line, I formatted it like this to make the code easier for you to inspect.
It should be
$name = "lap" . strval($i);
PHP uses . to concatenate strings, not +.
im new in php! i currently display data from MySQL every time i search for name or idnumber and display all in to the html table but when i click the button on the table row i get the last value of the data.
here is the code i made:
$searchdata=mysql_query("select * from tblstudent where Last_name
='".$search."'")or die("Error Query");
if(isset($_POST['Search'])){
if(empty($search)){
$error_2="Search Box is empty";
}
elseif(strlen($search)<1){
$error_2="ERROR SEARCH";
}
else{
//display data to the table
echo "<div id='res'><form id='form1' method='POST' action=".basename(__FILE__).">
Reservation Fees:<input id='descriptive' name='reserv' type='text' /> ";
echo "<table id='example' border='1'>
<tr>
<th>ID NUMBER</th>
<th>LAST NAME</th>
<th>FIRST NAME</th>
<th>MIDDLE NAME</th>
<th>COURSE</th>
<th>GENDER</th>
</tr>";
while($row=mysql_fetch_array($searchdata)){
$studentid=$row['student_id'];
$coursee=$row['Course'];
$lnamee=$row['Last_name'];
$fnamee=$row['First_name'];
$mnamee=$row['M_name'];
$gnder=$row['Gender'];
echo "
<tr>
<td>".$studentid." <input type='hidden' name='dummyID' value='$studentid'></td>
<td>".$lnamee." <input type='hidden' name='dummylname' value='$lnamee'></td>
<td>".$fnamee."<input type='hidden' name='dummyfname' value='$fnamee'></td>
<td>".$mnamee."<input type='hidden' name='dummymname' value='$mnamee'></td>
<td>".$coursee."<input type='hidden' name='dummycourse' value='$coursee'></td>
<td>".$gnder."<input type='hidden' name='dummygnder' value='$gnder'></td>
<td><input id='send' name='add' type='submit' value='Reserve' /></td>
</tr>";
}
echo "</table>
</form></div>";
}}}
you have a form with multiple elements named dummyID, dummylname, etc. So the value that gets submitted is just the last one you output.
Add in an index {$i} to determine which row is being submitted.
$i = 0;
while($row=mysql_fetch_array($searchdata)){
$studentid=$row['student_id'];
$coursee=$row['Course'];
$lnamee=$row['Last_name'];
$fnamee=$row['First_name'];
$mnamee=$row['M_name'];
$gnder=$row['Gender'];
echo "
<tr>
<td>".$studentid." <input type='hidden' name='dummyID[$i]' value='$studentid'></td>
<td>".$lnamee." <input type='hidden' name='dummylname[$i]' value='$lnamee'></td>
<td>".$fnamee."<input type='hidden' name='dummyfname[$i]' value='$fnamee'></td>
<td>".$mnamee."<input type='hidden' name='dummymname[$i]' value='$mnamee'></td>
<td>".$coursee."<input type='hidden' name='dummycourse[$i]' value='$coursee'></td>
<td>".$gnder."<input type='hidden' name='dummygnder[$i]' value='$gnder'></td>
<td><input id='send' name='add[$i]' type='submit' value='Reserve' /></td>
</tr>";
$i++
}
Then process the data like:
if(!empty($_POST['add'])) {
$i = current(array_keys($_POST['add']));
$studentid=$_POST['dummyID'][$i]
$coursee=$_POST['dummycourse'][$i];
$lnamee=$_POST['dummylname'][$i];
$fnamee=$_POST['dummyfname'][$i];
$mnamee=$_POST['dummymname'][$i];
$gnder=$_POST['dummygnder'][$i];
}