如何从jquery ' formData '中提取值以插入Mysql


How to extract values from jquery `formData` to insert into Mysql?

我的代码是关于通过$.ajax提交multipart form,这是成功地做到了&在submit.php中的json_encode上,它给了我这个:

{"success":"Image was submitted","formData":{"fb_link":"https:'/'/www.google.mv'/",
"show_fb":"1",
"filenames":[".'/uploads'/homepage-header-social-icons'/27.jpg"]}}

有人能解释我,在submit.php,我怎么能从formData提取值,存储在mysql表?我试过了,很多事情包括:

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

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

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

但是,似乎没有工作?

感谢

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

      $(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
      }
      });
      }
      });

UPDATE CODE OF - submit.php:

    <?php
    // Here we add server side validation and better error handling :)
    $data = array();
    if(isset($_GET['files'])) {
    $error = false;
    $files = array();
    $uploaddir = './uploads/homepage-header-social-icons/';
    foreach ($_FILES as $file) {
    if (move_uploaded_file($file['tmp_name'], $uploaddir . basename($file['name']))) {
    $files[] = $uploaddir . $file['name'];
    //$files = $uploaddir . $file['name'];
    }

    else {
    $error = true;
    }
    }
    $data = ($error) ? array('error' => 'There was an error uploading your image-files') : array('files' => $files);
    }

    else {
    $data = array(
    'success' => 'Image was submitted',
    'formData' => $_POST
    );
    }
    echo json_encode($data); 

    ?>

如果在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. 它发送多个文件,上传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);

像魅力一样工作!:)