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];
}
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 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
Im am trying to add a check box that will enable/disable the edit button. Im retrieving a price list and displaying it inside a table. When i add the javascript into the php code it doesn't work. Below is my code
<table border="1">
<tr>
<td width="100">Fee % </td>
<td width="100">Price</td>
<td width="100">Total</td>
<td width="102"> </td>
</tr>
<tr>
<?php
$sql1="select * from pricelist";
$result1=mysql_query($sql1) or die(mysql_error());
while ($row=mysql_fetch_array($result1)) {
$id=$row['id'];
$price=$row['h_price'];
$a=0;
print "<form id='form1' name='$a+' method='post' action=''>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>";
print "</tr>";
print "</form>";
}
?>
</table>
Can someone please tell me what am i doing wrong?
Thanks
take out the following code out of loop... it creates multiple form tag...
print "<form id='form1' name='$a+' method='post' action=''>";
Place "<tr></tr>" inside the loop...
final code looks like:
<form id='form1' name='fm1' method='post' action=''> <tr>
<?php
$sql1="select * from pricelist";
$result1=mysql_query($sql1) or die(mysql_error());
while ($row=mysql_fetch_array($result1)) {
$id=$row['id'];
$price=$row['h_price'];
$a=0;
print "<tr>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>";
print "</tr>";
}
print "</form>";
?>
Next if u want to control the button only then
<input type='checkbox' onclick='document.$a.submit.disabled = !this.checked;' />
and make sure of the following things:
1.) form name should be $a
i.e <form name='$a' ...>
2.) submit buttons name should be submit
i.e <input type='submit' name='submit'...>
3.) increase the $a variable only at the end of loop
i.e
$a++;
}
i think $a+ will cause syntax error. you need to increment $a at the end of loop like $a++. Also see page source and see what is coming from server.
I would recommend using following format for html + php output
<form id='form1' name='<?=$a++?>' method='post' action=''>
<tr>
<td><input name='fees' value ='<?=$fees?>' type='text' size='4' /></td>
<td><input name='price' value ='<?=$price?>' type='text' size='15' /></td>
<td><input type='checkbox' onclick='this.disabled = !this.checked;'><td>
<td><input type='submit' name='<?=$a++?>' value='Submit' disabled='disabled' /></td>
</tr>
</form>
<?
you also need to have < tr > to be in the while loop.
In your html part where you set up the formular you have to use " " to escape the php code. example:
print "<form id='form1' name='".$a+."' method='post' action=''>";
But as Adeel said already, what is $a+ ?
And you have to edit all prints and echoes in the way, that php expressions are excaped from the HTML source.