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条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法