2014-04-01 72 views
0

我有我的当前项目的此更新状态表单。截至目前,我只能用HTML条目更新我的状态。我想知道如何让用户更新照片。我知道输入类型必须是“文件”,我需要在表单中添加“enctype =”multipart/form-data“。我需要修改wall.js和functions.php的哪些部分以允许状态更新与照片。我有一个upload.php程序,但不知道怎样才能得到这一切一起工作。任何线索或方向,下一个尝试将是巨大的。如何在多部分表单上使用AJAX,Javascript和PHP?

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 //更新状态

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

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

回答

0

在PHP方面,请参阅PHP手册的“处理文件上传”部分,网址为http://www.php.net/manual/en/features.file-upload.php。有可能你想使用“POST方法上传”(一个神奇的变量$_FILES)。阅读那里的所有五个小节,以了解它是如何工作并避免问题的。

哦,在将任何用户输入放入SQL查询之前,请务必使用mysql_real_escape_string()/mysql_escape_string()将其转义。希望这些东西不在你的榜样中,因为你试图保持简短的发布。

相关问题