I have a while loop that pops out all of my blog posts. Along with these blog posts it puts up a button beside each one with a red x that when I click it I want it to delete the post next to it. The problem? I haven't come up with an effective way for it decide which post to delete. It usually either deletes the post by largest Id or all of the posts. So I will give you an idea of what I have... (this isn't my actual code, just an idea of it).
$result = mysql_query("SELECT * FROM post");
while($row = mysql_fetch_array($result){
$someVariable = $row['Id'];
$someVariable1 = $row['title'];
$someVariable2 = $row['content'];
?>
//This form right below here is the delete button. It functions, just need how to pull the correct Id***
<form action="" method="post">
<input type="submit" id ="<?php echo $someVariable ?>" value="" class="submit2" onClick="confirmation()">
</form>
<form action="" method="post">
<input type="submit" value="" class="submit3">
</form>
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Delete Post?")
if (answer){
<?php
mysql_query("DELETE FROM posts WHERE id=***");
?>
}
}
//-->
</script开发者_运维知识库>
<?php }?>
The function inside the Javascript is where the issue is. I can't seem to figure out how to get it to give back the right Id. I am fairly raw with php so be easy on me :p.
What you want is to submit the form when the user confirms the delete and handle the delete itself on the server.
function confirmation() {
var answer = confirm("Delete Post?")
return answer;
}
This way the form submits when the user confirms and the submit cancels when the user does not confirm.
Include the id in a hidden element on the form named "id". Let the form submit to a php page that then grabs the id from the $_POST['id'] and execute the SQL statement on the database then redirect to the list page again.
You cannot mix PHP and Javascript this way. Listen to me, it seems you've been terribly misled by something or someone, and you have to start from the basics, from scratch.
Please, listen to me: look for some basic PHP tutorial and remember PHP is a text pre-processor, while Javascript lets you have some dynamic interaction after the page has been fully loaded. It's client-side, whereas PHP is server side. Mixing them is something you might learn later.
You may not like this answer, downvote it if you want, but it's the only sensible one for this kind of scenario. Forget AJAX and other fancy technicalities other people suggested you answering this question, because you will eventually use them when you've learnt the basics, otherwise you'll get frustrated by not being able to make them work. You have to do some "hello world"s for some time.
Back to the basics, or you'll do some mess!
You can not mix PHP and Javascipt in the way that your propose. The PHP part is executed on the server, the result is sent back the user and only then the Javascript is executed.
You will need to use AJAX/XmlHTTPRequest to achieve what you want.
Assuming I understand what you're asking, instead of setting the id
of the submit element, it may be a better idea to include a hidden
form element instead:
<input type='hidden' name='postID' value='<?php echo $someVariable; ?>'>
When you process the form, use that instead.
Well I don't do PHP and I've been stuck in winforms land for about 4 years on long term contract right now but I believe the answer is that you can pass a parmeter (this) to your confirmation() function confirmation(this) which will give you access to all the properties on the button in your javascript function.
You cannot execute a PHP script from within a JavaScript as you are expecting. What you need to do is have JavaScript call a PHP script in a ajaxian approach.
Have your php echo
"<a href=\"javascript:deleteMe($id)\"><img src="x.jpg" /></a>"
into the html. Then the javascript should accept a param into the deletion function and submit an AJAX request to your php file.
You don't need a form for each entry. Just use an anchor tag with an onclick handler that points to your confirmation function. You can echo the id for the record into your confirmation function. Then have the confirmation function update a hidden field in your form for the record id to delete, and then submit the form to your delete php script.
精彩评论