javascript & php problem

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

dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

javascript & php problem

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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....
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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";

?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You haven't posted anything to the script...

Code: Select all

<script language="javascript" type="text/javascript">
if (confirm('Click OK')) &#123;
    document.getElementById('testform').submit()
&#125;
</script>

<form id="testform" action="<?php echo $_SERVER&#1111;'PHP_SELF']; ?>" method="post">
<input type="hidden" name="test" value="test">
</form>

<?php

if (!empty($_POST&#1111;'test'])) &#123;
    echo 'You clicked OK';
&#125; else &#123;
    echo 'You don''t have javascript, didn''t click ok, haven''t run the script yet or this doesn''t even work!';
&#125;

?>
But personally I don't see what you're wanting to achieve....
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sorry... I'd missed out the second echo
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

This is better anyway...

Code: Select all

<?php
if (!empty($_GET&#1111;'test'])) &#123;
    echo 'You click ok and the var test is currently '.$_GET&#1111;'test'];
&#125; else &#123;
    echo '<script language="javascript" type="text/javascript">';
    echo "if (confirm('Yes or No?')) &#123;\r\n";
    echo "window.location = '?test=Somewordorother'\r\n";
    echo "&#125;\r\n";
    echo '</script>';
&#125;
?>
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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() &#123;
    if (confirm('Click OK to submit the form!')) &#123;
       return true //Submit
    &#125; else &#123;
        return false //Don't submit
    &#125;
&#125;
</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.
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can use the onClick event handler in the button tags. I'll leave the rest to your imagination ;-)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

POST_$ isn't a valid variable name. Remember to quote your named array indices.
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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.
Post Reply