使用AJAX和PHP脚本的HTML上传文件 - 帮助?脚本、上传文件、AJAX、PHP

2023-09-11 01:35:16 作者:佩服许仙敢日蛇

我想执行的任务是不可能的。我还没有发现相同的情况还没有在互联网附近。

I am trying to perform a task that may not be possible. I haven't found the same scenario yet around the internet.

我有四个字段,上一点击,就会调用JavaScript函数,做一个AJAX调用数据从一个PHP文件。

I have four fields that, on a click, will call a Javascript function that does an AJAX call for data from a PHP file.

的字段最终将在数据库填充行和PHP将查询新组数据,并且用户将看到它。数据的一部分是图像,用户可以点击并展开。

The fields will ultimately be filling a row in a database and the PHP will query the new set of data, and the user will see it. Part of the data is an image that the user can click on and expand.

AJAX调用工作,不断扩大的形象工程,而是通过上传加入这一形象不,从而打破了整个事情。

The AJAX call works, the expanding image works, but adding this image via an upload does not, thus breaking the whole thing.

下面是HTML表单的一个片段:

Here is a snippet of the HTML form:

Entry: <input type='text' id='entry' />
Stop: <input type='text' id='stop' />
Final: <input type='text' id='final' />
Chart: <input type='file' id='chart' value='Get Chart'/>
<input type='button' onclick='addResult()' value='Add' />

在addResult()处理这样的事情:

The addResult() processes things like this:

var entry = document.getElementById('entry').value;
var stop = document.getElementById('stop').value;
var final = document.getElementById('final').value;
var chart = document.getElementById('chart').value;
var queryString = "?entry=" + entry + "&stop=" + stop + "&final=" + final + "&chart=" + chart + "&loggedIn=true";
ajaxRequest.open("GET", "addResult.php" + queryString, true);

没什么特别的,只是把数据并将其发送到addResult.php响应。下面是该过程用于处理图表上传数据在addResult.php:

Nothing special, just taking the data and sending it to addResult.php for a response. Here is the process for handling the 'chart' upload data in addResult.php:

$chart = $_GET['chart'];
$chart_loc = "../img/".basename($_FILES[$chart]['name']);
move_uploaded_file($_FILES[$chart]['tmp_name'], $chart_loc);
if(($entry != "") && ($stop != "") && ($final != "") && ($chart_loc != "")){
           mysql_query("INSERT INTO resultsMod VALUES (now(),'".$entry."','".$stop."','".$final."','".$chart_loc."')") or die(mysql_error());
        }

当我点击添加按钮,它什么都不做了,以前我添加了上传文件的领域,一切工作正常。所以,我必须做一些错误的处理文件。

When I click the 'Add' button, it does nothing, and before I added the upload file field, everything was working fine. So I must be doing something wrong with handling the file.

我从来没有与上传数据工作过,所以我不知道是否发送了这样的数据即使是允许的(没有表单提交,只是将文件传递的数据通过JavaScript到PHP文件)。 我也可能会做一些不正确的move_uploaded_file功能。

I've never worked with upload data before, so I don't know if sending that data like that is even allowed (without a form submission and just passing the file data through Javascript to a php file). I also may be doing something incorrect with the move_uploaded_file function.

当然的目标是有可能从形式使用,包括上载的图像的新的数据的实时更新。 (我知道我的code是简单的,我会​​添加到它一旦这个功能) 任何帮助是AP preciated,谢谢!

The goal of course is to have a real time update of the new data from the form, including the uploaded image. (I know my code is simple and I will add to it once this is functional) Any help is appreciated, thanks!

推荐答案

您尝试上传使用AJAX的文件,这是不是更多钞票。最好的办法是单独管理的文件上传,也许通过 SWF上传

You are trying to upload a file using AJAX, which is not posible. The best approach would be to manage the file upload separately, maybe through swf-upload