使用ajax从php获取记录并更改ID属性

时间:2018-11-25 11:04:21

标签: php ajax

我有一个ajax和php,如下所示,但它没有更改ID为#respo的html属性的值

需要任何修改吗?

$.ajax({
                        type:"POST",
                        url:"<?php echo base_url();?>/shortfiles/loadans.php",
                        dataType: "json",
                        data: {reccount: reccount},
                        success: function(response) {
                         var response = ($response);
                                $("#respo").text(response);                  
                       },


                    })      

和php

<?php
$id = $_POST['reccount'];

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "testsite");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt update query execution
$sql = "SELECT response from paper WHERE ID=$id";
$result=mysqli_query($link, $sql);

while ($row = mysql_fetch_row($result)) {

    $response => $row['response'];


}

echo json_encode($response);






// Close connection
mysqli_close($link);
?>

我想为ID为respo的html元素分配一个响应值

1 个答案:

答案 0 :(得分:0)

您的代码必须看起来像

$.ajax({
        type:"POST",
        url:"<?php echo base_url();?>/shortfiles/loadans.php",
        success:function(data){
        var obj = jQuery.parseJSON(data);
        document.getElementById('elementName').value = obj.varaibleName;
        }
    });