小编典典

如何使用jQuery将多个变量传递给PHP

ajax

好的,希望你们也能帮助我。我对jQuery和AJAX并不了解,也许我错过了一些非常简单的东西。我不能传递多个变量。

jQuery

$("#bodymes ul li span[contenteditable=true]").blur(function(){
            var weight_id = $(this).attr("id") ;
            var weightbody_mes = $(this).text() ;
            $.post( "../includes/bodymes_weight.php", { weightbodymes: weightbody_mes })  
                .done(function( data ) {   
                    $(".weightsuccess").slideDown(200); 
                    $(".weightsuccess").html("Weight Updated successfully"); 
                    if ($('.weightsuccess').length > 0) {
                        window.setTimeout(function(){
                            $('.weightsuccess').slideUp(200);
                        }, 4000);
                    }  
                    // alert( "Data Loaded: " + data);
            });
        });

因此,基本上,如果我运行此程序,它将运行正常,并且我的PHP脚本将对其进行处理。请注意,当我启用警报并显示加载的数据时,它会显示正确的信息(来自的信息weightbodymes),并且能够更新数据库。但是,一旦我添加了另一个变量,{ weightbodymes: weightbody_mes, weightid: weight_id }它就不会显示在警报框中加载的数据(如果我尝试显示来自两个变量的信息,它就起作用了,但仅向 PHP 提交了一个变量,即:

    $weightid = $_POST['weightid'];
    $weight = $_POST['weightbodymes'];

    if ($insert_stmt = $mysqli->prepare("UPDATE body SET body_weight=? WHERE body_id='$weightid'")) {
            $insert_stmt->bind_param('s', $weight);
            // Execute the prepared query.
            if (! $insert_stmt->execute()) {
                header('Location: ../index.php?error=Registration failure: INSERT nwprogram1');
            }
    }

希望您能告诉我我在哪里犯错误以及如何纠正它。先感谢您!


阅读 245

收藏
2020-07-26

共1个答案

小编典典

使用JS / JQUERY发布AJAX请求。 以下是我用于所有AJAX调用的语法。

var first = 'something';
var second = 'second';
var third = $("#some_input_on_your_page").val();


///////// AJAX //////// AJAX //////////
    $.ajax({
        type: 'POST',
        url:  'page_that_receives_post_variables.php',
        data: {first:first, second:second, third:third},
        success: function( response ){
            alert('yay ajax is done.');
            $('#edit_box').html(response);//this is where you populate your response
        }//close succss params
    });//close ajax
///////// AJAX //////// AJAX //////////

PHP页面page_that_receives_post_variables.php的代码

<?php
$first = $_POST['first'];
$second = $_POST['second'];
$third = $_POST['third'];

echo 'now do something with the posted items.';
echo $first; //etc...
?>
2020-07-26