I am generating radio buttons dynamically by PHP but it is not working .I am not able to uncheck a radio button once checked and once I select the other radio button with same name other is not automaticaly unchecked .
echo "<form action="."".">";
echo "<table border='1'>
<tr>
<th>Reg Num</th>
<th>Select</th>
<th>Reject</th>
</tr>";
/* <th>Name</th>
<th>State</th>
<th>Constituency</th> */
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
// echo "<td>" . $row['voter_id'] . "</td>";
// echo "<td>" . $row['name'] . "</td>";
// echo "<td>" . $row['state'] . "</td>";
// echo "<td>" . $row['constituency'] . "</td>";
echo "<td>" . $row['numreg'] . "</td>";
echo "<td><input type="."radio"." name=".$row['voter_id']. "value="."1"."></td>";
echo "<td><input type="."radio"." name=".$row['voter_id'] ."value="."2"."></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
try following
echo "<form action=''>";
echo "<table border='1'>
<tr>
<th>Reg Num</th>
<th>Select</th>
<th>Reject</th>
</tr>";
/* <th>Name</th>
<th>State</th>
<th>Constituency</th> */
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
// echo "<td> $row['voter_id']</td>";
// echo "<td>" . $row['name'] . "</td>";
// echo "<td>" . $row['state'] . "</td>";
// echo "<td>" . $row['constituency'] . "</td>";
echo "<td>" . $row['numreg'] . "</td>";
echo "<td><input type='radio' name='voter_id' value='$row[voter_id]'></td>";
echo "<td><input type='radio' name='voter_id' value='$row[voter_id]'></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
For that the name of your radio button must be same.Change your code like the following inside the while loop where you are creating the radio buttons
echo '<td><input type="radio" name="voter_id" id="'.$row["voter_id"]. '"value="1"></td>';
echo '<td><input type="radio" name="voter_id" id="'.$row["voter_id"] .'"value="2"></td>';
We dont need double quotes for the type.Try like
echo "<td><input type='radio' name='".$row['voter_id']. "' value='1' "></td>";
Finally I did this and it worked
echo '<td><input type="radio" name="'.$row["voter_id"]. '"value="1"></td>';
echo '<td><input type="radio" name="'.$row["voter_id"] .'"value="2"></td>';
Related
I'm trying to update a mySQL table after a button click..The button click is not the problem but I wonder how I can get the klant_pk which is unique to update a certain record in mySQL. As you see I print out the mySql table at first. So is there anyone who know how I can get the according klant_pk after I click on a button in the table..
Thanks
$result = mysqli_query($con, "SELECT * FROM bestelling");
echo "Bestellingen";
echo "<table border='1' align='center'>
<tr>
<th>Bestelling_pk</th>
<th>Klant_pk</th>
<th>Product</th>
<th>Commentaar</th>
<th>Tijd</th>
<th> Voortgang </th>
<th> Status </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['bestelling_pk'] . "</td>";
echo "<td>" . $row['klant_pk'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['opmerking'] . "</td>";
echo "<td>" . $row['tijd'] . "</td>";
echo "<td> <input type='button' value='In Wacht' onclick='return change(this);' />";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
while ($row = mysqli_fetch_array($result)) {
...
echo "<td id='klank_pk_".$row['klant_pk']."'>" . $row['klant_pk'] . "</td>";
...
echo "<td> <input type='button' value='In Wacht' onclick='change(getElementById('klank_pk_".$row['klant_pk']."').value);' />";
...
}
I have a form which displays stock levels and I want the user to be able to delete multiple products so have provided checkboxes. the code is below:
echo "<form method='get'>
<input type='submit' name='removestock' value= 'Remove'>
<table class='display' border='0'>
<tr>
<th>Select</th>
<th>Name</th>
<th>Description</th>
<th>Price (£)</th>
<th>Quantity</th>
<th>Size</th>
</tr>";
echo "<tr>";
require ('connection.php');
$query = mysql_query("SELECT * FROM items")or die(mysql_error());
while($results = mysql_fetch_array($query)){
echo "<td> <input type='checkbox' name='item' value='".$results['item_id']."'></td>";
echo "<td>" . $results['name'] . "</td>";
echo "<td>" . $results['description'] . "</td>";
echo "<td>" . $results['price'] . "</td>";
echo "<td>" . $results['quantity'] . "</td>";
echo "<td>" . $results['size_id'] . "</td>";
echo "</tr>";
}
echo "</table></form>";
And my parsing code is...
if(isset($_GET['removestock']) === true){
$errors = array();
$items = $_GET['item'];
echo $items;
}
But for some reason it only displays the last item_id selected. Your help will be much appreciated!
PS. I tried changing the checkbox name to name="items[]" and implemented a foreach loop to parse the data but it still did not work.
The name of the checkbox should be
<input type='checkbox' name='item[]' value='".$results['item_id']."'>
This wil give you an array which you can read with a foreach
Use name=item [] for the checkboxes. This will give you the array.
Then use var_export ($ items)
Change this line:
"<td> <input type='checkbox' name='item' value='".$results['item_id']."'></td>";
To read:
"<td> <input type='checkbox' name='item[" . $results['item_id'] . "]'></td>";
And you will get your desired array in the variable.
This always works for me:
$i =0;
while($results = mysql_fetch_array($query)){
echo "<td> <input type='checkbox' name='item[".$i."]' value='".$results['item_id']."'></td>";
echo "<td>" . $results['name'] . "</td>";
echo "<td>" . $results['description'] . "</td>";
echo "<td>" . $results['price'] . "</td>";
echo "<td>" . $results['quantity'] . "</td>";
echo "<td>" . $results['size_id'] . "</td>";
echo "</tr>";
$i++;
}
Once posted, the item[] element will be an array.
I am new to PHP and I have problem. I have a table with check boxes. I need to add if statement inside the check box.
echo "<div class='table1'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
echo "<form action='confirmsubmission.php' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'
value='".$row['module_id']."' />" . "</td>";
echo "<td>" . $row['module_id'] . "</td>";
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";
echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></div>";
Below is a If statement I need to add after value='".$row['module_id']."'
if($row['module_choice']=='Mandatory'){ echo "checked=\"true\""; }
A bit of a mouthful, but try:
...
echo "<td>" . "<input type='checkbox' name='check[]' value='".$row['module_id']."'".($row['module_choice']=='Mandatory' ? 'checked="true"' : "")." />" . "</td>";
...
Even though I'll advise you to use a templating system (Smarty, etc.), you can do the following code:
echo "<div class='table1'>
<form action='confirmsubmission.php' method='post'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'
value='".$row['module_id']."'";
if($row['module_choice']=='Mandatory'){
echo " checked='checked' ";
}
echo "/>" . "</td>";
echo "<td>" . $row['module_id'] . "</td>";
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";
echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></form></div>";
I am trying to retrieve records from db and put them into a table. I want to have an update button which can do some processing. Here is the code. It outputs properly. However the update button (image) doesn't work. Please help
<?php
echo "</br>";
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>Customer Number</th>
<th>Order Items</th>
<th>Transaction Time</th>
<th>Amount</th>
</tr>";
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("kaka", $con);
$sql="SELECT * FROM orders WHERE status = 'NRDY'";
$result2 = mysql_query($sql);
while($row1 = mysql_fetch_array($result2))
{
echo "<div class=\"update\"><form method='post' action=\"test.php\">\n";
echo "<tr>";
echo "<td><input type='hidden' name='order_id' value='".$row1['order_id']."'>" .$row1['order_id']. "</td>";
echo "<td><input type='hidden' name='cust_num' value='".$row1['cust_num']."'>" .$row1['cust_num']. "</td>";
echo "<td><input type='hidden' name='items' value='".$row1['items']."'>" .$row1['items']. "</td>";
echo "<td><input type='hidden' name='order_time' value='".$row1['order_time']."'>" .$row1['order_time']. "</td>";
echo "<td><input type='hidden' name='order_id' value='".$row1['amount']."'>" .$row1['amount']. "</td>";
//echo "<td>" . $row1['order_id'] . "</td>";
//echo "<td>" . $row1['cust_num'] . "</td>";
//echo "<td>" . $row1['items'] . "</td>";
//echo "<td>" . $row1['order_time'] . "</td>";
//echo "<td>" . $row1['amount'] . "</td>";
echo "<td>" . "<input type=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n" . "</td>";
echo "</tr>";
echo "</form></div>";
}
echo "</table>";
?>
Here is test.php
<?php
echo $_POST['order_id'];
?>
Use a validator. You aren't allowed to wrap divs or forms around table rows. Some browsers recover from this error by moving the div/form so it is outside the table (leaving the content behind). Your inputs are therefore not inside a form so shouldn't be expected to do anything.
I have trouble with creating an approval form as am still php beginner,
the idea is
user submit a form am setting a default value"0" in the approved row at the table..
so behind the scenes the admin shows all members from this table where approved="0"
and this is the code
<code>
<?php
$con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ebarea_epic", $con);
$query = "select * from medicalrep where approved='0'";
$result=mysql_query($query);
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Faculty'] . "</td>";
echo "<td>" . $row['Graduation Year'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['Line'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Appointment'] . "</td>";
echo "<td>" . $row['Resign'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</code>
I just want to add checkbox for every table user and when checked thier status changed to 1 in approved column
thanks all
$con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ebarea_epic", $con);
$query = "select * from medicalrep where approved='0'";
$result=mysql_query($query);
$i = 1; //counter for the checkboxes so that each has a unique name
echo "<form action='process.php' method='post'>"; //form started here
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
<th>Update</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['Faculty'] . "</td>";
echo "<td>" . $row['Graduation Year'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['Line'] . "</td>";
echo "<td>" . $row['Area'] . "</td>";
echo "<td>" . $row['Appointment'] . "</td>";
echo "<td>" . $row['Resign'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "<td><input type='checkbox' name='check[$i]' value='".$row['ID']."'/>";
echo "</tr>";
$i++;
}
echo "</table>";
echo "<input type='submit' name='approve' value='approve'/>";
echo "</form>";
mysql_close($con);
Now comes process.php
if(isset($_POST['approve'])){
if(isset($_POST['check'])){
foreach ($_POST['check'] as $value){
$sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
mysql_query($sql) or die (mysql_error());
}
}
}
though you are using mysql_* functions here, i recommend you to use PDO
EDIT:
As per your request, this is the update.
Change this code in your admin panel script:
echo "<input type='submit' name='approve' value='approve'/>";
Delete the above line and add this instead:
echo "<input class='action' type='button' name='approve' value='approve' />";
echo "<input class='action' type='button' name='edit' value='edit' />";
echo "<input class='action' type='button' name='delete' value='delete' />";
echo "<input type='hidden' name='action' value='' id='action' />"; //Action (edit, approve or delete) will be set here which will be passed as POST variable on form submission
Now you will need some javascript to do some tricks.
Add the following code preferably head section in your admin panel script
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.action').click(function(){
var action = $(this).attr('name');
$('#action').val(action);
$(this).closest('form').submit();
})
})
</script>
Now comes the modification in process.php file
if (isset($_POST['approve'])) {
if (isset($_POST['check'])) {
foreach ($_POST['check'] as $value) {
$sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
mysql_query($sql) or die(mysql_error());
}
}
} elseif(isset($_POST['edit'])){
//do the edit things here
} elseif(isset($_POST['delete'])){
foreach ($_POST['check'] as $value){
$sql = "DELETE FROM post WHERE ID=$value";//modify it
mysql_query($sql) or die(mysql_error());
}
}
NOTE
You may not want to mutiple checkbox for edit. You just need to tweak the javascript code above a little and it'l send the ID as a post variable on form submission from which you can retreive the details for one entry and then edit functions will come. I'l leave it to you. try it, post your trial code here and i'l give you a solution if it doesn't work.