Hi folks,
i was wondering if i wanna delete a record from database how can i do it using php and when a person click cancel button b4 running of query he is alerted about if he is sure of deleting his account?
You can’t run php code client side. You’ll have to take a look at AJAX, or do a redirect to the PHP script that does the deletion after has confirmed he wants to cancel his membership
Perhaps code example will help. please?
Edit : How do i target someone with his primary id (which isn’t shown on screen) the only thing we have on screen is Name when he confirms it.
If you want to do this with PHP, just add a second stage to the sequence.
User clicks link (button = link, effectively), which loads the page.(http://www.mysite.com/deleteuser.php)
PHP sees user has clicked button, but hasnt sent confirmation. Asks user to confirm. Link now contains confirm variable. (http://www.mysite.com/deleteuser.php?confirm=1) User clicks confirm link, page reloads.
PHP sees user has clicked confirm, performs query.
Now, if you want to do this without reloading the page, you’re looking at a Javascript effect.
- you can use JS to show an alert box when the user clicks the cancel button, then when the user clicks ok call the php script that does the deletion
or
- clicking the cancel button calls another page that shows the question, and when the user clicks ok it calls another php script that does the deletion.
Beat by a couple of seconds…
A example would be helpful?
here is what made but it doesn’t work
<script>
function confirmation(){
var cancel = confirm("Do you really want to cancel your membership?")
if(cancel){
<?php
$queryDel = mysql_query("Delete from info where name = $_REQUEST[name] ") ;
if($queryDel){
echo "Unscuessfull";
}else{
echo "Successful";
}
?>
}else{
}
}
</script>
Try…
<form action="#" method="post">
<input type="submit" name="cSub" id="cSub" value="Cancel">
</form>
I created this to test if it echo but it doesn’t
<input type="button" name="cSub" id="cSub" value="Cancel" onClick="window.location.reload()" />
if(isset($_POST['cSub'])){
echo "Deleted";
}
where am i going wrong?
If i go with the 3rd option. How will i do it? i mean i did the reload page but won’t it execute the php query even b4 clicking the button (First time user come on the page i.e)? Would i use if condition with isset?
Exactly. Use an if isset to see if the first button has been clicked; if so, display the confirm message (instead of displaying the regular form again), with another button/link/whatever. Then add another if isset to check if they’ve clicked the confirm link.
(Generally it’ll go in reverse order, so check for the confirm first in your code)
Onclick is a Javascript event. You can write an onclick function in PHP, yes, but it wont work with the javascript onclick.
I’m suggesting the same thing i suggested 6 posts ago. either: 1) learn AJAX (1 ‘pageload’, with a javascript confirm, and then an AJAX call to actually do the delete), or 2) use the full PHP method (3 pageloads; the original page, the confirm page, and the actual deletion)
onClick=‘…’ is JS.
Of course, you can also write a function in PHP and call it onClick, but that’s not the same thing.
So what are you suggesting? btw the function onclick isn’t a JS, its php
No.
PHP runs when a page is loading. (Server-Side)
Javascript runs when a page is sitting in the browser (Client-Side).
Javascript cannot run PHP code directly.
Think of it this way.
You’re running a… car factory.
Your automated robots have in their systems the blueprints and materials for a 4 door sedan. (The Code)
The automated robots make the car. (PHP)
The customer buys the car. (Page Loaded.)
The customer pushes a button in the car, and the air conditioning comes on. (Javascript).
The customer can not push a button in the car, and make the robots change the car to an SUV. The robots have already done their job on this car. It’s finished.
When PHP runs, everything between <?php and ?> does not go out to the client. When you View Source on your code posted in the OP, what you will see is this:
<script>
function confirmation(){
var cancel = confirm("Do you really want to cancel your membership?")
if(cancel){
}else{
}
}
</script>
[
Well for now what i am doing i creating a button with onclick function. The function is of php and i have written a function for it. so when clicked it will execute the query. do u think the logic is right?
help guys? anyone
I would appreciate if someone gave me a million dollars.
If you’re not willing to learn AJAX, use PHP, send the form to a new (or the same), catch the form variable (from your code above, you should probably be looking for $_POST[‘name’] ), and present a new form, one with the ‘name’ as a hidden field, and a confirm field attached to the button. The page that form points to then catches the $_POST[‘confirm’], and deletes the user.
Thanks but i would appreciate if i can get a example. As i m not familiar with ajax so i have JS as only option or php