generating buttons to update inventory in php - php

Okay so I'm a bit lost here as I'm very new to AJAX and fairly new to PHP. So I have an inventory management system build with phpmyadmin and wamp. The registration/ log in system works great. so when the user logs in a table of all inventory is generated on screen, along with a button next to each inventory item that allows the current user to check in or check out an item. the buttons are all generated and I gave them a unique id using php and the code looks like this.
<table class = "table">
<thead>
<tr>
<td><center><strong>Item Id</strong></center> </td>
<td><center><strong>Item Description</strong></center> </td>
<td><center><strong>Item Type</strong> </center></td>
<td><center><strong>Availability</strong></center></td>
<td><center><strong>Name</strong></center></td>
<td><center><strong>Check in/out button</strong></center></td>
</tr>
</thead>
<tbody>
<?php
mysql_select_db('inventory management system');
$query="SELECT * FROM inventory";
$results = mysql_query($query);
$a = 0;
while($row = mysql_fetch_array($results)) {
$a++;
echo '<script type = "text/javascript">c++;</script>'
?>
<tr>
<td><center><?php echo $row['item_id']?></center></td>
<td><center><?php echo $row['item_desc']?></center></td>
<td><center><?php echo $row['item_type']?></center></td>
<td><center><?php echo $row['availability_status']?></center></td>
<td><center><?php echo $row['name']?></center></td>
<td><center><?php
if(is_null($row['name'])){
//this is what is generating each button with a unique id echo '<input class="btn btn-default" type="submit" value="Check Out" id = "<?php echo $a; ?>" onclick="<?php ?>">';
}else if(!is_null($row['name'])){
echo '<input class="btn btn-default" type="submit" value="Check In" id = "<?php echo $a; ?>" onclick="updateinventory()">';
}
?></center></td>
</tr>
<?php
}
?>
</tbody>
</table>
so now when the user clicks a button, it updates the database to set the name field to null signifying that nobody has the item checked out, that way another user can sign in and check out an item in the inventory. I'm really struggling and have tried everything from on click methods (which for some reason I couldn't get to work) to plenty of things with ajax and php.. and I cant get a seemingly simple feature to work... Thank you for any possible help.

You have already opened php tags so change
echo '<input class="btn btn-default" type="submit" value="Check In" id =
"<?php echo $a; ?>" onclick="updateinventory()">';
to
echo "<input class='btn btn-default' type='submit' value='Check In'
id ='$a' onclick='updateinventory()'>";
Similarly remove php tags from IF since php starting tags have been written above.
type="submit" this will directly submit the page, and you probably need to make it button so that you may call javascript function that will eventually call ajax.

Related

Pass Two values when a Submit button is clicked -PHP

This table is displaying details about my workload table per row. And when I click my "pre-View_attend" submit button I want to get the "WorkloadID" and "subjectID" of that workload row where the submit button was clicked.
Currently I have a hidden field containing the subject ID which is "subjectID_hidden", and when I try to get the "WorkloadID" and "subjectID" by using isset.
It seems that I can't accurately get the exact subject ID of the row where I've clicked the "pre-View_attend" submit button.
<tbody style="font-size:20px; text-align:center;">
<tr>
<td><?php echo $row["subjectName"]; ?></td>
<td><?php echo $row["className"]; ?></td>
<td><?php echo $row["RoomNumber"]; ?></td>
<td><?php echo $Sched_Days.'<br>'.$FST.' To '.$FET;?></td>
<td><button style="font-size:15px;" type="button" id="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary view_StudentList">View Student List</button></td>
<td><button style="font-size:15px;" type="submit" name="gettingAttendance" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">Take Attendance</button></td>
<td><button style="font-size:15px;" type="submit" name="pre-View_attend" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">View Attendance</button></td>
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
</tr>
<?php } mysqli_close($connect);?>
</tbody>
/*
I'm getting the workload ID accurately, but the subject ID is incorrect.
I believe I'm getting the last subject ID that my query produced
*/
<?php
if(isset($_POST['pre-View_attend']))
{ $FWID=$_POST['pre-View_attend'];
$FSJID=$_POST['subjectID_hidden'];
echo"Workload ID: $FWID SubjectID: $FSJID";
}
?>
You get the last value because you have a bunch of hidden fields with the same name.
You could name them subjectID_hidden[<?=$row['WorkloadID'];?>] to get an array to then get the subjectID by WorkloadID:
$subjectID = $_POST["subjectID_hidden"][$_POST["pre-View_attend"]];
You need to query the table for something unique to the row you want to retrieve information from. This unqiue data should already be existing on the page, encoded into the button/form.
Example;
<?php
if(isset($_POST['pre-View_attend'])) {
global $db;
$select = "select * from mytable where unique='$unique'";
$connect = mysqli_query($db, $select);
while($row=mysqli_fetch_array($connect)) {
$myfirstVariable = $row['myfirstVariable'];
$mysecondVariable = $row['mysecondVariable'];
$show = "$myfirstVariable $mysecondVariable";
}
}
?>
Then you can call $show when you want in the HTML.
p.s. there are quite a few errors in your html like;
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
should be
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>" />
self closing at the end with forwards slash.

Deleting Specific rows using PHP

I am trying to add a delete button to my note taking website project I am doing at school. Almost like a forum, where you would delete replies to a topic... I want to be able to delete individual notes. Currently I can delete, however it will only delete the most recent note (bottom of the notes table). How would I get it so the delete button deletes the note from the corresponding row, not just the bottom one.
The main interface (Where I believe the problem is)
$sql="SELECT noteID, title, note, timestamp FROM $tbl_name WHERE user='$currentuser' AND subject='$currentsubject'";
$result=mysql_query($sql);
?>
<table align="center" id="notes">
<tr>
<td width="10%"><strong>Title</strong></td>
<td width="10%"><strong>ID</strong></td>
<td width="50%"><strong>Note</strong></td>
<td width="10%"><strong>Time</strong></td>
<td width="10%"><strong>Delete</strong></td>
</tr>
<?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
echo'
<br>
<tr>
<td>'.$rows['title'].'</td>
<td>'.$rows['noteID'].'</td>
<td>'.$rows['note'].'</td>
<td>'.$rows['timestamp'].'</td> ';
$todelete = $rows['noteID'];
$_SESSION['todelete'] = $todelete;
echo'
<td><form method="post" action="deletenote.php" ><input type="submit" name="submit" value="Delete" </td>
</tr>
';
}
ob_end_flush()
?>
Another way of doing it would be by just providing a delete link for each post:
// Start looping table row
while($rows=mysql_fetch_array($result)){
echo '<br>';
echo '<tr>';
echo '<td>'.$rows['title'].'</td>';
echo '<td>'.$rows['noteID'].'</td>';
echo '<td>'.$rows['note'].'</td>';
echo '<td>'.$rows['timestamp'].'</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
Keep in mind that deletenote.php would have to verify that the user is signed in and has permission to delete the post.
First of all: where is your corresponding code/query regarding the specific use case (delete note)?
You are trying to submit a form without any parameters, I am assuming you are trying to use the session variable. However, this variable will always be set to the last loop-iteration. Therefore, I would recommend you to use this code sample:
// Start looping table row
while($rows=mysql_fetch_array($result)){
echo '<br>';
echo '<tr>';
echo '<td>'.$rows['title'].'</td>';
echo '<td>'.$rows['noteID'].'</td>';
echo '<td>'.$rows['note'].'</td>';
echo '<td>'.$rows['timestamp'].'</td>';
echo '<td><form method="post" action="deletenote.php" ><input type="hidden" name="noteID" value="'.$rows['noteID'].'"><input type="submit" name="submit" value="Delete"></form> </td>';
echo '</tr>';
}

can't delete from specific row from generate table from mySQL

Hye, i'm working on a small project of Inventory system. Everything is okay until this last part. I have included a delete button at the end of each row for user to delete any item of choice, but when user press the delete button, it deleted the last row of the table, instead of the row/item of choices, where is my mistake in my coding?
Thank you!
<?php
$result= mysql_query("SELECT * from Staff ORDER BY status");
$count=1;
while($row=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><?php echo $count; ?></td>
<td align="left"><?php echo $row['staffId']; ?></td>
<td align="left"><?php echo $row['name']; ?></td>
<td align="left"><?php echo $row['address'];?></td>
<td align="left"><?php echo $row['pNum'];?></td>
<td align="left"><?php echo $row['status'];?></td>
<td align="left"><?php echo $row['type'];?></td>
<td><input type="submit" name="deleteStaff" value="Delete" onClick="displayMessage()">
<input type="hidden" name="staffId1" value="<?PHP echo $row['staffId']; ?>">
</td>
</tr>
</tbody>
<?php
$count++; }
if(isset($_POST['deleteStaff'])){
$id=$_POST['staffId1'];
$result=mysql_query("DELETE from Staff WHERE staffId='$id' ");
if($result){
echo '<script> location.replace("viewStaff.php"); </script>';
}
else{
?>
<script>
alert ("Fail to delete data")
window.location.href='viewStaff.php'
</script>
<?PHP
}
}
?>
I've encountered this error before as well. I think the error is happening since you're calling the same "onclick" function for each array result.
Instead, create a link to a page where you can run the php deletion script and pass the staffID into the URI and Get that in the php deletion script that you created.
example
<form action="deletion_script.php?staffid=<?php echo $staffid; ?>">
Put that inside of the td tag
Then in your deletion_script.php file, put something like this
<?php
//get staffid from URI
$staffid = $_GET['staffid'];
$sql="DELETE FROM $table_name WHERE staffid = '$staffid'"
$result = mysqli_query($connect, $sql);
if($result) {
//send back to the page you want
header("Location: ../inventory.php");
}
?>
This worked for me, and how I handle all situations like this from now on. You'll need to adjust the code a little to fit your set up.
since the name for the hidden field is the same for all rows being generated the post request is pulling the value of the last row( also "staffId1" ).
I recommend either using a individual form for each row in which case your code doesn't change as much or use js to get the required staffID using selectors
If you include the same hidden field (StaffId1) for every record it will contain more then one value.
You are now listening to the pressed SUBMIT so you don't need the hidden field. You can just get the value if the pressed SUBMIT.
Button
type="submit" name="staffId" value="<?php echo $id ?>"
Php
$id=$_POST['staffId'];

How to I get selected row ID when deleted or updated row on button click?

How do I get selected row id when deleted or update row button click?
A button click passes the ID in Demo1.php file.
demo.php
<?php if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {?>
<tr>
<td> <?php echo $row["ID"];?></td>
<td> <?php echo $row["Name"];?></td>
<td> <?php echo $row["Email"];?></td>
<td> <a href="Demo1.php" ><button name="Update" id="Update" onclick="Update($row['ID'])" >Update</button></a></td>
<td> <button name="Delete" id="Delete" onclick="Delete($row['ID'])" >Delete</button>
</td>
</tr>
</table>
You simply need to use <?php ?> and echo in the Javascript function calls. I assume that you already implemented the two Javascript functions - Update and Delete.
<button name="Update" id="Update" onclick="Update(<?php echo $row['ID']; ?>)" >Update</button>
<button name="Delete" id="Delete" onclick="Delete(<?php echo $row['ID']; ?>)" >Delete</button>
I'm not sure why you use both <a> and <button> click for Update. If you want to pass ID to demo1.php, you just need to add a query string in href.
Update
I have done something similar to what #Sithu suggested, but instead of the ID I added this for each added instance. And add the ID to each button name, and just create onClick for each name...

Is POST information from jquery $.post sent in parent page

I have a parent page which has a drop down list in it. using the onchange event, data is posted to a second page using $.post(). This page uses the posted variables to output mysql data with a checkbox for each line. This page output is then inserted into the parent page using jquery $('#DIV name').html(output).show();
The user can then see the mysql table rows with corresponding checkboxes. They then select the checkboxes they want and say delete. Delete is a form submit button.
My question is, when they click delete how do I take the form data from the second page and post it with that of the parent page so that I can then use $_POST[] to get the checkbox info and delete the selected table rows?
example of the parent page code is:
javascript/jquery
<script type="text/javascript">
function get(row){ //row being processed, defined in onchange="get(x)"
('getpeopleinjobs.php',{ //data posted to external page
postvarposition: form["position"+row].value, //variable equal to input box, sent to external page
postvarjob: form["job"+row].value, //variable equal to input box, sent to external page
postvarperson: form["person"+row].value, //variable equal to drop down list, sent to external page
postrow: row}, //variable equal row being processed, sent to external page
function(output){
$('#training'+row).html(output).show(); //display external results in row div
//popupWindow = window.open('t4.php?variable1Name=Vicki&variable2Name=Maulline','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
});
}
</script>
Form data is
<tr>
<td>
<?PHP echo $i; ?>
</td>
<td>
<input type=text NAME="position<?PHP echo $i; ?>" id="position<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,0);?>">
</td>
<td>
<input type=text NAME="job<?PHP echo $i; ?>" id="job<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,1);?>">
</td>
<td>
<SELECT NAME="person<?PHP echo $i; ?>" id="person<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" onchange="get(<? echo $i; ?>);">
<OPTION VALUE=0 >
<?=$optionpeople?>
</SELECT>
</td>
<td onclick="train(<? echo $i; ?>);" style="color:grey; cursor: pointer;">
<div id="training<?PHP echo $i; ?>"><font color=grey size=2></div>
</td>
<td>
</td>
</tr>
<?PHP
$i++;
$r++;
}
?>
The second page or page called by jquery, the output is:
echo
"
<table border=0 width=400>
<tr>
<td width=20>
</td>
<td width=150>
<b>Position<b>
</td>
<td>
<b>Job<b>
</td>
</tr>
";
while($line = mysql_fetch_array($result))
{
echo "
<tr>
<td>
";
?>
<input type=checkbox name="delete[]" id="delete[]" value="<?php echo $rows['id']; ?>">
<?PHP
echo "
</td>
<td>
";
echo $line['position'];
echo "
</td>
<td>
";
echo $line['job'];
echo "
</td>
</tr>
";
}
?>
<tr>
<td>
<input type=submit name="update" id="update" value="Update">
</td>
</tr>
</table>
<?PHP
}
To repeat I want the table checkbox element from the second page posted with the form data of the parent page.
Is this possible as my testing hasnt given me any luck.
Am I doing something wrong or do I need to modify the jquery code?
Thanks as always for the assistance.
There is no second page. Really - you're loading HTML content from the second page, but it's being inserted into the parent page. All content, from your browsers perspective, is in the same page. The fields should be included as long as they're inside the DOM inside the element. Use the developer tools for your browser or Firebug for Firefox to make sure that the content is placed within the form element in the DOM. The developer tools should also be able to show you exactly which variables are submitted to the server when the form is submitted.
The 'action' parameter of 'form' will indicate where the content gets submitted (and it seems you've left that part out of your HTML, so it's impossible to say if you've left out or just not included it in the paste.

Categories