ajax替换后的PHP空字段

时间:2018-06-12 10:17:32

标签: php jquery ajax

我有几个输入和两个选择的表单。当我选择品牌时,我想看模特选项。一切正常。但是当我点击提交时,vehicle_model字段为空。除了vehicle_model字段之外,$ _POST中的字段已填充。我不知道为什么。

<div class="form-group">
  <select class="form-control brand-select" name="brand" required="">
    <option selected="true" value="1">MAN</option>
    <option value="2">Volvo</option>
    <option value="3">Renault</option>
    <option value="4">DAF</option>
  </select>
</div>

<div class="form-group" id="ajax_model">
  <select class="form-control" name="vehicle_model" required="">
    <option selected="" value="1">XS1</option>
    <option value="2">XS2</option>
  </select>
</div>
$(".brand-select").change(function() {
  var id_brand = $(this).val();
  var vehicle_type = <?php echo $type;?>;
  $.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>" + "vehicle/ajax_model",
    data: {
      'id_brand': id_brand,
      'vehicle_type': vehicle_type
    },
    dataType: 'text',
    success: function(data) {
      $("#ajax_model").html(data);
    }
  });
});

我的表格完整代码。 AJAX正在运作 - 我选择了品牌之后的模特。但是当我点击“保存”时,PHP看不到$vehicle_model。但只有当我运行AJAX时。当我刷新页面而不改变品牌时,一切正常。

<table id="tabela" class="table table-bordered table-striped">
  <tbody>
    <form action="http://192.168.0.200:9000/vehicle/update_information" method="post" accept-charset="utf-8">
      <tr>
        <td>Number:</td>
        <td>
          <div class="form-group"><input type="text" class="form-control" autocomplete="off" value="12345" name="number"></div>
        </td>
      </tr>
      <tr>
        <td>VIN:</td>
        <td>
          <div class="form-group"><input type="text" class="form-control" autocomplete="off" maxlength="17" minlength="17" value="12345678901234567" name="vin"></div>
        </td>
      </tr>
      <tr>
        <td>Brand:</td>
        <td>
          <div class="form-group">
            <select class="form-control brand-select" name="brand" required>
              <option selected="true" value='1'>MAN</option>
              <option value='2'>Volvo</option>
              <option value='3'>Renault</option>
              <option value='4'>DAF</option>
            </select>
          </div>
        </td>
      </tr>
      <tr>
        <td>Model:</td>
        <td>
          <div class="form-group" id="ajax_model">
            <select class="form-control" name="vehicle_model" required>
              <option selected value="1">XS1</option>
              <option value="2">XS2</option>
            </select>
          </div>
        </td>
      </tr>
      <tr>
        <td>Kilometers</td>
        <td>
          <div class="form-group"><input type="text" class="form-control" value="100 km" disabled></div>
        </td>
      </tr>
      <tr>
        <td>Comment:</td>
        <td>
          <textarea class="form-control" rows="5" name="comment" id="comment"></textarea>
          <input type="hidden" name="id_vehicle" value="137" />

        </td>
      </tr>
      <tr>
        <td colspan="2"><button type='submit' class='btn btn-md btn-success' style="float:right;margin-top:10px;width:80px;">Save</button>
        </td>
      </tr>
    </form>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:-1)

因为您忘记发送 vehicle_model 试试这个

<script>
    $( ".brand-select" ).change(function() {
        var id_brand = $(this).val();
        var vehicle_model=$("select[name=vehicle_model]").val()
        var vehicle_type = <?php echo $type;?>;
        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>" + "vehicle/ajax_model",
            data: {
                 'id_brand': id_brand, 
                 'vehicle_type': vehicle_type,
                  'vehicle_model':vehicle_model},
            dataType: 'text',
            success: function(data){
                $( "#ajax_model" ).html( data );
            }
        });
    });
</script>