Javascript 无法在php中检索$\u POST变量

Javascript 无法在php中检索$\u POST变量,javascript,php,ajax,Javascript,Php,Ajax,在对这个问题进行了大量的搜索和无休止的研究之后,我决定在这里发表文章。 我使用ajax将带有文件名变量的formdata传递给php。然后,我尝试访问变量名,以便将其传递给第二个php,后者将该名称加载到数据库中。但我无法用php传递或回显数据。有人能帮忙吗 我的javascript function uploadFile() { var input = document.getElementById("picture"); file = input.files[0]; newFil

在对这个问题进行了大量的搜索和无休止的研究之后,我决定在这里发表文章。 我使用ajax将带有文件名变量的formdata传递给php。然后,我尝试访问变量名,以便将其传递给第二个php,后者将该名称加载到数据库中。但我无法用php传递或回显数据。有人能帮忙吗

我的javascript

function uploadFile() {
  var input = document.getElementById("picture");
  file = input.files[0];
  newFileName = elementHandle[9].val() + elementHandle[10].val() + elementHandle[11].val();
  console.log(newFileName);
  if (file != undefined) {
    formData = new FormData();

    if (!!file.type.match(/image.*/)) {
      formData.append("newFileName1", newFileName);
      $.ajax({
        url: "upload_file.php",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        success: function(data) {}
      });
    } else {
      alert('Not a valid image!');
    }
  } else {
    alert('Input something!');
  }
}
我的php

<?php
    $dir = "im_ges/";
    $file_name = $dir. $_REQUEST["newFileName1"].".jpg";
    echo $file_name;
    move_uploaded_file($_FILES["image"]["tmp_name"], $dir. $_POST ['newFileName1'].".jpg");
?>

试试这个示例代码

form.html

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'post.php',
            data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
            contentType: false,       // The content type used when sending data to the server.
            cache: false,             // To unable request pages to be cached
            processData:false,        // To send DOMDocument or non processed data file it is set to false
            success: function (responce) {
             alert(responce);
            }
          });

        });

      });
    </script>
  </head>
  <body>
    <form  action="" method="post" enctype="multipart/form-data"><form id="uploadimage" action="" method="post" enctype="multipart/form-data">  
      <input name="name" value="First Name"><br>
      <input name="file" type ="file" ><br>
      <input name="submit" type="submit" value="Submit">
    </form>
  </body>
</html>
这将提醒表单中的值,请尝试此示例代码

form.html

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'post.php',
            data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
            contentType: false,       // The content type used when sending data to the server.
            cache: false,             // To unable request pages to be cached
            processData:false,        // To send DOMDocument or non processed data file it is set to false
            success: function (responce) {
             alert(responce);
            }
          });

        });

      });
    </script>
  </head>
  <body>
    <form  action="" method="post" enctype="multipart/form-data"><form id="uploadimage" action="" method="post" enctype="multipart/form-data">  
      <input name="name" value="First Name"><br>
      <input name="file" type ="file" ><br>
      <input name="submit" type="submit" value="Submit">
    </form>
  </body>
</html>

如果您在您可以使用的自定义php控制器中获得文件名,则这将从表单

警告值

$target_dir = "uploads/";

$filename = "phonenumber.png";//Set here the new file name which you passed through ajax 

$target_file = $target_dir .$filename;
 // test here the file is moved or  not,
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
// if moved the file then update db with the $filename write code here
then update db with the $filename
}
或者我更改了上面mentioed中的一些代码,并添加了一个文本字段来输入电话号码,然后将所选文件移动到taget中,并在 带有电话名称的文本字段,您可以检查它是否对您有用

form.html

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'post.php',
            data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
            contentType: false,       // The content type used when sending data to the server.
            cache: false,             // To unable request pages to be cached
            processData:false,        // To send DOMDocument or non processed data file it is set to false
            success: function (responce) {
             alert(responce);
            }
          });

        });

      });
    </script>
  </head>
  <body>
    <form  action="" method="post" enctype="multipart/form-data"><form id="uploadimage" action="" method="post" enctype="multipart/form-data">  
      <input name="name" value="First Name"><br>
      <input name="phone" value=""><br> <!-- new field -->
      <input name="file" type ="file" ><br>
      <input name="submit" type="submit" value="Submit">
    </form>
  </body>
</html>

$(函数(){
$('form')。关于('submit',函数(e){
e、 预防默认值();
$.ajax({
键入:“post”,
url:'post.php',
数据:newformdata(this),//发送到服务器的数据,一组键/值对(即表单字段和值)
contentType:false,//向服务器发送数据时使用的内容类型。
cache:false,//无法请求缓存页面
processData:false,//若要发送DOMDocument或未处理的数据文件,将其设置为false
成功:功能(响应){
警报(响应);
}
});
});
});



post.php

    <?php
    echo "Name Value : ".$_POST["name"]."\n\n";
    echo "\n File Details\n";
    print_r($_FILES);

    $target_dir = "uploads/";

    $uploadOk = 1;
    $filename = $_FILES['image']['name'];


    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $target_file = $target_dir . $_POST["phone"].$ext; // here we change the file name before uploads 
             // test here the file is moved or  not,
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
// if moved the file then update db with the $filename write code here
        } 
    ?>


在根目录中创建一个名为“uploads”的文件夹,如果在自定义php控制器中获得文件名,则提供完整权限

$target_dir = "uploads/";

$filename = "phonenumber.png";//Set here the new file name which you passed through ajax 

$target_file = $target_dir .$filename;
 // test here the file is moved or  not,
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
// if moved the file then update db with the $filename write code here
then update db with the $filename
}
或者我更改了上面mentioed中的一些代码,并添加了一个文本字段来输入电话号码,然后将所选文件移动到taget中,并在 带有电话名称的文本字段,您可以检查它是否对您有用

form.html

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'post.php',
            data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
            contentType: false,       // The content type used when sending data to the server.
            cache: false,             // To unable request pages to be cached
            processData:false,        // To send DOMDocument or non processed data file it is set to false
            success: function (responce) {
             alert(responce);
            }
          });

        });

      });
    </script>
  </head>
  <body>
    <form  action="" method="post" enctype="multipart/form-data"><form id="uploadimage" action="" method="post" enctype="multipart/form-data">  
      <input name="name" value="First Name"><br>
      <input name="phone" value=""><br> <!-- new field -->
      <input name="file" type ="file" ><br>
      <input name="submit" type="submit" value="Submit">
    </form>
  </body>
</html>

$(函数(){
$('form')。关于('submit',函数(e){
e、 预防默认值();
$.ajax({
键入:“post”,
url:'post.php',
数据:newformdata(this),//发送到服务器的数据,一组键/值对(即表单字段和值)
contentType:false,//向服务器发送数据时使用的内容类型。
cache:false,//无法请求缓存页面
processData:false,//若要发送DOMDocument或未处理的数据文件,将其设置为false
成功:功能(响应){
警报(响应);
}
});
});
});



post.php

    <?php
    echo "Name Value : ".$_POST["name"]."\n\n";
    echo "\n File Details\n";
    print_r($_FILES);

    $target_dir = "uploads/";

    $uploadOk = 1;
    $filename = $_FILES['image']['name'];


    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $target_file = $target_dir . $_POST["phone"].$ext; // here we change the file name before uploads 
             // test here the file is moved or  not,
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
// if moved the file then update db with the $filename write code here
        } 
    ?>


在根目录中创建一个名为“uploads”的文件夹,并提供完整权限

在PHPI上尝试
var\u dump($\u POST)
如果(文件!=未定义){array(19){fname]=>string(7)“dfgfdgg”[“mname”]=>string(0)”[“lname”=>string(0)”[“addr1”=>string(4)“dfgf”[“addr2”=>string(0)”[“city”]=>string(3)“sfg”[“state”]=>string(2)“DE”[“zip”]=>string(5)“23423”[“email”]=>string(17)”ssz3cdfs@dsdf.com“[“地区电话”]=>string(3)“222”[“前缀电话”]=>string(3)“222”[“电话”]=>string(4)“2222”[“性别”]=>string(4)“男性”[“月”]=>string(2)“01”[“日期”]=>string(2)“01”[“年”=>string(4)“2000”[“医疗条件”]=>string(0)“[“explavel”]=>string(11)“experied”[“category”]=>string(4)“teen”}我实际上尝试了var_转储,但没有获得所需的变量,但由于某些原因,我可以使用move_上传的文件($_FILES[“image”][“tmp_name”],$dir.$_POST['newFileName1'..jpg])成功访问PHP中的is;Try
var_转储可能重复($_POST)
如果(文件!=未定义){array(19){[“fname”]=>string(7)“dfgfdgg”[“mname”]=>string(0)”“[“lname”]=>string(0)”“[“addr1”]=>string(4)“dfgf”[“addr2”]=>string(0)”[“city”=>string(3)“sfg”[“state”=>string(2)“DE”[“zip”=>string(5)”23423”=>string(17)"ssz3cdfs@dsdf.com“[“地区电话”]=>string(3)“222”[“前缀电话”]=>string(3)“222”[“电话”]=>string(4)“2222”[“性别”=>string(4)“男性”[“月”]=>string(2)“01”[“日期”]=>string(2)“01”[“年”=>string(4)“2000”[“医疗条件”=>string(0)”[“解释”=>string(11)“有经验的”[“类别”=>string(4)“青少年”]实际上,我尝试了var_dump,但没有获得所需的变量,但由于某种原因,我可以使用move_uploaded_文件($_文件[“image”][“tmp_name”],$dir.$_POST['newFileName1']..jpg”)成功访问PHP中的is;可能与Thank you Ganesh重复。唯一的问题是我想上传电话号码作为文件名,以保持它们的唯一性,而不是用户提供的原始文件名。因此,我添加了代码:newFileName=elementHandle[9]。val()+elementHandle[10]。val()+elementHandle[11]。val();formData.append(“image”,newFileName);$.ajax({url:“upload_file.php”,类型:“POST”,数据:formData,processData:false,contentType:false,成功:函数(数据){}谢谢Ganesh。唯一的问题是我想上传电话号码作为文件名,以保持它们的唯一性,而不是用户提供的原始文件名。因此我添加了代码:newFileName=elementHandle[9]。val()+elementHandle[10]。val()+elementHandle[11]。val();formData.append(“image”,newFileName);$.ajax({url:upload_file.php”,输入:“POST”,数据:formData,processData:false,