du5591 2013-06-16 16:52
浏览 38
已采纳

使用php调用db并在js变量中保存值

I am trying to load the values from mysql using php and js. Following is function which called when a button is clicked.

<script type="text/javascript">
function ab(){ 
   var MyJSNumVar = "<?php
   $orderid = $_GET['orderid'];
   $result="";
   $db = mysqli_connect("localhost", "root", ""); 
   mysqli_select_db($db, "mydba"); 
   $sql = "SELECT amount FROM mydba.fl_placed_order where orderid='".$orderid."'"; 
   $result = mysqli_query($db, $sql) or die(mysqli_error($db)); 
   while ($info = mysqli_fetch_array($result)) { 
      $result = stripslashes($info['orderid']); 
   } 
   echo $result;

?>";
}
</script>

Body code is

<button onclick="ab()">mittal</button>

In browser the page is empty with no error in console. Even i cannot see the button. What's wrong in this code...

Based on your expert advice i changed my code....

<script type="text/javascript">

function ab(){ 
$.ajax({
            type: 'get',
            url: "javascript.php/orderid=CF450AA4",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg, status) {
                alert("successful");
                console.log(msg);
            },
            error: function (msg, status) {
                console.log("failure");
                console.log(msg);
                alert("failure");
            }
        });
}
</script>

and my javascript.php file is

<?php
$orderid = $_GET['orderid'];
$result="";
$db = mysqli_connect("localhost", "root", ""); 
mysqli_select_db($db, "givem6la_shaleenmittal"); 
$sql = "SELECT amount FROM givem6la_shaleenmittal.fl_placed_order where orderid='".$orderid."'"; 
$result = mysqli_query($db, $sql) or die(mysqli_error($db)); 
while ($info = mysqli_fetch_array($result)) { 
    $result = stripslashes($info['orderid']); 
} 
echo $result;
?>

Response is
Notice: Undefined index: orderid in C:\xampp\htdocs\javascript.php on line 2

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\javascript.php on line 11

I have no clue what is this...

  • 写回答

3条回答 默认 最新

  • dpn68721 2013-06-16 17:12
    关注

    For dataType: "json", the required result should be sent as json values from php script. You can use json_encode() in php to convert your result to json. In your script change it to

    echo json_encode($result);
    

    Also in your javascript

    $.ajax({
                type: 'get',
                url: "javascript.php",
                data: "orderid=CF450AA4", //add this line
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    alert(response);
    
                },
                error: function (response) {
                    console.log("failure");
                    console.log(response);
                    alert("failure");
                }
            });
    

    Update: Also modify the line $info = mysqli_fetch_array($result) to $info = mysqli_fetch_array($result,MYSQLI_ASSOC) in your db script. And also

    $sql = "SELECT amount,orderid FROM givem6la_shaleenmittal.fl_placed_order where orderid='$orderid'";
    
    
    while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC)) { 
        $final_result = stripslashes($info['orderid']); 
    } 
    echo json_encode($final_result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址