简体   繁体   English

如何在多部分表单上使用AJAX,Javascript和PHP?

[英]How to use AJAX, Javascript, and PHP on multipart forms?

I have this update status form for my current project. 我有当前项目的此更新状态表格。 As of now I am able to update my status with HTML entries only. 到目前为止,我仅能使用HTML条目更新状态。 I was wondering how I would go about allowing users to update with a photo. 我想知道如何允许用户更新照片。 I know that the input type must be "file" and I need to add "enctype="multipart/form-data" to the form. What parts of the wall.js and functions.php do I need to modify to allow status update with photos. I do have a upload.php script but don't know how I can get it all to work together. Any clues or direction to try next would be great. 我知道输入类型必须为“文件”,并且需要在表单中添加“ enctype =“ multipart / form-data”。我需要修改wall.js和functions.php的哪些部分以允许状态更新我确实有一个upload.php脚本,但不知道如何将它们一起使用,尝试下一步的任何线索或方向都很好。

HTML Form HTML表格

<form method="post" action="">
<input type="text" name="update" id="update">
<br />
<input type="submit" id="update_button"  class="update_button"/>
</form>

wall.js //update status wall.js //更新状态

$(document).ready(function()
{
$(".update_button").click(function()
{
var updateval = $("#update").val().split('\\').pop();
var dataString = 'update='+ updateval;
if(updateval=='')
{
alert("Please Enter Some Text");
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('Loading Update...');
$.ajax({
type: "POST",
url: "message_ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$("#flash").fadeOut('slow');
$("#content").prepend(html);
$("#update").val('');
$("#update").focus();
$("#stexpand").oembed(updateval);
}
});
}
return false;
});
}

Functions.php //Insert Update Functions.php //插入更新

public function Insert_Update($id, $update)
{
$update=htmlentities($update);
$time=time();
$ip=$_SERVER['REMOTE_ADDR'];
$query = mysql_query("SELECT msg_id,message FROM `messages` WHERE id_fk='$id' order by msg_id desc limit 1") or die(mysql_error());
$result = mysql_fetch_array($query);
if ($update!=$result['message']) {
$query = mysql_query("INSERT INTO `messages` (message, id_fk, ip,created) VALUES ('$update', '$id', '$ip','$time')") or die(mysql_error());
$newquery = mysql_query("SELECT M.msg_id, M.id_fk, M.message, M.created, U.username FROM messages M, users U where M.id_fk=U.id and M.id_fk='$id' order by M.msg_id desc limit 1 ");
$result = mysql_fetch_array($newquery);
return $result;
}
else
{
return false;
}
}

On the PHP side, see the "Handling File Uploads" section of the PHP manual at http://www.php.net/manual/en/features.file-upload.php . 在PHP方面,请参见http://www.php.net/manual/en/features.file-upload.php上 PHP手册的“处理文件上载”部分。 Chances are you'll want to use the "POST method uploads" (a la the $_FILES magic variable). 您可能会想使用“ POST方法上载”(例如$_FILES魔术变量)。 Read all five sub-sections there to know how it all works and avoid problems. 阅读那里的所有五个小节,了解它们如何工作并避免出现问题。

Oh, and before putting any user input into your SQL queries, make sure to escape it using mysql_real_escape_string() / mysql_escape_string() . 哦,在将任何用户输入放入您的SQL查询之前,请确保使用mysql_real_escape_string() / mysql_escape_string()对其进行转义。 Hopefully that stuff wasn't in your example only because you were trying to keep your posting brief. 希望这些内容不仅仅因为您试图使发布简短而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM