简体   繁体   English

PHP无法获取从AJAX传递的POST值

[英]PHP Not Able To Get POST value passed from AJAX

I'm following what this post mentioned to upload a file. 我正在关注这篇文章提到的上传文件。 A little difference is I added one more text input field on the form. 略有不同是我在表单上添加了另一个文本输入字段。 The image file is uploaded to server successfully but it seems the value in input field doesn't get passed to PHP for database update. 图像文件已成功上传到服务器,但是输入字段中的值似乎没有传递给PHP进行数据库更新。 The database function is fired and database record added but missing the value from the form. 数据库函数被触发,并且数据库记录已添加,但缺少表单中的值。

Can anyone point me out what I missed? 谁能指出我错过的事情? Thanks. 谢谢。

 $.validate({ form: '#frmSlide', modules: 'file, html5', validateOnBlur: false, errorMessagePosition: 'top', // Instead of 'element' which is default scrollToTopOnError: false, // Set this property to true if you have a long form onSuccess: function($form) { var file_data = $('#a_imgfile').prop('files')[0]; var form_data = new FormData(); form_data.append('file', file_data); $.ajax({ url: 'slide_upd.php', // point to server-side PHP script dataType: 'text', // what to expect back from the PHP script, if anything cache: false, contentType: false, processData: false, data: form_data, type: 'post', success: function(data) { alert(data); } }); } }); 
 <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data" class="form-horizontal" id="frmSlide"> <div class="form-group"> <label class="col-sm-4 control-label" for="imgfile">Image file</label> <div class="col-sm-8"> <input type="file" id="a_imgfile" data-validation="required mime size" data-validation-allowing="jpg, png, gif" data-validation-ratio="1:1" data-validation-max-size="1M" data-validation-error-msg="An image file is mandatory." /> </div> </div> <div class="form-group"> <div class="col-sm-8 col-md-offset-4" id="image-holder"> </div> </div> <div class="form-group"> <label class="col-sm-4 control-label" for="seq">Sequence</label> <div class="col-sm-8"> <input class="form-control server" name="a_seq" id="a_seq" type="number" min="1" max="4" value="" placeholder="Enter display sequence of this slide" data-validation-error-msg="Only 1 to 4 is allowed." /> </div> </div> <div class="form-group"> <div class="col-sm-offset-4 col-sm-8"> <button name="btnUpd" id="btnUpd" type="submit" class="clsUpd btn btn-primary"><i class="fa fa-floppy-o"></i>&nbsp;Update</button> </div> </div> </form> 

 <?php $image_name = $_FILES['file']['name']; $image_size = $_FILES['file']['size']; $image_temp = $_FILES['file']['tmp_name']; move_uploaded_file($image_temp, 'img/'.$image_name); $seq = $_POST['a_seq']; addSlide($seq); ?> 
 function addSlide($seq) { $seq = (int)$seq; mysql_query("INSERT INTO slide (seq, lastchgat) VALUES ('$seq', now())") or die(mysql_error()); } 

  1. The a_seq is not appended to the form_data. a_seq不会附加到form_data。
  2. add var a_seq = $('#a_seq').val(); 添加var a_seq = $('#a_seq').val();
  3. form_data.append('a_seq', a_seq); form_data.append('a_seq',a_seq);
  4. Should be good to go 应该很好走

I think this will fix your problem 我认为这可以解决您的问题

$.validate({
  form: '#frmSlide',
  modules: 'file, html5',
  validateOnBlur: false,
  errorMessagePosition: 'top', // Instead of 'element' which is default
  scrollToTopOnError: false, // Set this property to true if you have a long form
  onSuccess: function($form) {
    //var file_data = $('#a_imgfile').prop('files')[0];
    //var form_data = new FormData();
    //form_data.append('file', file_data);
    //------ instead of three lines i just did this and works fine for me -------
    var formData=new FormData($('#frmSlide')[0]);
    $.ajax({
      url: 'slide_upd.php', // point to server-side PHP script
      cache: false,
      contentType: false,
      processData: false,
      data: formData,
      type: 'post',
      success: function(data) {
        alert(data);
      }
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM