faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

23 of 26 people (88%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

I need to get a result of confirn() (Javascript function) to use it in a PHP function, can I do it?

Mar 15th, 2008 17:52
ha mo, Alex Dean, Jon Bjorseth, Alex Essine,


No... PHP is serverside scripting while javascript is clientside...
E.g. PHP gets something to chew on, chews away, (all on the server) and 
sends the result bach to the browser as HTML, whereas the Javascript 
from a form or somrthing, would chew on whatever the user write in an 
input field before its even getting sendt to the server...
Hope this made a little sense anyways....
You could write JavaScript to save the results of your confirm() in a
hidden form field, then call form.submit() to send the form data (and
the results of your confirm() to your PHP script.
<form name="testForm" action="yourScript.php" method="POST">
  <input type="hidden" name="confirmField">
  <input type="button" value="Ask a Question" onClick="askQuestion();">
</form>
<script>
  function askQuestion()
  {
    //not sure what you're confirming, so I'll invent this.
    if(confirm('Does this make any sense?') == 1))
    {
      window.document.testForm.confirmField.value = "It makes sense.";
    } else {
      window.document.testForm.confirmField.value = "It makes no sense.";
    }
    window.document.testForm.submit();
  }
</script>
Your PHP could just be 
<? var_dump($HTTP_POST_VARS); ?> to see the result, or you could access
the submitted value with $HTTP_POST_VARS['confirmField'].
http://www.tantofa.com
http://www.fantofa.com
http://www.mantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.mozmar.com
http://www.yamot.com
http://www.templatestemp.com