javascript and php are completely different. first of all, php is server side and javascript is client side. This means that php and javascript cannot communicate with eachother easily. Its possible but only through varibles passed along by the GET function of php.
DELETE.PHP
<?php
if (isset($_GET['studentCode']) && !isset($_GET['conf'])) {
$id = $_GET['studentCode'];
echo '
<script type="text/javascript">
var del = window.confirm("Are you sure deleting the selected record?");
if (del==true) {
window.location="http://www.banditssoftball.org/test2.php?studentCode=' . $id . '&conf=yes";
}
else {
window.location="http://www.something.com/list.php";
}
</script>';
}
if (isset($_GET['conf'])) {
$conf = $_GET['conf'];
if ($conf == 'yes') {
include ("../database.php");
connect();
doCommand("DELETE FROM tblstudent WHERE stCode=$id");
disConnect();
}
}
?>
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
I personally would put the javascript confirm in the original page instead of having to load or reload a page, detect a set variable, then confirm the action.
If a form submission is used, you can add the attribute onsubmit to your form element:
When the form is submitted the confirmation box will appear, if the user selects OK the form will carry out it's action, if the user selects CANCEL, the form will not be submitted and saving you from having to load and reload pages unnecessarily.
As Kieth pointed out, all of your PHP script is executed on the server, and only those things inside the <?php ?> tags that were echoed into the document will be passed to the client.
You absolutely need to understand that concept when using php.
HazardTW
Junior Poster in Training
71 posts since Sep 2007
Reputation Points: 37
Solved Threads: 3
Cripes! What happened to KISS (Keep It Simple Stupid)
May be I'm missing something? I am assuming link word delete is just an anchor tag?
So whats wrong with rendering it like this:
<a href="delete.php?id=1" onclick="return confirm('Are you sure');">Delete</a>
This way you never even leave List.php, unless the user selects 'Ok'.
Finally you should check the id in the query string for sql injection in delete.php and also check the request is from an authenticated and authorized user. Otherwise I can randomly splat students just pasting any id I like in my address bar or drop your whole DB.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
LOL I missed the "link" part, same idea only with a different tag ;) Had forms on my mind...
Love the avatar hollystyles.
HazardTW
Junior Poster in Training
71 posts since Sep 2007
Reputation Points: 37
Solved Threads: 3
Love the avatar hollystyles.
"The lab isss running at 100% effeeeeciency"
"Didi, Didi noooooooooooooooooo......"
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
this is the shortest way.
function rusure()
{
if (confirm("are you sure to delete this record?"))
{
return true;
}
else
{
return false;
}
}
if true, function will post the page, if not, do nothing.
i use this one and works perfect
While your method is not wrong, I must point out that it is not short.
You require a call to a function that calls a built in javascript function, then test the returned value of that function, and based on that test, your function returns true or false.
Since the javascript function 'confirm()' IS a function, and returns only true or false, ie. equates to only true or false, then as far as I know direct use of the function itself as a value is the shortest route which is exampled in three posts above in the form of inline element attributes "onClick = 'return confirm()'" and "onSubmit = 'return confirm()'"
Given the only two possible values for confirm(), replace it with the values and it looks like this:
"onClick = 'return true'" or "onClick = 'return false'".
Even if you did want to call your own function from the onclick attribute, you could shorten your function to one simple line: function rusure(){return confirm('are you sure');}
If you can see the redundancy in your code it should help you write leaner code :)
Hope this helps.
HazardTW
Junior Poster in Training
71 posts since Sep 2007
Reputation Points: 37
Solved Threads: 3