Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ajax在mysql中插入数据后无法刷新div_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Php 使用ajax在mysql中插入数据后无法刷新div

Php 使用ajax在mysql中插入数据后无法刷新div,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,有三个页面pricing.php、disc.php、disc_save.php。php是我的主要表单,因此它包含使用ajax发布数据的脚本 pricing.php <script> // button #disc_save_amt is present in disc.php $('#disc_save_amt').live('click', function(){ $.ajax({ url:"disc_save.php",//u

有三个页面pricing.php、disc.php、disc_save.php。php是我的主要表单,因此它包含使用ajax发布数据的脚本

pricing.php

<script>
// button #disc_save_amt is present in disc.php

    $('#disc_save_amt').live('click', function(){ 

        $.ajax({
            url:"disc_save.php",//url to submit
            type: "post",
            dataType : 'json',
            data : {
            'discount_type' : $('#discount_type').val(),
            'discount' : $('#disc_in').val()    
        },
        success: function(){}

        });

    return false;
    });

</script>

//disc.php中有按钮#disc_save_amt
$('#disc_save_amt').live('click',function(){
$.ajax({
url:“disc_save.php”//url提交
类型:“post”,
数据类型:“json”,
数据:{
‘折扣类型’:$(‘折扣类型’).val(),
'折扣':$('disc#u in').val()
},
成功:函数(){}
});
返回false;
});
php包含一个按钮,单击该按钮后,数据将通过该按钮插入数据库

<div id="disc_display">
    <form method="post" id="disc_data" action="">

        <table>
            <tr>
                <td>
                    <select name="discount_type" id="discount_type">
                        <option selected="selected" value="percent">Percent</option>
                        <option value="fixed">Fixed</option>
                    </select>

                </td>

                <td>
                    <input type="text" value="0" id="disc_in" name="disc_amt"/>
                </td>
            </tr>

            <tr>
                <td>
                    <input type="button" value="save" name="disc_save_amt" id="disc_save_amt" 
                     style="width:auto; height:auto; font-size:12px" />
                </td>

                <td>
                     <button name="cancel" id="cancel" onclick="loadXMLDoc_disc_cancel()">cancel</button>
                </td>
            </tr>
        </table>

    </form>
</div>

百分比
固定的
取消
下面是disc_save.php

<?php

    //$total= $_SESSION['total'];

    $discount_type= $_POST['discount_type'];
    $discount= $_POST['discount'];


    $con = mysqli_connect("localhost","root","","my_db");
    $run= "INSERT INTO discount(discount_type,discount) VALUES('$discount_type','$discount')";
    mysqli_query($con,$run) or die(mysqli_error($con));


?>

如何在不刷新的情况下检索指定div中的结果???

稍微修改一下脚本,
disc\u save.php

$con = mysqli_connect("localhost","root","","my_db");
$run= "INSERT INTO discount(discount_type,discount) VALUES('$discount_type','$discount')";
mysqli_query($con,$run) or die(mysqli_error($con));

// Return output to the ajax request
echo json_encode(array('status'=>'y', 'discount_type'=>$discount_type, 'discount'=>$discount));
pricing.php
中对ajax请求进行少量修改:

$('#disc_save_amt').on('click', function(){ 

    $.ajax({
        url:"disc_save.php",//url to submit
        type: "post",
        dataType : 'json',
        data : {
          'discount_type' : $('#discount_type').val(),
          'discount' : $('#disc_in').val()    
        },
        success: function(response){

           console.log(response);
           // Play with the response accordingly
           if( response.status == "y" ) {

              // Do whatever you want:
              // response.discount_type - Discount Type
              // response.discount      - Discount

              // This will replace your <div id="disc_display"></div> with response
              $('#disc_display').html(response.discount_type);
           }
       }

    });
});
$('disc\u save\u amt')。在('click',function(){
$.ajax({
url:“disc_save.php”//url提交
类型:“post”,
数据类型:“json”,
数据:{
‘折扣类型’:$(‘折扣类型’).val(),
'折扣':$('disc#u in').val()
},
成功:功能(响应){
控制台日志(响应);
//相应地处理响应
如果(response.status==“y”){
//做你想做的事:
//响应.折扣类型-折扣类型
//回复:折扣-折扣
//这将替换为您的响应
$('#disc_display').html(响应.折扣类型);
}
}
});
});

您应该在disc_save.php中创建html设计,并将其附加到div中。Ajax将返回该html响应。您可以将该html分配给Success元素下的特定div。像这样
$(“#disc_display”).html(响应)


首先,您需要从php代码返回一个状态为OK的JSON对象。您可以将想要返回的数据添加到该JSON对象中,并从ajax调用中的success函数访问它。为了更安全,我修改了你的php

PHP

<?php

$discount_type= $_POST['discount_type'];
$discount= $_POST['discount'];

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Insert rows */
$stmt = $mysqli->prepare("INSERT INTO discount(discount_type, discount) VALUES(?, ?)");
$stmt->bind_param('ss',$discount_type, $discount);
$stmt->execute();

if ($mysqli->affected_rows==1){
    echo json_encode(array(
        'status'=>'OK', 
        'discount_type'=>$discount_type, 
        'discount'=>$discount
    ));
} else {
    echo json_encode(array(
        'status'=>'Error',
    ));
}

$stmt->close();

您只是根据外观进行插入,而没有返回任何要添加到表中的内容?您可以将其添加到ajax调用的
success
函数中。e、 g.
$('#disc_data table tr').first().append('somenewhtml here')
refresh div是什么意思?
$(“#disc_save_amt”)。live()
不推荐使用
$(“#disc_save_amt”)。on()
@SamyMassoud我想用单击“save”按钮时发布的结果替换它。@RobSchmuecker我尝试过返回内容,但没有成功,这就是为什么我只发布代码用于插入。我写了$(“#disc_display”).html(response.disk);在if条件内,但未获得o/p。@user3164770通过firebug确认输出,您的
$(“#disc_display”)在哪里
DOM元素?@user3164770确保输出,我看到
#disc_display
包含您的表单,输出将用结果替换您的表单。不,只有数据被插入。#disc_display没有被结果替换。不,没有结果…只有数据被插入!!
<?php

$discount_type= $_POST['discount_type'];
$discount= $_POST['discount'];

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Insert rows */
$stmt = $mysqli->prepare("INSERT INTO discount(discount_type, discount) VALUES(?, ?)");
$stmt->bind_param('ss',$discount_type, $discount);
$stmt->execute();

if ($mysqli->affected_rows==1){
    echo json_encode(array(
        'status'=>'OK', 
        'discount_type'=>$discount_type, 
        'discount'=>$discount
    ));
} else {
    echo json_encode(array(
        'status'=>'Error',
    ));
}

$stmt->close();
$('#disc_save_amt').click( function(){ 

    $.ajax({
        url:"disc_save.php",//url to submit
        type: "post",
        dataType : 'json',
        data : {
        'discount_type' : $('#discount_type').val(),
        'discount' : $('#disc_in').val()    
    },
    success: function(data){}
        if (data.status == "OK") {
            $('#disc_display').append(data.discount_type + ' : ' + data.discount);
        } else {
            $('#disc_display').prepend('Something went wrong!');
        }
    });

return false;
});