通过ajax将表单(POST)中的变量发送到另一个PHP脚本(GET)

时间:2015-05-11 11:24:38

标签: javascript php jquery ajax forms

在过去的几天里,我试图为我的文件找到合适的上传脚本。当我找到一个时,我注意到我可以使用更多功能。在这个脚本中有一个简单的表单,只有“浏览”按钮,进度条(Ajax,jQuery / JS)和上传按钮。而已。我要做的是在表单中添加一些功能,比如选择文件的类别。这是 upload_form.html

的代码
<!DOCTYPE html>
<html>
<head>
<script>
/* Script written by Adam Khoury @ DevelopPHP.com */
/* Video Tutorial: http://www.youtube.com/watch?v=EraNFJiY0Eg */
function _(el){
    return document.getElementById(el);
}
function uploadFile(){
    var file = _("file1").files[0];
    // alert(file.name+" | "+file.size+" | "+file.type);
    var formdata = new FormData();
    formdata.append("file1", file);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "file_upload_parser.php");
    ajax.send(formdata);
}
function progressHandler(event){
    _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
    var percent = (event.loaded / event.total) * 100;
    _("progressBar").value = Math.round(percent);
    _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
    _("status").innerHTML = event.target.responseText;
    _("progressBar").value = 0;
}
function errorHandler(event){
    _("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
    _("status").innerHTML = "Upload Aborted";
}
</script>
</head>
<body>
<h2>HTML5 File Upload Progress Bar Tutorial</h2>
<form id="upload_form" enctype="multipart/form-data" method="post">
  <input type="file" name="file1" id="file1"><br>
  <input type="button" value="Upload File" onclick="uploadFile()">
  <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
  <h3 id="status"></h3>
  <p id="loaded_n_total"></p>
</form>
</body>
</html>

file_upload_parser.php

<?php
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}
if(move_uploaded_file($fileTmpLoc, "test_uploads/$fileName")){
    echo "$fileName upload is complete";
} else {
    echo "move_uploaded_file function failed";
}
?>

我想要做的是在表单中添加更多选项(例如“select”),然后在按下“上传”按钮后,将该表单中的变量发送到.php文件并使用GET函数来获取该变量,以便我可以将其用于其他内容,例如将该文件及其类别添加到数据库中。问题是我不知道该怎么做。我试图更改提交类型的上传按钮,但它不起作用(坏主意,现在我知道了),我试图在JS代码中添加一些变量,但(在我的情况下)它不起作用好。你能帮我解决一下这个问题吗? 我将非常感谢能得到的任何帮助。 此致

2 个答案:

答案 0 :(得分:1)

必须将

GET个参数添加到网址中。因此,如果表单包含

<select id="menu">
    ...
</select>

您的Javascript将是:

function uploadFile(){
    var file = _("file1").files[0];
    // alert(file.name+" | "+file.size+" | "+file.type);
    var formdata = new FormData();
    formdata.append("file1", file);
    var menuValue = _("menu").value;
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "file_upload_parser.php?menu=" + menuValue);
    ajax.send(formdata);
}

然后您的PHP可以使用$_GET['menu']来获取此参数的值。

顺便说一下,您需要将onclick更改为onclick="uploadFile(); return false;"。它必须返回false以防止默认表单提交。

答案 1 :(得分:0)

完成文件上传后,您需要再执行一次ajax调用。喜欢,

function completeHandler(event){
        _("status").innerHTML = event.target.responseText;
        _("progressBar").value = 0;
        sendSelectedParameter();
}
function sendSelectedParameter(){
    //get selected value and execute another ajax call of GET type.
}