Page 1 of 2
javascript & php problem
Posted: Sun Feb 20, 2005 6:54 pm
by dannymc1983
would just like to know how to execute some javascript code within a php script. i know it is possible to place html and javascript within a php script eg. echo "<script language = 'javascript'>confirm('click yes or no')</script>";
what i would like to know in particular is how to get the result of a confirm box so i can use this handy feature in a php script. ive tried this piece of code which is definitely wrong but will give you an idea of what im trying to achieve:
<?
function confirm($text)
{
echo "<script language = \"javascript\">
if (confirm('$text'))
return 1;
else
return 0;
</script>";
}
?>
anyone know how to get this working?
Posted: Sun Feb 20, 2005 7:00 pm
by Chris Corbyn
You can't very easily pass javascript variables into PHP without sending some sort of submission back to the server.
You can however pass PHP vars into your javascript by just echoing them into the page between the <script> tags.
Looking at your code here I can't really see what you're trying to acheive. You must understand that all php is executed on the server so all you can do is write the Javascript but it won't execute until run in the client browser.
If you merely want the user to confirm something using javascript and then some php code to be executed you need the result of your confirm to be a redirection to the PHP script required....
Posted: Sun Feb 20, 2005 7:03 pm
by timvw
that is not going to work:
php generates html/javascript
---> that output is send to the client
----> that javascript output is executed by the client's browser
----> so there is no way to mix them like this
what you could do is send the result of the confirm dialog back to the server... and generate new output based on the result... this is usually done by calling submit() on a form, or with window.location
Posted: Sun Feb 20, 2005 7:19 pm
by dannymc1983
sorry im a bit of a beginner when it comes to php. i tried this piece of code but it didnt work. could it be modifed to work or is it completely wrong??
<script type="text/javascript">
var name = confirm("Press a button")
</script>
<?
$value = $_POST['name'];
if ($value)
echo "ok was clicked";
else
echo "cancel was clicked";
?>
Posted: Sun Feb 20, 2005 7:26 pm
by Chris Corbyn
You haven't posted anything to the script...
Code: Select all
<script language="javascript" type="text/javascript">
if (confirm('Click OK')) {
document.getElementById('testform').submit()
}
</script>
<form id="testform" action="<?php echo $_SERVERї'PHP_SELF']; ?>" method="post">
<input type="hidden" name="test" value="test">
</form>
<?php
if (!empty($_POSTї'test'])) {
echo 'You clicked OK';
} else {
echo 'You don''t have javascript, didn''t click ok, haven''t run the script yet or this doesn''t even work!';
}
?>
But personally I don't see what you're wanting to achieve....
Posted: Sun Feb 20, 2005 7:32 pm
by Chris Corbyn
Sorry... I'd missed out the second echo
Posted: Sun Feb 20, 2005 7:40 pm
by Chris Corbyn
This is better anyway...
Code: Select all
<?php
if (!empty($_GETї'test'])) {
echo 'You click ok and the var test is currently '.$_GETї'test'];
} else {
echo '<script language="javascript" type="text/javascript">';
echo "if (confirm('Yes or No?')) {\r\n";
echo "window.location = '?test=Somewordorother'\r\n";
echo "}\r\n";
echo '</script>';
}
?>
Posted: Sun Feb 20, 2005 7:54 pm
by dannymc1983
thanks for those. i couldnt get them working though. I dont really understand your second solution at all! for your first solution all i get is the output: "You don''t have javascript, didn''t click ok, haven''t run the script yet or this doesn''t even work!"
is it working for you or do you know why it might not be working?
just to answer your question about why i would want to use something like this: I just think that a pop up box asking a user if he does or does not want to do something is the easiest way of confiming an action in a webpage. for example if a user wanted to delete all his emails a pop up box would appear asking the user if he was sure he wanted to do this. and to make pop boxes you need javascript, as far as im aware.
Posted: Sun Feb 20, 2005 8:01 pm
by Chris Corbyn
To your first question the code initally posted does the same for me.
The second code just demonstrated that there are means and way of PHP knowing if the user confirmed the javascript prompt (although the filters on this site turned the letter R into the word 'are' so that needs to be changed but it does work.
Following on from what you were saying about confirming before deleting emails etc... that's just pure javascript... don't submit the info to the server if the user doesn't confirm... there's no need for PHP to parse it otherwise.
I assume you know how to validate forms with javascript and prevent their submission right?
Posted: Sun Feb 20, 2005 8:10 pm
by dannymc1983
no, not familiar with javascript at all, just started looking through some of its pop up features. i am failrly familiar with forms though. with regards to what you were saying: "don't submit the info to the server if the user doesn't confirm". yes thats exactly what im looking to do. would you have a simple example?
Posted: Sun Feb 20, 2005 8:18 pm
by Chris Corbyn
Yeah it's reasonably straight forward. The idea is that we either want to submit the form (TRUE) or not submit it (FALSE). So we call a function the chooses which action to take onSubmit.
Code: Select all
<script language="javascript" type="text/javascript">
function checkForm() {
if (confirm('Click OK to submit the form!')) {
return true //Submit
} else {
return false //Don't submit
}
}
</script>
<form name="test" action="somefile.php" method="post" onSubmit="return checkForm()">
Enter a word <input type="text" name="word">
<p>
<input type="Submit" value="Submit">
</form>
The form asks the function checkForm() to return a value of true or false depending upon what the user choses. If false is the outcome the submission fails.
Posted: Sun Feb 20, 2005 8:41 pm
by dannymc1983
ok thanks, i understand that. but say if i have a form that contains two submit buttons, like an update button and a delete button. the form might look something like this:
<form method = "post">
<input type = "text" name = "myname", value = $myname>
<input type = "submit" name = "update" value = "update">
<input type = "submit" name = "delete" value = "delete">
</form>
my php script would then be something like this:
<?
if (isset(POST_$[delete]))
//some functions to delete my name
if (isset(POST_$[update]))
//some functions to update my name
?>
how would i create a sperate confirm box when either button is pressed. like when delete is pressed a box appears saying: are you sure you want to delete your name?
and when update is pressed a box appears saying: are you sure you want to update your name?
Posted: Sun Feb 20, 2005 8:47 pm
by Chris Corbyn
You can use the onClick event handler in the button tags. I'll leave the rest to your imagination

Posted: Sun Feb 20, 2005 9:06 pm
by feyd
POST_$ isn't a valid variable name. Remember to quote your named array indices.
Posted: Sun Feb 20, 2005 9:31 pm
by dannymc1983
ok thanks for that. i tried it and it worked. one other thing i tried was to open a new file whenever one of the submit buttons is pressed
for example:
Code: Select all
<input type = "submit" name = "delete" value = "delete" onclick = "confirmDelete.html">
this didnt work for some reason. would just like to know is it even possible???
it worked when i had onlcick equal to some function in the script, like the way you specified.