无法在php中检索$ _POST变量

时间:2017-11-16 06:41:13

标签: javascript php ajax

经过大量搜索并无休止地研究这个问题,我决定在这里发帖。 我使用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");
?>

2 个答案:

答案 0 :(得分:0)

试试此示例代码

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>

创建post.php

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

这将提示表格中的值

答案 1 :(得分:0)

如果您在自定义php控制器中获得文件名,则可以使用

Sub InsertAnyRows(NumRows As Integer)

    Dim thisWS As Worksheet
    Set thisWS = ActiveSheet
    If Not (InRange(ActiveCell, thisWS.Range("QuoteLines")) Or InRange(ActiveCell, thisWS.Range("LabourLines")) Or InRange(ActiveCell, thisWS.Range("OptionsLines"))) Then Exit Sub

    Application.ScreenUpdating = False
    Application.CutCopyMode = False

    Dim NumWindows As Integer
    NumWindows = ThisWorkbook.Windows.Count

    If NumWindows > 1 Then

        Dim Windows() As Window
        Dim WindowsSheets() As Worksheet

        ReDim Windows(NumWindows)
        ReDim WindowsSheets(NumWindows)

        Dim i As Integer

        For i = 1 To NumWindows
            Set Windows(i) = ThisWorkbook.Windows(i)
            Set WindowsSheets(i) = Windows(i).ActiveSheet
        Next i

    End If

    Dim RowLoc As Range, EndLoc As Range, SelRange As Range
    Set SelRange = Selection
    Set RowLoc = thisWS.Cells(Selection.Rows(1).Row, 1)

    Select Case NumRows

        Case Is < 0 ' must be inserting costs rows
            NumRows = NumRows * -1
            RowLoc.Resize(NumRows).Offset(1, 0).EntireRow.Insert
            thisWS.Range("CostsBlankRow").Copy
            RowLoc.Resize(NumRows).Offset(1, 0).PasteSpecial Paste:=xlPasteAll

        Case 0 ' must be inserting a header row
            RowLoc.Offset(1, 0).EntireRow.Insert
            thisWS.Range("TabHeaderRow").Copy
            RowLoc.Offset(1, 0).PasteSpecial Paste:=xlPasteAll

        Case Else ' must be inserting normal rows
            RowLoc.Resize(NumRows).Offset(1, 0).EntireRow.Insert
            thisWS.Range("TabBlankRow").Copy
            RowLoc.Resize(NumRows).Offset(1, 0).PasteSpecial Paste:=xlPasteAll

    End Select

    SelRange.Select

    If NumWindows > 1 Then

        For i = NumWindows To 1 Step -1
            Windows(i).Activate
            WindowsSheets(i).Activate
        Next i

    End If

    Application.CutCopyMode = False
    Application.ScreenUpdating = True

End Sub

或者我改变了上面的一些代码,并添加了一个文本字段来输入一个电话号码,并将所选文件移到了taget,输入值为 带有名称电话的文本字段可以检查它对您有用吗。

<强> form.html

$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
}

<强> post.php中

<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>

在根目录中创建一个名为&#34的文件夹;上传&#34;并给予完整的permison