如何通过多个变量到PHP与jQuery [英] How to pass multiple variables to PHP with jQuery

查看:186
本文介绍了如何通过多个变量到PHP与jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好男人希望你能帮助我解决这个为好。我不是疯狂知识渊博与jQuery和AJAX,也许我失去了一些东西很简单。我不能够通过1个多变量。我曾尝试之间最接近的几个不同的事情,我有什么工作是<一个href="http://stackoverflow.com/questions/28091557/can-not-seem-to-pass-more-than-one-variable-with-jquery-to-mysql">this但即使没有工作。

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

所以基本上如果我运行这个它会完全正常工作,我的PHP脚本将会处理它。请注意,当我去,使提醒我,并显示数据加载它显示准确的信息(信息来自 weightbodymes ),我能够更新数据库。但只要我再添变数 {weightbodymes:weightbody_mes,weightid:weight_id} 它不会出现在警告框加载的数据(如果我尝试显示来自两个变量信息它的工作,但它只能提交1 VAR只为 PHP 是:

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!

推荐答案

使用JS / JQUERY 发表Ajax请求。下面是我用我所有的AJAX调用的语法。

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

$ C $下的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...
?>

这篇关于如何通过多个变量到PHP与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆