使用ajax

时间:2019-01-04 13:38:19

标签: php ajax codeigniter

我有这个观点

<div class="w3-row w3-padding-64">
<div class="w3-twothird w3-container">
<form id='create_delivery'  method="post" class="form-horizontal" 
enctype="multipart/form-data">
 from  <input type="text" name="first_address"><br>

To <input type="text" name="second_address" ><br>
<label>weight</label>
<input type="number" name="weight"   ><br>

<label>price</label>
<input type="number" name="price"   ><br>

<label>description</label><br>
<textarea name="description"></textarea>
<br>

<input type="submit" value="Submit">
</form>
</div>

</div>

然后我通过ajax向控制器方法发送了发布请求

 $(document).ready(function(){
 $("#create_delivery").submit(function(evt){
 var postData = $(this).serialize();
 $.ajax({

 url: baseURL + "deliverys/create_delivery",

 type:'post',

 data:postData,

 dataType:'json',

 success:function(data){

 alert('delivery created');

 }
 });
 evt.preventDefault();
      });
 });

这是控制器

  public function create_delivery(){
  if(isset($_POST)):
  $first_address=   $this->input->post('first_address');
  $second_address=  $this->input->post('second_address');
  $description=      $this->input->post('description');
  $Weight=           $this->input->post('Weight');
  $price=            $this->input->post('price');

  $data = array(
  "first_address"=>$first_address,
  "second_address"=>$second_address,
  "description"=>$description,
  "Weight"=>$Weight,
  "price"=>$price

  );
  $this->deliverys_model->create_delivery($data);
  endif;
  }

所有值都为空的问题, 当我发送不带ajax的帖子请求时,它起作用 但是使用ajax时,这些值为NULL

错误编号:1048 将值插入deliverysfirst_addresssecond_addressdescriptionready_to_buyWeightprice)值(NULL,NULL, NULL,NULL,NULL,NULL)

4 个答案:

答案 0 :(得分:1)

我认为您没有通过var postData = $(this).serialize()获得表格数据

尝试$("#create_delivery").serialize()

如果表单中没有<input type="file">,请不要使用enctype="multipart/form-data"

$(document).ready(function(){
 $("#create_delivery").submit(function(evt){
 evt.preventDefault();

 $.ajax({
   url: baseURL + "deliverys/create_delivery",
   type:'POST',
   data:$("#create_delivery").serialize(),
   dataType:'json',
   success:function(data){
     alert('delivery created');
   }
 });
 evt.preventDefault();
      });
 });

答案 1 :(得分:0)

尝试这些

private void BtnSubmit_Click(object sender, EventArgs e)
    {
        //get asset number 
        string asset = txtAssetNumber.Text;
        //get invID from textbox
        int InventoryID = Convert.ToInt32(txtinv.Text);
        //this query changes available status to 'No'              
        inventoryTableAdapter.SetNo(asset);    
        //runs query to determine what rows have 'Yes'
        inventoryTableAdapter.Available(this.loanerCabDataSet.Inventory);

        try
        {        
            //add new row to log table
            inventoryLogBindingSource.AddNew();
            //suppose to insert the ID into table
            inventoryLogTableAdapter.insertinvid(InventoryID);

            //set textbox back to today's date
            txtOutDate.Text = DateTime.Today.ToString("MM/dd/yyyy");

            this.Validate();
            this.inventoryBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.loanerCabDataSet);

            MessageBox.Show("Success");
        }
        catch (Exception)
        {
            throw;
        }

    }

代替

$first_address = $_POST['first_address'];
$second_address = $_POST['second_address'];
$description = $_POST['description'];
$Weight = $_POST['weight'];
$price = $_POST['price'];

我不知道您的代码中$first_address= $this->input->post('first_address'); $second_address= $this->input->post('second_address'); $description= $this->input->post('description'); $Weight= $this->input->post('Weight'); $price= $this->input->post('price'); 的上下文,也许您可​​以显示更多代码,但是上面的代码应该可以工作。

答案 2 :(得分:0)

您需要将“类型”更改为“方法”

$.ajax({
 url: baseURL + "Trips/create_trip",
 method:'POST',
 data:postData,
 dataType:'json',
 success:function(data){
 alert('Trip created');
 }
 });

答案 3 :(得分:0)

.serialize();用于创建get方法字符串...如果要发布数据,请使用ajax更改您的js

$("#create_delivery").submit(function(evt){

 evt.preventDefault();

var first_address = $( "input[name='first_address']" ).val();
var second_address = $( "input[name='second_address']" ).val();
var weight = $( "input[name='weight']" ).val();
var price = $( "input[name='price']" ).val();
var description = $( "input[name='description']" ).val();

 $.ajax({

 url: baseURL + "deliverys/create_delivery",

 type:'post',

data : {first_address:first_address,second_address:second_address,weight:weight,price:price,description:description},

 dataType:'json',

 success:function(data){

 alert('delivery created');

 }
 });
      });