是否可以从JSP页面的JavaScript部分调用PHP-Script ..?

时间:2014-08-22 12:02:56

标签: javascript php jsp

我在服务器上写了以下脚本 -

<?php

//Create an array
$json_response = array();


   // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();
    $status = "In Progress";

    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);

    if (!$con) 
    {
        die('Could not connect to database: ' . mysql_error());
    }

    //Query to select pending queries database
    $result = mysqli_query($con, "SELECT * FROM tbl_query_master WHERE status='".$status."' ORDER BY query_date DESC");

    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
    {        
        $row_array['query_id'] = $row['query_id'];
        $row_array['sender_mobile_no'] = $row['sender_mobile_no'];
        $row_array['sender_name'] = $row['sender_name'];
        $row_array['query_string'] = $row['query_string'];
        $row_array['action_taken'] = $row['action_taken'];        
        $row_array['status'] = $row['status'];
        $row_array['query_date'] = $row['query_date'];
        $row_array['action_date'] = $row['action_date'];
        $row_array['view_status'] = $row['view_status'];
        $row_array['read_status'] = $row['read_status']; 

        //push the values in the array

        array_push($json_response,$row_array);
    }    
   echo json_encode($json_response); 
?>

上面的脚本返回一个JSON对象,这对我在JSP页面的JavaScript部分有用,但我不知道如何从java脚本部分调用php-script,所以需要你的指导。希望你明白我在说什么谢谢你......!

2 个答案:

答案 0 :(得分:0)

假设您的php脚本已部署到Web服务器(带有php mod的apache)并由某个URL触发,例如: http://localhost/script.php

然后在您的javascript中,您可以使用jquery执行POSt请求:

$.getJSON('http://localhost/script.php', function(json) {
  // do what you need with your json data
});

答案 1 :(得分:0)

最后我使用AJAX解决了它,如下所示,我不知道这种方法的性能是否正确,但对我来说是完美的。在JavaScript部分中编写以下代码。

var xmlHttp;
        //FUNCTION TO CREATE BROWSER COMPATIBLE OBJECT.
        function createBrowserObject()  
        {               
            if (typeof XMLHttpRequest != "undefined")  //Object for Netscape 5+, Firefox, Opera, Safari,and Internet Explorer 7
            {                       
                    xmlHttp = new XMLHttpRequest();
            }
            else if (window.ActiveXObject)  //Version for Internet Explorer 5 and 6.
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
            }
            if (xmlHttp == null)         //Fails on older and nonstandard browsers
            {
                alert("Browser does not support XMLHTTP Request");
            }
        }

        function getDataChange() 
        {           
            if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") //Check whether server response came back with no errors.
            {
               //THE RESPONSE FROM SERVER. DO YOUR STUFF WITH xmlHttp.responseText
                alert("Responce= "+xmlHttp.responseText);
                //document.getElementById("cart_div").innerHTML = xmlHttp.responseText;

            }
        }

        function getData()
        {           
               createBrowserObject();//CREATE BROWSER COMPATIBLE OBJECT.// 

                var url = "http://yourdomain.com/your_script.php";  // URL of server-side resource.
                // Using following way you can send parameter to your script. 
                //url += "?param1=" + param1 + "&param2=" + param2;
                xmlHttp.onreadystatechange =getDataChange;  //ASSIGN RESPONSE HANDLER FUNCTION NAME TO ONREADYSTATECHANGE//
                xmlHttp.open("GET", url, true);   //INITIATE GET or POST REQUEST. (Here GET)
                xmlHttp.send(null);    // SEND DATA. (Always null in case of GET.)

        }

最后创建要调用的事件&#34; getData()&#34;功能。就是这样。谢谢..!