PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
In my guestbook, I have it where users can post a comment onto the page. I would like it if there was a button that would delete the post selected. The code i use is:
function deleteEntry(form, id) {
if (confirm("Are you sure you want to delete this entry?")) {
var theForm = document.getElementById(form);
theForm.item_to_delete.value = id;
theForm.submit();
}
else {
return false;
}
}
I typically keep all my js in its own directory and include files as needed, but that's more to keep my file structure somewhat sane. If you'll only be using that function in this one page and would rather have it directly in the page, that will work too.
When I do this, it does the pop up asking if I'm sure, but then nothing happens. When it is suppose to delete, i want each button to delete that one post that the button is associated with. How would I do this without defining an id number and how would i do it without erasing the entire database?
It won't work without defining an ID; that's what tells it which post to delete. Since I assume you're populating your form with information from the database to begin with, you should already have each item's ID.
I have my id auto increment each time a post is made. When I have it made, I want the button to be assigned to this value as well. How would I do this, and would it help if I posted my codes into here?
Even if the ID field is auto incremented rather than specified, each post is going to have its own ID. When you're fetching the info to populate your form, make sure the ID is included in your query.
<?php
function connect($to) {
include("cred.php");
include("delete.js");
$connection = mysql_connect($guestbook[$to]["host"], $guestbook[$to]["username"], $guestbook[$to]["password"]) or die ("Connot connect");
mysql_select_db($guestbook[$to]["database"], $connection) or die ("Connot connect to db");
$getstuff = mysql_query($guestbook[$to]["values"]) or die ("Invalid Query");
$rownum = mysql_num_rows($getstuff);
if($rownum==0) {
echo "<p>No posts have been made yet. Be the first to make one.</p>";
}
while($row = mysql_fetch_assoc($getstuff)) {
//set variables to show in the posts
$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
$message = $row['message'];
$date = $row['date'];
$time = $row['time'];
//set background color variables to set backgrounds to every other table based on id number
if($id%2) {
$bgcolor = '#FFFFFF';
}
else {
$bgcolor = '#E1E1E1';
}
echo "
<table bgcolor='$bgcolor'>
<tr>
<td>
<strong>Posted by: $name ($email) on $date at $time.</strong> <input type='submit' name='submit' value='Delete Post' onclick='deleteEntry(form,id);'>
</td>
</tr>
<tr>
<td>
".nl2br(strip_tags($message))."
</td>
</tr>
</table>
";
}
return $connection;
}
connect("mysqlconnector");
?>
<?php
function connect($to) {
include("cred.php");
include("delete.js");
$connection = mysql_connect($guestbook[$to]["host"], $guestbook[$to]["username"], $guestbook[$to]["password"]) or die ("Connot connect");
mysql_select_db($guestbook[$to]["database"], $connection) or die ("Connot connect to db");
$getstuff = mysql_query($guestbook[$to]["values"]) or die ("Invalid Query");
$rownum = mysql_num_rows($getstuff);
if($rownum==0) {
echo "<p>No posts have been made yet. Be the first to make one.</p>";
}
echo "<form id=\"frmPosts\" action=\"\" method=\"post\">
<input type=\"hidden\" name=\"item_to_delete\" value=\"\" />";
while($row = mysql_fetch_assoc($getstuff)) {
//set variables to show in the posts
$id = $row['id'];
$name = $row['name'];
$email = $row['email'];
$message = $row['message'];
$date = $row['date'];
$time = $row['time'];
//set background color variables to set backgrounds to every other table based on id number
if($id%2) {
$bgcolor = '#FFFFFF';
}
else {
$bgcolor = '#E1E1E1';
}
echo "
<table bgcolor='$bgcolor'>
<tr>
<td>
<strong>Posted by: $name ($email) on $date at $time.</strong> <input type='button' name='submit' value='Delete Post' onclick='deleteEntry(frmPosts, $id);'>
</td>
</tr>
<tr>
<td>
".nl2br(strip_tags($message))."
</td>
</tr>
</table>
";
}
echo "</form>";
return $connection;
}
if ($_POST['item_to_delete'] != '')
{
$sql = "DELETE FROM guestbook WHERE id = {$_POST['item_to_delete']}";
mysql_query($sql);
}
connect("mysqlconnector");
?>