Multiply复选框发布php ajax返回问题

时间:2015-08-17 22:27:17

标签: php jquery ajax post checkbox

有代码:

  //$(document).ready(function () {

  function call() {

    var msg = new Array();

    $("input[name='uservalues[]']:checked").each(function() {
      msg.push($(this).val());
    });
    console.log(msg); //first log
    $.ajax({
      type: 'POST',
      url: 'res2.php',
      dataType: 'html',
      data: msg,
      success: function(data) {
        console.log(data); //second log
        $('#results').html(data);
      },
      error: function(xhr, str) {
        alert('Error: ' + xhr.responseCode);
      }
    });
    console.log(msg); //third log
  }
  //});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<form method="POST" id="formx" action="javascript:void(null);" onsubmit="call()">
  <input class="uservalues" type="checkbox" name="uservalues[]" value="9001" checked>user1
  <br>
  <input class="uservalues" type="checkbox" name="uservalues[]" value="9002" checked>user2
  <br>
  <input class="uservalues" type="checkbox" name="uservalues[]" value="9003">user3
  <br>
  <input class="uservalues" type="checkbox" name="uservalues[]" value="9011">user11
  <br>
  <input class="uservalues" type="checkbox" name="uservalues[]" value="9087" checked>user87
  <br>
  <input class="uservalues" type="checkbox" name="uservalues[]" value="9089">user89
  <br>
  <input class="uservalues" type="checkbox" name="uservalues[]" value="9096">user96
  <br>
  <input class="uservalues" type="checkbox" name="uservalues[]" value="9097">
  <br>
  <input type="submit" class="btn btn-default btn-lg" value="Send">
</form>
<div id="results">output</div>

我的res2.php

<?php
print_r ($_POST);
?>

所以问题,在console.log拳头和第三个 - 我有["9001", "9002", "9087"] 但在第二个日志和回复后我有

Array ( [undefined] => )

当使用结束标记取消注释$(document).ready函数时 - 我有错误 -

  

未捕获的ReferenceError:未定义调用

1 个答案:

答案 0 :(得分:0)

我认为你必须传递这样的名字:

$_POST['uservalues'];

您的功能必须在脚本中。

<script>
    //your function
</script>