duanfu7004 2014-08-22 12:02
浏览 71
已采纳

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

I have written following script on server-

<?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); 
?>

Above script returns one JSON object which is useful for me in JavaScript section of my JSP page, but I don't know how to call php-script from the java script section, so need your guidance for the same. Hope you understand what I'm saying Thank you..!

  • 写回答

2条回答 默认 最新

  • dongyishe6689 2014-08-23 06:22
    关注

    Finally I solved it using AJAX as follows, I don't know whether performance wise this method is correct or not, but perfectly worked for me. Write the following code in your JavaScript section.

    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.)
    
            }
    

    And finally create event to call "getData()" function. That's it. Thank you..!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费