954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Javescript within PHP

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

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

you must echo also the javascript codes.

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

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
 

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

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

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
 

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

Delete Entry

get confirmation message before getting to delete.php

tomshanty
Newbie Poster
1 post since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 

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
 

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

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 

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

tanha
Posting Whiz in Training
218 posts since Aug 2007
Reputation Points: 8
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You