Rishi Reddy
Rishi Reddy

Reputation: 123

Call php inside javascript

Hi friends I have a PHP file as Shown below and I need to call a stored procedure inside javascript ie When i confirm yes I need to call the stored procedure . How should I include the following php code in IF loop of js(ie when i confirm yes)

This is the stored procedure:

 $sql = "CALL procedure(?);";
 $parameters = array($buyer_user_id);
 $query = $this->db->query($sql, $parameters);

This is the PHP file:

<?php
echo "<script>if (confirm('are u sure ?')) {";
echo "}";
echo " else {";
echo "}</script>";
?>

Upvotes: 0

Views: 1150

Answers (1)

Shawn31313
Shawn31313

Reputation: 6052

You need to use AJAX:

<script type="text/javascript">
    if (confirm('Are you sure?')) {
         var request = window.XMLHttpRequest() ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

         request.open("POST", "phpfile.php", true);
         request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         request.onreadystatechange = function () {
             if (request.readyState == 4 && (request.status == 200 || request.status == 0 /*Fixes a FF bug*/)) {
                 alert(request.responseText); // should return "example1-example2"
             }
         }
         request.send("data1=example1&data2=example2"); // you can send data here
    }
</script>

phpfile.php:

<?php
     return $_POST['data1'] . "-" . $_POST['data2'];
?>

I really dislike how complicated traditional AJAX is compared to using a library, but hope this gave you the idea.

Upvotes: 2

Related Questions