使用ajax更改表单字段但字段值未传递提交

时间:2018-04-07 07:56:56

标签: php ajax forms

我有一个带有下拉客户列表的标准表单,当选择客户时使用ajax调用load-customers.php页面并完全根据customer_id更改表单字段,同时填写相关信息。< / p>

其中一个字段是另一个名为address_id的丢弃,它再次允许用户从已为所选客户定义的多个地址中进行选择。

问题是,address_id在提交时没有被传递给$ _POST,公平地说,其他任何字段都不是......但是其他字段只是占位符而我只关心customer_id和address_id。

我如何解决这个问题?这是我用过的唯一的ajax。

AJAX:

function getCustomer(val) {
    $.ajax({
        type: "POST",
        url: "load-customers.php",
        data:'id='+val,
        success: function(data){
            $("#customer").html(data);
        }
    });
}

客户:

<div class='form-group row'>
    <label class='col-md-4 control-label'>Quick Select</label>
    <div class='col-md-8 selectContainer'>
        <div class='input-group'>
            <span class='input-group-addon'><i class=''></i></span>
            <select name='Customer_ID' class='form-control selectpicker' id='Customer_ID'
                    onChange="getCustomer(this.value);">
                <option value='0'>Please create a new Customer</option>
                <?php
                $sell_type = "Trade";
                $getcustomer = $conn->prepare("SELECT * FROM customers WHERE type = :type");
                $getcustomer->bindParam(':type', $sell_type);
                $getcustomer->execute();
                foreach ($getcustomer->fetchAll(PDO::FETCH_ASSOC) as $row) {
                    $customer_id = $row['id'];
                    $customer_name = $row['name'];
                    $customer_company = $row['company'];

                    if (!empty($customer_company)) {
                        $customer = $customer_company;
                    } else {
                        $customer = $customer_companyname;
                    }
                    echo "<option value='$customer_id'>$customer</option> ";
                }
                ?>
            </select>
        </div>
    </div>
</div>

<div id='address'>
    // address results go here
</div>

地址:

    <div class='form-group row'> 
      <label class='col-md-4 control-label'>Postcode</label>
        <div class='col-md-8 selectContainer'>
        <div class='input-group'>
            <span class='input-group-addon'><i class=''></i></span>
        <select id='address_id' class='form-control selectpicker' name='address_id' >
";

        $getaddresses = $conn->prepare("SELECT * FROM addresses WHERE customer_id = :id");
        $getaddresses->bindParam(':id', $customer_id);
        $getaddresses->execute();
            foreach ($getaddresses->fetchAll(PDO::FETCH_ASSOC) as $row) {
                $address_id = $row['id']; 
                $postcode = $row['postcode'];
                $areacode = $row['areacode'];

                echo "
                  <option value='$address_id'>$postcode</option>
                ";
            }

 echo "
        </select>
      </div>
    </div>
  </div>

表单本身很大但标题是:

<form class="form-horizontal justify-content-center" action="new.php" method="post"  id="form">

并提交时:

<button type="submit" class="btn btn-primary mb-2">Submit</button>

0 个答案:

没有答案