必承其重 | 欲带皇冠 2013-07-19 05:49 采纳率: 50%
浏览 24

在JavaScript中调用php

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>";
?>
  • 写回答

1条回答 默认 最新

  • DragonWar% 2013-07-19 05:54
    关注

    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.

    评论

报告相同问题?