PHP read before javascript ????

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

PHP read before javascript ????

Post by reecec »

Hi,

Does PHP get read before the javascript

As my confirm box doesnt display it just does the PHP straight away a deletes the table

this is whats shown
Pleaseeeee function disp_confirm() { var r=confirm("Press a button") if (r==true) { document.write("You pressed OK!
Droped DB
i think the php is stoping the javascript as in my php it has the die function. Is this is what is being read and stops the js??

heres the code:


Code: Select all

<script type="text/javascript">

function disp_confirm()
  {
  var r=confirm("Are you sure you want to delete")
  if (r==true)
    {

document.write("<?

$sql = "DROP TABLE `$tabletodrop`";
$dosql=mysql_query($sql);
if ($dosql) 
{
$logedinuser=$_SESSION['user'];
$month=date('F Y');
$day=date('l dS');
$time=date('h:i:s A');
$addtolog="INSERT INTO net_db_log (`username`, `log`, `time`, `day`, `month`) VALUES ('$logedinuser', 'Deleted Database $tabletodrop', '$time', '$day', '$month')";
$doaddlog=mysql_query($addtolog);




die ("<br>Droped $tabletodrop");
}
else
{
echo "<br>Error Droping $tabletodrop it may not exist";
}

?>")
    }
  else
    {
    document.write("Canceled")
    }
  }
</script>



<form>
<input type="button" onclick="disp_confirm()" value="Display a confirm box">
</form>
thanks any help would be nice
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

php is server side which means it gets processed before it sends anything to the client (javascript is client side).

so php gets processed, sends info the client (in a form perhaps), the form gets processed (again perhaps) and is sent back to the server for further processing.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

so i would have to make the js redirect to a seperate php page
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

your document.write() method is not doing anything (on the client side). You can't simply mix php into javscript like that. You need to submit a form and check the action of the submit process (using php) and if a certain action was taken, then you process your delete:

ie:
untested

Code: Select all

<script>
function deleteIt()
{
  if(confirm("are you sure you want to delete...blah blah blah"))
     document.MyForm.submit();
}
</script>

<form method="post" name="MyForm" action="delete.php">
<input type="hidden" name="holder" value="1">
<input type="button" onClick="deleteIt()" value="delete">
</form>
delete.php

Code: Select all

if(isset($_POST['holder']))
{
  //....run delete stuff in here
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

as far as PHP is concerned, javascript is just text. There's NOTHING special about it... it means nothing special to PHP whatsoever.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

i have made it so when the delete link is clicked it opens a confirm box if the confirm it goes to a php ppage to proccess like you all said


just getting into javascript and wasnt sure how to put it with php thanks for your help
Post Reply