使用Ajax和PHP发送多选框值

时间:2019-01-22 12:42:56

标签: javascript php arrays ajax function

我的php和ajax代码有问题,所以我有此代码

<select id='absence_reason[]' name='absence_reason[]'>
     <option value='1'>Value 1</option>
     <option value='2'>Value 2</option>
     <option value='3'>Value 3</option>
     <option value='4'>Value 4</option>
     <option value='5'>Value 5</option>
     <option value='6'>Value 6</option>
</select>

<select id='absence_reason[]' name='absence_reason[]'>
     <option value='1'>Value 1</option>
     <option value='2'>Value 2</option>
     <option value='3'>Value 3</option>
     <option value='4'>Value 4</option>
     <option value='5'>Value 5</option>
     <option value='6'>Value 6</option>
</select>

现在我想使用AJAX和PHP发送这2个选择框的值: 好的,在Ajax中,我已经编写了这段代码,但是没有用

    function adding_multi_absences() {
      var reason = new Array();
      $("input[name='absence_reason[]']:selected").each(function() {
            reason.push( $(this).val() );
      });

      $.ajax({
          type: "POST",
          url: "include/ajax/adding_multi_absences.php",
          data: {
              add_multi_reason:reason
          },
          beforeSend: function () { // Before Send the data do somthing
             $(".loader").fadeIn();
             $(".showing").fadeIn();
          },
          success: function(data) { // If Process Success do somthing in our case will be show some data in browser
             $("#resultshow").fadeIn().html(data);
          },
          error: function(err) {  // if Process Error
            alert(err);
          },
          complete: function() { // After complete all Request Do Somting
            $('.loader').fadeOut();
              setTimeout(get_student_for_multi_absences,2000); // Refresh Every X Seconde
          }

      });
}

在我的PHP文件中,此代码

PHP文件

                            if (!empty($_POST['add_multi_reason'])) {
                                // this variabel have Multi Values From CheckBox Of Select Student  who Absences in Each Class
          $reason                      =  $_POST['add_multi_reason'];
                                $date_D                      =  date('d');
                                $date_M                      =  date('m');
                                $date_Y                      =  date('Y');
                                $hours                       =  date('H');
                                $mins                        =  date('i');
                                $date_now                    =  date('Y-m-d');
                                foreach ($absences as $student_code) {
                                    add_absences($student_code,$reason,$admin_school_code,$date_D,$date_M,$date_Y,$hours,$mins,$admin_token,$admin_permission);
                                }
                                echo show_alert("success",$Lang['success_operation']);
                        }else{

                                echo show_alert("warning",$Lang['no_absence_selected']);
                        }

我想在数据库中发送这2个选择框,但是每一个都插入一行谢谢

2 个答案:

答案 0 :(得分:0)

您只需要表格中的值属性名称。范例: 表格

<form id="login_form">
    <input type="text" name="username" id="a"/>
    <input type="password" name="password" id="b"/>
    <button type="button" onclick="login()">Submit</button>
</form>

JavaScript

$(document).ready(function(){});
function login(){
  var obj = $('#login_form').serialize();
  $.post('index.php', obj, function(res){
    alert(res);
  })
}

PHP-index.php

<?php
if(!empty($POST['username']) && !empty($POST['password'])){
  $user = $POST['username'];
  $pass = $POST['password'];
  $res = "Username : ".$user." || Password : ".$pass;
  return $res;
}
?>

如果将整个表单序列化为对象并传递URL,则将获得所有可能的值和选定的值。

提示:下面的代码将向您显示所有接收到的数据。您可以使用javascript发出警报。

<?php  
  print_r($_POST);
?>

答案 1 :(得分:0)

您必须稍微更改一下代码 1。这样便可以在数组中获得select值    $("select[name='absence_reason[]']").each(function() { reason.push( $(this).val() ); }) 2。然后对ajax原因数组进行字符串化     var data = {add_multi_reason: JSON.stringify(reason)}; $.ajax({ type: "POST", url: "include/ajax/adding_multi_absences.php", data: data, beforeSend: function () { // Before Send the data do somthing $(".loader").fadeIn(); $(".showing").fadeIn(); }, success: function(data) { // If Process Success do somthing in our case will be show some data in browser $("#resultshow").fadeIn().html(data); }, error: function(err) { // if Process Error alert(err); }, complete: function() { // After complete all Request Do Somting $('.loader').fadeOut(); setTimeout(get_student_for_multi_absences,2000); // Refresh Every X Seconde } }); 3。这就是您如何在php中获得两个选择的值     $feedback = json_decode($_POST['add_multi_reason']); 首先选择值var_dump($feedback[0]); 第二选择值var_dump($feedback[1]);