JavaScript数组未使用Ajax发布到PHP文件

时间:2019-04-19 12:23:25

标签: javascript php jquery html ajax

我有一个JavaScript数组,该数组来自html形式的Checkboxes。我正在使用name =“ DayChosen []”将数据放入可以传递给Ajax的数组中。我知道数据在登录到控制台时会进入数组,但是在PHP页面上似乎丢失了。我正在尝试收集选定的项目并打开一个新页面并将数据发布到此页面。

我尝试将其编码为json,然后使用PHP对其进行解码,但是我一直得到一个空值。我也尝试过使用JSON,但仍然无法正常工作。

我使用var_dump来发现我返回的是空值。

   <form> 
    <input type = "checkbox" class="CourseDay" name = "DayChosen[]"  value = "Legal"><i class="checkbox-pposition DayChosen">Legal Module</i><br>        
    <input type = "checkbox" class="CourseDay" name = "DayChosen[]"  value = "Day 1"/><i class="checkbox-pposition DayChosen">Day 1</i><br>
    <input type = "checkbox" class="CourseDay" name = "DayChosen[]"  value = "Rodent"/><i class="checkbox-pposition DayChosen">Day 2 Rodent</i><br>
    <input type = "checkbox" class="CourseDay" name = "DayChosen[]"  value = "Large animal"><i class="checkbox-pposition DayChosen">Day 2 Large animal </i><br>
    <input type = "checkbox" class="CourseDay" name = "DayChosen[]"  value = "Aquatic"><i class="checkbox-pposition DayChosen">Day 2 Aquatic</i><br>
    <input type = "checkbox" class="CourseDay" name = "DayChosen[]"  value = "Wildlife"><i class="checkbox-pposition DayChosen">Day 2 Wildlife</i><br>

<select class="Register-Multiple form-control">
     <option disabled selected value > -- Select number of Students to Register -- </option>
  <?php 
      for ($x = 1 ; $x <= 10; $x++) {
         echo '<option value = '.$x.'>'.$x.'</option>';
      } 
  ?>
</select>

     <input type="submit" id = "Register" class="col-6 text-center btn btn-primary Register" value="Register">
  </form>



  <script type="text/javascript">




  $('.Register-Multiple').on('change', function (e){



var courses = [];
$("input[type=checkbox]:checked").each ( function() {
      courses.push($(this).val());
      console.log($(this).val());

});


$.ajax({
       type: "POST",
       url:  '../../wp-content/themes/traffica/Multiple- 
registration.php',
       data: {courses:courses}, // serializes the form's elements.
       success: function(data)
       {

          console.log(data);

              window.open('../../wp- 
content/themes/traffica/Multiple-registration.php');
       }

     });

});     

2 个答案:

答案 0 :(得分:2)

您缺少更改功能的右括号

请添加on change功能的右括号

然后请添加thees代码并运行它

$('.Register-Multiple').on('change', function (e){
    var courses = [];
    $("input[type=checkbox]:checked").each ( function() {
       courses.push($(this).val());
       console.log($(this).val());

     });

      console.log(courses);

       $.ajax({
           type: "POST",
           url:'sample.php',
           data: {courses:courses}, // serializes the form's elements.
           success: function(data)
           {

                console.log(data);
           }

      });
});

使用php文件获取数组

print_r($_POST['courses']);

答案 1 :(得分:0)

数组可以通过两种方法传递

with open('foo') as f:
    while True:
        lines = f.readlines(8192)
        if not lines:
            break
        for line in lines:
            pass

在这种情况下,如果数组具有非UTF8字符串或其他问题,则json可能会失败,那么您可以使用序列化插入 如果您喜欢json,则可以将char设置为UTF8
尝试一下并检查error.log是否有任何输出