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

Ok guys hope you can help me with this one as well. I am not to crazy knowledgeable with jQuery and AJAX and probably I am missing something very simple. I am not able to pass more than 1 variable. I have tried several different things amongst closest to what I have working is this but even that didn't work.

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);
            });
        });

So basically If I run this it will work perfectly fine and my PHP script will process it. Notice that when I go and enable to alert me and show data loaded it shows correct info (info from weightbodymes) and I am able to update db. But as soon as I add another variable { weightbodymes: weightbody_mes, weightid: weight_id } it won't show data loaded in alert box (if I try to show info from both variables it's working but it only submits one var only to PHP which is:

    $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');
            }
    }

Hope you can tell me where I am making a mistake and how to correct it. THANK YOU IN ADVANCE!

dtn36013
dtn36013 我试过第一个和第三个答案,但似乎没有用。生病了多次。谢谢!
5 年多之前 回复
duandu1377
duandu1377 这应该有你想要的stackoverflow.com/questions/8191124/...
5 年多之前 回复
dqdmvg7332
dqdmvg7332 发送类似这样的内容{'data':{'var_1':'value1','var_2':'value2'}}。然后通过$_POST['data']['var_1'],$_POST['数据访问它']['VAR_2']
5 年多之前 回复
douyu9433
douyu9433 为数据对象添加更多属性:{weightbodymes:weightbody_mes,weightid:weight_id,foo:'bar'}
5 年多之前 回复

1个回答

POST AJAX REQUEST using JS / JQUERY. The below is the syntax I use for all my AJAX calls.

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 //////////

Code for the PHP page 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...
?>
dream12001
dream12001 我使用alert()并给了我正确的信息,但问题是PHP,因为我有if语句和我在这个问题中发布的部分是“else”的一部分,并且在if和else我放置相同的变量,而我尝试不同 解决方案,忘了改变它,所以它导致错误的记录更新和一堆其他混乱。 再次感谢你! @Stephen Carr
5 年多之前 回复
drob50257447
drob50257447 问题是什么? 对于调试,在你通过ajax中的post变量之前,只需提醒(your_variables),这样你就可以看到你是否真的得到了什么。 通常帮助我。
5 年多之前 回复
doudou32012
doudou32012 我让它与你的解决方案一起工作。 也投了票。 谢谢! @Stephen Carr
5 年多之前 回复
doupian6118
doupian6118 嗯...这似乎是一个解决方案,但由于某种原因,它仍然只能传递一个变量。 所以例如,在一个响应中,我只得到了weightbodymes,虽然我传递了两个变量,但我的数据行看起来像这样:data:{weightid:weight_id,weightbodymes:weightbody_mes,third:third},(我留下第三个元素,并指定变量为 好吧,但没有尝试用PHP调用它)。 还有其他建议吗?
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐