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

我有一个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);




桃花长相依
浏览 132回答 2
2回答

MMMHUHU

您缺少更改功能右括号请添加on change功能的右括号并且请添加代码并运行$('.Register-Multiple').on('change', function (e){&nbsp; &nbsp; var courses = [];&nbsp; &nbsp; $("input[type=checkbox]:checked").each ( function() {&nbsp; &nbsp; &nbsp; &nbsp;courses.push($(this).val());&nbsp; &nbsp; &nbsp; &nbsp;console.log($(this).val());&nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp; console.log(courses);&nbsp; &nbsp; &nbsp; &nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url:'sample.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data: {courses:courses}, // serializes the form's elements.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;success: function(data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; });});和php文件用于获取数组print_r($_POST['courses']);

当年话下

数组可以通过两种方法传递//first is serialize$result = serialize($array);//second method&nbsp;$result = json_encode($array);在你的情况下,如果阵列具有非UTF8字符串或其他一些问题,那么您可以使用连载insted的如果你喜欢JSON,所以你可以检查字符集为UTF-8 JSON可能会失败,给它尝试和检查error.log中任何输出
打开App,查看更多内容
随时随地看视频慕课网APP