javascript & php problem
Moderator: General Moderators
-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
javascript & php problem
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?
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?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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....
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....
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
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
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";
?>
<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";
?>
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
You haven't posted anything to the script...
But personally I don't see what you're wanting to achieve....
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!';
}
?>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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>';
}
?>-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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?
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
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?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
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.
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>-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
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?
<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?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
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:
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.
for example:
Code: Select all
<input type = "submit" name = "delete" value = "delete" onclick = "confirmDelete.html">it worked when i had onlcick equal to some function in the script, like the way you specified.