hi,
I have two php file:
1. list.php
2. delete.php

On the list.php, there is a link word delete which refer to the delete.php and also send the id of the current record to the delete.php.

On the delete.php I have the following code:

<body>
<?php
//must ask for confirmation, now it not check it...
	if(isset($_GET['studentCode']))
	{
		$getCode = $_GET['studentCode'];
		?>
		<script language="javascript">
		var del = window.confirm('Are you sure deleting the selected record?');
		if(del==true)
		{
		<?php
			include ("../database.php");
			connect();
			doCommand("DELETE FROM tblstudent WHERE stCode=$getCode");
			disConnect();
			print(reDirect("list.php"));	
		?>
		}
		else
		{
			alert('Canceled Deletion');
		<?php
			print(reDirect("list.php"));
		?>
		}
		 </script>
<?php
	}	
?>
</body>

it deletes the seleced record which came from list, and the confirmation not appears...

PROBLEM:
I want the confirmation message appears before deleting the selected record, if yes, then delete, if no, then go back to list.php

you must echo also the javascript codes.

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();
}
}
?>

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: <form action="" onsubmit="return confirm('are you sure?')">

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.

Thanks all for replying,
I will go through the new code and suggestions...Then will post what happened...

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.

LOL I missed the "link" part, same idea only with a different tag ;) Had forms on my mind...

Love the avatar hollystyles.

Love the avatar hollystyles.

"The lab isss running at 100% effeeeeciency"

"Didi, Didi noooooooooooooooooo......"

hi,
I have two php file:
1. list.php
2. delete.php

On the list.php, there is a link word delete which refer to the delete.php and also send the id of the current record to the delete.php.

On the delete.php I have the following code:

<body>
<?php
//must ask for confirmation, now it not check it...
	if(isset($_GET['studentCode']))
	{
		$getCode = $_GET['studentCode'];
		?>
		<script language="javascript">
		var del = window.confirm('Are you sure deleting the selected record?');
		if(del==true)
		{
		<?php
			include ("../database.php");
			connect();
			doCommand("DELETE FROM tblstudent WHERE stCode=$getCode");
			disConnect();
			print(reDirect("list.php"));	
		?>
		}
		else
		{
			alert('Canceled Deletion');
		<?php
			print(reDirect("list.php"));
		?>
		}
		 </script>
<?php
	}	
?>
</body>

it deletes the seleced record which came from list, and the confirmation not appears...

PROBLEM:
I want the confirmation message appears before deleting the selected record, if yes, then delete, if no, then go back to list.php

<a href="delete.php?id=<?= stripslashes($feed); ?>" onclick="return confirm('Are you sure you want to delete this journal entry?')" class="style1">Delete Entry</a>

get confirmation message before getting to delete.php

Member Avatar for fatihpiristine

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.

Member Avatar for fatihpiristine

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 javascript method, then test the returned value of that method, and based on that test, your function returns true or false.

Since the javascript method 'confirm()' IS a function, and returns only true or false, ie. equates to only true or false, then as far as I know a direct test of the method itself 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()'"

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');}

Hope this helps.

thanks for the remark. i forgot to add it there

Thanks all of u for replying and guiding, from ur ideas I solved my problem...Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.