title: How to call a javascript function from PHP at form submit event
body: <p>I want to call a javascript function from php at the form submit event. and that javascript function will access the php variables, send them to a php script on another website using ajax. The following code is just a representation.</p>
  
  <pre><code>&lt;?php
  ....
  .....
  ......
  if($_POST["action"]=="xyz"){
  $myname = "asim";
      echo "&lt;script type='text/javascript'&gt;submitform();&lt;/script&gt;";
  }
  .....
  ....
  ...
  gotoanotherpagefinally();
  ?&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;script type="text/javascript"&gt;
      function submitform(){
          alert("&lt;?php echo $myname; ?&gt;");
          $.ajax({
                  type: 'POST',
                  data: {
                      action: 'whatever',
                      fileID: "&lt;?php echo $myname; ?&gt;",
                  },
                  url: 'http://xyz.com/API/query.php'
              });
      }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="myform"&gt;
    ------
    ------
  &lt;input type="submit" name="submit_details"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I need to call the javascript function from php because the php variables taken by js function are only set by php itself and not the form values or something.</p>