Posting from a form in a table - php

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.

Related

Get table value html

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);' />";
...
}

Multiple delete through checkout

I am stuck in one of my application module. I have to set multiple delete options in my application.
I have used the below code. The designing is perfectly fine. All what I want to add multiple delete options in it.
Below is the code which I have tried out:
<html>
<head>
<title>Strad Hosting Limited -Web Hosting</title>
<script language="javascript">
function validate()
{
var chks = document.getElementsByName('checkbox[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one.");
return false;
}
return true;
}
</script>
</head>
<body >
<?php
$con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<form name='form1' method='post' action='' onSubmit='return validate();'>";
echo "<table border='1' style='
background-color: white;'>
<tr>
<th></th>
<th>id</th>
<th style='padding-left: 11px;'>Lead Generated Date </th>
<th>name</th>
<th>email</th>
<th>contacts</th>
<th>requirement</th>
<th style='display:none;'>Company name</th>
<th style='display:none;'>Address</th>
<th>Next Follow-Up date</th>
<th>Final_Details</th>
<th style='display:none;'>Status</th>
<th style='padding-left: 12px; display:none;'>Lead Closed Date</th>
<th>EDIT</th>
<th>Follow-up History</th>
<th>Add Follow-up</th>
<th>Delete Record</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['contacts'] . "</td>";
echo "<td>" . $row['requirement'] . "</td>";
echo "<td style='display:none;'>" . $row['company_name'] . "</td>";
echo "<td style='display:none;'>" . $row['address'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['final_details'] . "</td>";
echo "<td style='display:none;'>" . $row['status'] . "</td>";
echo "<td style='display:none;'>" . $row['lead_close'] . "</td>";
echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>";
echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>";
echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
echo "</tr>";
}
echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>";
$count=mysqli_num_rows($result);
if(isset($_POST['delete'])){
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id=$_POST['checkbox'][$i];
$sql = "DELETE FROM Persons WHERE id='$del_id'";
echo $sql;
$result2 = mysqli_query($con,$sql);
}
// if successful redirect to delete_multiple.php
if($result2)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_table_del.php\">";
}
}
mysqli_close($con);
echo "</table>";
echo "</form>";
?>
<br><br>
[ Back To Home ]
</body>
</html>
I am stuck, the query is not working I think. I don't know what I am missing.
Try replacing
$sql = "DELETE FROM Persons WHERE id='$del_id'";
With
$sql = "DELETE FROM Persons WHERE id=$del_id";
I'm assuming the ID is a number so the single quotes aren't needed.

Parsing multiple checkbox values into php array

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.

Dynamically generated radio button dont work

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>';

Approve Form submit

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.

Categories