如何使用AJAX将PHP Session变量添加到FormData中?

时间:2017-09-27 02:19:54

标签: javascript php jquery ajax form-data

我想使用FormData和AJAX传递PHP会话变量(称为' profileid')。我认为下面的内容可行,但事实并非如此。为什么呢?

var imageData = new FormData();
imageData.append('image', $('#uploadImage')[0].files[0]);
imageData.append('profileid', <?php echo $_SESSION['profileid'];?>);

//Make ajax call here:
$.ajax({
      url: '/upload-image-results-ajax.php',
      type: 'POST',
      processData: false, // important
      contentType: false, // important
      data: imageData,
      //leaving out the rest as it doesn't pertain

1 个答案:

答案 0 :(得分:1)

您可以在$sql= "INSERT INTO pasystems (catatan) VALUES('$catatan')"; 网址参数中添加profileid,而不是在FormData中添加它:

$.ajax

不要忘记在代码的开头放置$(document).ready(function (e) { $('#uploadImageForm').on('submit',(function(e) { e.preventDefault(); var formData = new FormData(this); $.ajax({ url: "/upload-image-results-ajax.php?profileid=<?= $_SESSION['profileid']; ?>", type: "POST", data: formData, cache: false, contentType: false, processData: false, success: function(response){ console.log("success"); console.log(response); }, error: function(response){ console.log("error"); console.log(response); } }); })); $('#uploadImage').on("change", function() { $("#uploadImageForm").submit(); }); });