【问题标题】:How can find inputs in php codes?如何在 php 代码中找到输入?
【发布时间】:2015-10-20 10:23:57
【问题描述】:

我在 jQuery 中使用 $.ajax 从数据库中获取了一些输入,当用户单击按钮时它会出现:

HTML

<div id="div1"></div>
<input type="button" id="button1"/>

JQuery

$("#button1").click(function() {
   $.ajax({
      type: 'POST',
      url: "get.php",
      data: {vals: "send"},
      success : function(response) { 
         $("#div1").append(response);
   }});
})

将元素导入div后,会有一个input type="file"这样的:

编辑: 我的表单代码在这里:

HTML

<input name="upload1" id="upload1" type="file"/>
<input type="button" value="submit" id="button2"/>

JQuery

$("#button2").click(function() {
    $.ajax({
        type: 'POST',
        url: "uploadPhoto.php",
        data: {filename: filename},
        success : function(response) {      
            alert(response);
        }
    });
})

PHP

$filename = $_POST['filename ']; 

$target_dir = "image/";
$target_file = $target_dir . $filename;

var  $src_temp = $_FILES["upload1"]["tmp_name"];    
if (move_uploaded_file($src_temp, $target_file)) {
echo 'success';
}

结束编辑 1

当我想为这个按钮获取$_FILES["upload1"]["tmp_name"] 以在 PHP 代码中查找上传文件的临时文件夹时,它找不到具有 upload1 名称的输入。

如何在 PHP 代码中找到输入名称?

【问题讨论】:

标签: php input filepath


【解决方案1】:

尝试以下代码获取 $_FIELS 中的值

$("#button1").on('click', function() { 
   var filename = document.getElementById('image id here').value.replace(/C:\\fakepath\\/i, '');
   var image = document.getElementById('image id here').files[0];
   Data = new FormData();
   Data.append('upload1', image);
   Data.append('file_name', filename); //pass file name
   var xmlReq = new XMLHttpRequest(); //creating xml request
   xmlReq.open("POST", 'enter your url', true); //send request
   xmlReq.onload = function(answer) {
    if (xmlReq.status == 200) {
        var response = jQuery.parseJSON(xmlReq.responseText);
        console.log(response); 
    } else {
        console.log("Error " + xmlReq.status + " occurred uploading your file.<br \/>");
    }
};
     xmlReq.send(Data); //send form data
 });

【讨论】:

  • 感谢您的回答,当用户单击提交按钮时,我将带有 $.ajax 的输入 ID 和名称发送到单独的 php 文件,并且您的
    无法在此提交
  • 你是什么意思? $image_name?
  • 用ajax提交for时为什么要在php代码中查找输入名称?
  • 谢谢@Basheer Kharoti,我在 ajax 中的数据还有一些其他变量可以像这样发送 {filename: filename} 如何使用 {filename: filename} 发送 formData
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多