1

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.

<?php
....
.....
......
if($_POST["action"]=="xyz"){
$myname = "asim";
    echo "<script type='text/javascript'>submitform();</script>";
}
.....
....
...
gotoanotherpagefinally();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
    function submitform(){
        alert("<?php echo $myname; ?>");
        $.ajax({
                type: 'POST',
                data: {
                    action: 'whatever',
                    fileID: "<?php echo $myname; ?>",
                },
                url: 'http://xyz.com/API/query.php'
            });
    }
</script>
</head>
<body>
<form id="myform">
  ------
  ------
<input type="submit" name="submit_details">
</form>
</body>
</html>

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.

4 Answers 4

1

You can catch the submit event using jQuery:

$("#myform").submit(function(e) {
  // needed so the default action isn't called 
  //(in this case, regulary submit the form)
  e.preventDefault(); 

  $.ajax(...);
});
Sign up to request clarification or add additional context in comments.

2 Comments

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.
This won't work, as php is server side, and js is client side. You can render the parameters using php, but once it hits the client, you are done.
1

you can use :

<form id="myform" onsubmit="submitform()">

1 Comment

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.
0

You can use jQuery to do this.

$(':input').click(function(){});

then inside this function you can use an ajax request to send the variable to php page that you have.

var variableA, variableB; // variables to be initiated by an ajax request

// ajax request to get variables from php page
$.ajax(
{
  url:'php_page_path/source_page.php',
  data:"message=getVars",
  type: 'post',
  success: function(data){
  // use the data from response
    var obj = JSON.parse(data);
    variableA = obj.varA;
    variableB = obj.varB;
  }
});


// use the variables in this ajax request and do what you want
$.ajax(
{
  url:'php_page_path/page.php',
  data:"var1="+variableA+"&variableB="+vaiableB ,
  type: 'post',
  success: function(j){
    // use the data from response
  }
});

2 Comments

variable is not an input value, it is something my PHP script deduce from database.
then you need to use another ajax request, the first one to get these variables from a json response, and the other one to use these variables. I WILL EDIT MY ANSWER FOR WHAT I COMMENTED NOW
0

Please changes your code -

<?php
   $myname="asim";
?>
<script type="text/javascript">
    function submitform(myname){
        alert(myname);
        $.ajax({
                type: 'POST',
                data: {
                    action: 'whatever',
                    fileID: myname,
                },
                url: 'http://xyz.com/API/query.php'
            });
    }
</script>

HTML

<form id="myform" onsubmit="submitform('<?php echo $myname;?>')">
  ------
  ------
  <input type="submit" name="submit_details">

</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.