doushi4864 2013-02-03 22:02 采纳率: 100%
浏览 43
已采纳

AJAX / JQUERY没有更新MySQL行

I am currently trying to update a textarea when a user clicks elsewhere. I'm not well-versed in AJAX and Jquery. However, the script doesn't seem to be updating the row in the DB.

Jquery/Text area:

<textarea id="<?php echo $item_id; ?>_textarea"><?php echo $notes; ?></textarea>
    <script type="text/javascript">
    $('<?php echo $item_id; ?>_textarea').on('blur',function () {
var notesVal = $(this).val(), id = $(this).data('id');        
var itemVal = <?php echo $item_id; ?>;
$.ajax({
     type: "POST",
     url: "updateNotes.php",
     data: {notes:notesVal , id:id, itemId:itemVal},
     success: function(msg) {
         $('#'+id).html(msg);
     }
 })
});
</script>

updateNotes.php:

<?php

include('db_connect.php');
include('order_functions.php');



$email = $_SESSION['username'];

$cartId = getcartid($mysqli, $email);
$notes = $_POST['notes'];
$itemID = $_POST['itemId'];


$query = "UPDATE `rel` SET `notes` = '$notes' WHERE `cart_id` = '$cartId' && `id_item` = '$itemID'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

if(result) {
return "Notes Updated";

} ?>

  • 写回答

1条回答 默认 最新

  • dounao1875 2013-02-03 23:37
    关注

    You forgot the $ in your last if-statement in your php-code and you should use "echo" (or likewise) instead of "return" as you are not in a function.

    <?php
    
    include('db_connect.php');
    include('order_functions.php');
    
    $email = $_SESSION['username'];
    
    $cartId = getcartid($mysqli, $email);
    $notes = $_POST['notes'];
    $itemID = $_POST['itemId'];
    
    
    $query = "UPDATE `rel` SET `notes` = '$notes' WHERE `cart_id` = '$cartId' && `id_item` = '$itemID'";
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
    
    if($result) {
        echo "Notes Updated";
    }
    ?>
    

    Your html/javascript-code is a bit wrong. This is how i guess you wanted it to work:

    <div id="<?php echo $item_id; ?>_div">
        <textarea id="<?php echo $item_id; ?>_textarea" data-id="<?php echo $item_id; ?>"><?php echo htmlentities($notes); ?></textarea>
    </div>
    
    $('#<?php echo $item_id; ?>_textarea').on('blur', function () { // don't forget # to select by id
    
        var id = $(this).data('id'); // Get the id-data-attribute
        var val = $(this).val();
        $.ajax({
            type: "POST",
            url: "updateNotes.php",
            data: {
                notes: val, // value of the textarea we are hooking the blur-event to
                itemId: id // Id of the item stored on the data-id
            },
            success: function (msg) {
                $('#' + id + '_div').html(msg); //Changes the textarea to the text sent by the server
            }
        });
    });
    

    Good luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化