如何从jquery`formData`中提取值以插入到Mysql中?

时间:2016-09-25 11:14:10

标签: php jquery mysql ajax

我的代码是关于提交multipart form,通过$.ajax,它正在成功地执行此操作&在json_encode的{​​{1}}上,它给了我这个:

submit.php

有人可以解释一下,在{"success":"Image was submitted","formData":{"fb_link":"https:\/\/www.google.mv\/", "show_fb":"1", "filenames":[".\/uploads\/homepage-header-social-icons\/27.jpg"]}} 中,我如何从submit.php中提取值,存储在mysql表中?我试过了很多东西,包括:

formData

 $fb_link = formData['fb_link'];
 $show_fb = formData['show_fb'];

$arr = json_encode($data);
$fb_link=$arr['fb_link'];

但是,似乎什么都没有用?任何猜测?

由于

更新: 父页面上的JS代码是:

$fb_link = REQUEST['fb_link'];
 $show_fb = REQUEST['show_fb'];

更新代码 - submit.php

      $(function()
      {
      // Variable to store your files
      var files;

      // Add events
      $('input[type=file]').on('change', prepareUpload);
      $('form#upload_form').on('submit', uploadFiles);

      // Grab the files and set them to our variable
      function prepareUpload(event)
      {
      files = event.target.files;
      }

      // Catch the form submit and upload the files
      function uploadFiles(event)
      {
      event.stopPropagation(); // Stop stuff happening
      event.preventDefault(); // Totally stop stuff happening

      // START A LOADING SPINNER HERE

      // Create a formdata object and add the files
      var data = new FormData();
      $.each(files, function(key, value)
      {
      data.append(key, value);
      });

      //var data = new FormData($(this)[0]);


      $.ajax({
      url: 'jquery_upload_form_submit.php?files=files',
      type: 'POST',
      data: data,

      //data: {data, var1:"fb_link" , var2:"show_fb"},

      cache: false,
      dataType: 'json',
      processData: false, // Don't process the files
      contentType: false, // Set content type to false as jQuery will tell the server its a query string request
      success: function(data, textStatus, jqXHR)
      {
      if(typeof data.error === 'undefined')
      {
      // Success so call function to process the form
      submitForm(event, data);
      }
      else
      {
      // Handle errors here
      console.log('ERRORS: ' + data.error);
      }
      },
      error: function(jqXHR, textStatus, errorThrown)
      {
      // Handle errors here
      console.log('ERRORS: ' + textStatus);
      // STOP LOADING SPINNER
      }
      });
      }

      function submitForm(event, data)
      {
      // Create a jQuery object from the form
      $form = $(event.target);

      // Serialize the form data
      var formData = $form.serialize();

      // You should sterilise the file names
      $.each(data.files, function(key, value)
      {
      formData = formData + '&filenames[]=' + value;
      });

      $.ajax({
      url: 'jquery_upload_form_submit.php',
      type: 'POST',
      data: formData,
      cache: false,
      dataType: 'json',
      success: function(data, textStatus, jqXHR)
      {
      if(typeof data.error === 'undefined')
      {
      // Success so call function to process the form
      console.log('SUCCESS: ' + data.success);
      }
      else
      {
      // Handle errors here
      console.log('ERRORS: ' + data.error);
      }
      },
      error: function(jqXHR, textStatus, errorThrown)
      {
      // Handle errors here
      console.log('ERRORS: ' + textStatus);
      },
      complete: function()
      {
      // STOP LOADING SPINNER
      }
      });
      }
      });

2 个答案:

答案 0 :(得分:1)

如果您在ajax中使用POST方法,那么您可以在PHP中访问这些数据。

print_r($_POST);

使用ajax表单提交。

//Program a custom submit function for the form
$("form#data").submit(function(event){
  //disable the default form submission
  event.preventDefault();
  //grab all form data  
  var formData = new FormData($(this)[0]);
  $.ajax({
      url: 'formprocessing.php',
      type: 'POST',
      data: formData,
      async: false,
      cache: false,
      contentType: false,
      processData: false,
      success: function (returndata) {
         alert(returndata);
      }
  });
  return false;
});

您可以访问PHP上的数据

$json = '{"countryId":"84","productId":"1","status":"0","opId":"134"}';
$json = json_decode($json, true);
echo $json['countryId'];
echo $json['productId'];
echo $json['status'];
echo $json['opId'];

如果要访问文件对象,则需要使用$_FILES

$profileImg = $_FILES['profileImg'];
$displayImg = $_FILES['displayImg'];

答案 1 :(得分:0)

这个问题在以下方面有所不同: 1.发送MULTIPLE文件,上传 2.包含SELECT 3.包含INPUT

因此,图像被序列化和放大通过GET发送。并且,休息FORM数据正在通过POST发送。所以解决方案是:

 var data = new FormData($(this)[0]); 

inside:function uploadFiles(event){}

在submit.php中:

print_r($_POST);
print_r($_GET);
die();

这将为您提供两个值。然后评论这些&根据守则,进行以下更改:

$filename_wpath =  serialize($files);
$fb_link = $_POST['fb_link'];
$show_fb = $_POST['show_fb'];


$sql = "INSERT INTO `tblbasicheader`(fldFB_image, fldFB_link, fldHideShow)
            VALUES('$filename_wpath','$fb_link','$show_fb')";

mysqli_query($db_conx, $sql);

像魅力一样! :)