Page 1 of 1

PHP read before javascript ????

Posted: Mon Nov 20, 2006 12:47 pm
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

Posted: Mon Nov 20, 2006 1:00 pm
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.

Posted: Mon Nov 20, 2006 1:21 pm
by reecec
so i would have to make the js redirect to a seperate php page

Posted: Mon Nov 20, 2006 1:28 pm
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
}

Posted: Mon Nov 20, 2006 1:37 pm
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.

Posted: Mon Nov 20, 2006 4:12 pm
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