/    Sign up×
Community /Pin to ProfileBookmark

javascript confirm() php delete

I’m really an amateur at this, but it seems to me like this code should work. The overall PHP function is called by a link and it’s meant to delete items. I really would like to give my users a fail-safe if they mistakenly click the button for the function. Any idea how to improve on this code and make it work?? (At the present time, it runs the delete statements regardless of the answer you provide at the confirmation.)

Thanks!

[CODE]function delete_item($itm_no)
{
echo ‘<SCRIPT language=”JavaScript”>
var answer = confirm(“Do you really want to delete this item?”)
if (answer)
{‘;

$sql2 = ‘DELETE FROM `Item` WHERE `item_id` = ‘. $itm_no .’ LIMIT 1′;
$sql3 = ‘DELETE FROM `List_Xref` WHERE `item_id` = ‘. $itm_no .’ LIMIT 1′;
$result2 = mysql_query($sql2)
or die (“Could not execute query: “.mysql_error());
$result3 = mysql_query($sql3)
or die (“Could not execute query: “.mysql_error());

echo ‘}else{}</SCRIPT>’;
}[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@rulianAug 27.2007 — i'm afraid you cant do that.

PHP cant execute in the middle of a javascript.

PHP is a server side language, which means before a client's computer ever gets any information from the server, the PHP must be completly finished.

tha'ts why your script deletes no matter what because it doesnt see the javascript.

Likewise, javascript cant execute a server side command from the client side, you can do it via AJAX if you want to look into it, but at that point to achieve what you want, the principle is the same

if (answer == true)

{

/// your code to pass information to the server

/// which could be as simple as a window.href tag


<script type="text/javacript" language="Javascript">

function deleteMe(id){

var answer = confirm("are you sure?");

if (answer == true)

{

window.location.href='mydeleteurl.php?delitem=' + id;

}

}

</script>

the rest all depends on your PHP, and how you set up your calls in the body

}
Copy linkTweet thisAlerts:
@YelgnidrocAug 27.2007 — I tend to use a onclick handler, e.g. using a submit button to delete an appointment:

[CODE]<INPUT TYPE="submit" name="delete" value="Delete appointment" class="delete_button" onClick="return confirm('Are you 100% totally certain that you want to DELETE this appointment?')">[/CODE]

This could be adapted for use with an hyperlink as follows:

[CODE]<?php
echo "<a href='delete.php' onClick="return confirm('Are you 100% totally certain that you want to DELETE this appointment?')" >hello</a>";
?>[/CODE]
×

Success!

Help @dz_boy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...