简体   繁体   English

如何将变量和数组变量一起提交给jQuery并返回给PHP?

[英]how to submit a variable along with array variable in jquery to php and return them?

I have created a form (for testing) which include text field with id and name attribute. 我创建了一个表单(用于测试),其中包括带有id和name属性的文本字段。 Also in the same form i have a check boxes which the user will check to submit to php. 同样以相同的形式,我有一个复选框,用户将检查该复选框以提交给php。 html form is as follows: html格式如下:

<form id="f1" action="chkbx.php" method="get">
    <input type="text" name="t1" id="txt" />
    <br>
    <input type=checkbox name="color[]" class="color" value="c1"/>
    <label>chk1</label>
    <hr>
    <input type="checkbox" name="color[]" class="color" value="c2"/>
    <label>chk2</label>
    <hr>
    <input type="checkbox" name="color[]" class="color" value="c3"/>
    <label>chk3</label>
    <hr>
    <input type="checkbox" name="color[]" class="color" value="c4"/>
    <label>chk4</label>
    <hr>
    <input type="button" id="btn" value="Submmit" name="sub" />
</form>

I have jquery to handle the form so that the text field is stored in a variable, and a loop that will go through the check boxes to see which box is checked and store its value in an array. 我有jquery来处理表单,以便将文本字段存储在变量中,还有一个循环,该循环将遍历复选框以查看选中了哪个框并将其值存储在数组中。 Everything work fine so far. 到目前为止一切正常。 In jquery i'm using ajax to pass the form to php file as follows: 在jquery中,我使用ajax将表单传递给php文件,如下所示:

$(document).ready(function(){
$("#btn").click(function(){
    var dest = $("#f1").attr("action");
    var meth = $("#f1").attr("method");
    var txt = $("#txt");
    var col = [];
    $(".color").each(function(){
        if ($(this).is(":checked")){
            col.push($(this).val());
        }
    });
    console.log(col); // indicate that all checked values inserted in the array
    $.ajax({
        data: {d1: col, d2: txt},
        url: dest,
        method: meth,
        success: function(res){
            alert(res);
        }
    });
});

}); });

Lastly, this is my code in php: 最后,这是我在php中的代码:

<?php

   $arrV = $_GET["d1"];
   $txtV = $_GET["t1"]; // i tried the variable name from jquery and tried d2 written in data section in ajax

   print_r($arrV);
   echo $txtV;

?> ?>

Please notice when i try to alert back each variable sepratley it works just fine (works with the array alone and the varable alone with tiny cahnges in ajax section and php). 请注意,当我尝试向每个变量sepratley发出警报时,它就可以正常工作(仅与数组配合使用,并且与ajax节和php中的小改动一起单独与变量配合使用)。 However when i try to retrieve them both it won't work. 但是,当我尝试同时检索它们时,将无法正常工作。 After several tries i got the following errors returned back from php (syntax_error undefined index, rangeError maximum call stack size exceeded) 经过几次尝试,我从php返回了以下错误(syntax_error未定义索引,超出了rangeError最大调用堆栈大小)

what am I doing wrong? 我究竟做错了什么? Please help! 请帮忙!

There are a number of flaws in the above code ... you're using <form> to submit AND $.ajax for one. 上面的代码中有很多缺陷……您正在使用<form>提交AND $.ajax I'd simplify as follows: 我将简化如下:

HTML 的HTML

<form id="f1">
    <input type="text" name="t1" id="txt">
    <br>
    <input type=checkbox name="color[]" class="color" value="c1"/>
    <label>chk1</label>
    <hr>
    <input type="checkbox" name="color[]" class="color" value="c2"/>
    <label>chk2</label>
    <hr>
    <input type="checkbox" name="color[]" class="color" value="c3"/>
    <label>chk3</label>
    <hr>
    <input type="checkbox" name="color[]" class="color" value="c4"/>
    <label>chk4</label>
    <hr>
    <input type="button" id="btn" value="Submmit" name="sub" />
</form>

JS JS

<script>
    function getInputs( el ){
        var values = {};
        $(el + ' :input').not('[type="button"]').each(function(){
            if ($(this).attr('type') == 'radio' && $(this).is(':checked')) {
                values[this.name] = this.value;
            } else if ($(this).attr('type') == 'checkbox') {
                values[this.name] = $(this).is(':checked') ? 1 : 0;
            } else {
                values[this.name] = this.value;
            }
        });
        return values;
    }

    $(function(){

        $("#btn").click(function(){
            var $data = getInputs('#btn');
            $.ajax({
                url      : 'chkbx.php',
                type     : 'POST',
                dataType : 'json',
                data     : {data: $data}
            });
            return false;
        });

    });
</script>

PHP (chkbx.php) PHP(chkbx.php)

<?php
    $data = $_POST['data'];
    echo print_r($data,true);
?>

The above will probably NOT work as-is, but should give you an idea of a cleaner approach that does work. 上面的内容可能无法按原样工作,但应为您提供一个更有效的更清洁方法的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM