简体   繁体   English

无法使用Ajax将javascript数组传递给php

[英]Unable to pass javascript array to php using ajax

I have the following code to pass a Javascript array to PHP using ajax : 我有以下代码使用ajax将Javascript数组传递给PHP:

in HTML : 在HTML中:

echo "<input type=\"hidden\" id= \"que_id\" name= \"que_id[]\" value=".$questions['que_id'].">";

This is inside a loop. 这是一个循环。

in Javascript : 在Javascript中:

var que_id_array = new Array();
    $('input[name="que_id[]"]').each(function(){
       que_id_array.push($(this).val());
    });

AJAX Call : AJAX电话:

$.ajax({
    type:"POST",
    url: 'questionmastermodify.php',
    data: { que_id:que_id_array},
    success: function(data) {
        $('.my_update_panel').html(data);
        $('#overlay').fadeOut();
    }
});

in PHP : 在PHP中:

$que_id = $_REQUEST['que_id'];

echo count($que_id);    

The count displays 1 and not the size of the array, whereas in Javascript the console shows : 计数显示1,而不显示数组的大小,而在Javascript中,控制台显示:

console.log(que_id_array);

output : 输出:

["151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199", "200"]

I am stuck as i need this array in PHP but unable to pass this array from JS to PHP. 我陷入困境,因为我需要在PHP中使用此数组,但是无法将此数组从JS传递到PHP。

Thanks in advance.... 提前致谢....

Sandy505 Sandy505

I made a quick test with your code... and with a few changes, It work's for me. 我对您的代码进行了快速测试……并进行了一些更改,这对我来说很有效。
Take a look at your code, specially the loop when you create the <input> fields... 查看您的代码,特别是创建<input>字段时的loop ...

And also.. in the loop you have an ID in the <input> ... that's not good at all!.. change it for class for example... 而且..在loop您在<input>有一个ID ...根本不好!..例如,将其更改为class ...

As an example, this is what I tried: 例如,这是我尝试的:

Main PHP 主PHP

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
    <script type="text/javascript">
        jQuery(document).ready(function() {
            $('#theForm').submit(function(event) {
                event.preventDefault();
                createTheArray();
            });
        });
        function createTheArray(){
            // Create the Array
            var que_id_array = new Array();
            $('input[name="que_id[]"]').each(function(){
                 que_id_array.push($(this).val());
            });
            console.log(que_id_array); // output ["151", "152", "153"] 
            sendTheForm(que_id_array); // do the ajax call
        }
        function sendTheForm(que_id_array){
            // AJAX Call
            $.ajax({
                    type:"POST",
                    url: 'questionmastermodify.php',
                    data: { que_id:que_id_array},
                    success: function(data) {
                            $('.my_update_panel').html(data);
                    }
            });
        }
    </script>
</head>
<body>
    <form id="theForm">
        <?php
            // your original Array
            $arrayName = array(
                array('que_id' => '151'),
                array('que_id' => '152'),
                array('que_id' => '153')
            );
            foreach ($arrayName as $key => $questions) {
                echo "<input type='text' class='que_id' name='que_id[]' value='{$questions['que_id']}'>";
            }
        ?>
        <input type="submit" value="send" />
    </form>
    <div class="my_update_panel">result will be loaded here...</div>
</body>
</html>

And your questionmastermodify.php PHP file 和你的questionmastermodify.php PHP文件

<?PHP
    $que_id = $_REQUEST['que_id'];
    echo '<pre>';
    print_r($que_id);
    echo '</pre>';
?>

Result 结果

After form submit .. the HTML will print out: form submit ..之后,HTML将打印出来:

<pre>Array
(
    [0] => 151
    [1] => 152
    [2] => 153
)
</pre>

And in the console.log(); 并在console.log();

["151", "152", "153"]

Give a try and good luck! 试试看,祝你好运!

You could encode the array into JSON: 您可以将数组编码为JSON:

data: { que_id: JSON.stringify(que_id_array)},

In the PHP, decode it: 在PHP中,对其进行解码:

$que_id = json_decode($_REQUEST['que_id']);
echo count($que_id);  

The problem has been sorted out : 问题已解决:

The culprit was the version of the jquery i was using. 罪魁祸首是我使用的jQuery版本。 I was using jQuery JavaScript Library v1.3.2 - which caused this problem. 我正在使用jQuery JavaScript Library v1.3.2-导致此问题。

Using the latest : jquery-1.9.0.min.js solve the problem. 使用最新的:jquery-1.9.0.min.js解决了这个问题。

I am accepting the answer provided by #gmo as it brought me near to the problem solving and also about that helpful tip about not using id attribute in the Loop... 我接受#gmo提供的答案,因为它带给我接近解决问题的能力,并且还提供了有关在循环中不使用id属性的有用提示...

Thanks all... 谢谢大家

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

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