我有这个HTML
<div class="headChat"></div>
<input type="file" id="chatfil" accept="image/*" name="file_in">
<label for="chatfil">
<img src="../img/camera.png" class="addphc">
</label>
和这个jquery
$(".Mesform").submit(function(){
event.preventDefault();
var val=$(this).siblings('textarea').val();
var who=$(".headChat").text();
var formData = new FormData($(".Mesform")[0]);
if (val!="") {
$.ajax({
url: '../files/ajax.php',
type: 'GET',
cache: false,
contentType: false,
processData: false,
data:"ChatSent="+val+"&sento="+who,
success: function (data) {
$(".ChatDiv").append(data);
}
});
$.ajax({
url: '../files/ajax.php',
type: 'POST',
cache: false,
contentType: false,
processData: false,
data:formData,
success: function (data) {
alert(data);
}
});
}
});
,这在ajax.php中
if(isset($_FILES['file_in']) && isset($_GET['ChatSent'])){
$file_new_name="";
$file=$_FILES['file_in'];
$file_name=$file['name'];
$file_tmp=$file['tmp_name'];
$file_size=$file['size'];
$file_error=$file['error'];
$file_extension=explode('.', $file_name);
$file_extension=strtolower(end($file_extension));
$allowed=array('jpg','png');
if (in_array($file_extension, $allowed)) {
if ($file_error===0) {
if ($file_size<=2097152) {
$file_new_name=generateRandomString(15).rand(0,100).'.'.$file_extension;
$_SESSION['file_new_name']=$file_new_name;
$file_destination='../chatimg/'.$file_new_name;
if (!move_uploaded_file($file_tmp,$file_destination)) {
echo "<p class='filerror'>Error occurred.Please try again</p>";
exit();
}
}
}
}
$ChatText=$_GET['ChatSent'];
$ChatText=htmlspecialchars($ChatText);
$ChatText=trim($ChatText);
$Sento=$_GET['sento'];
$Idquery=$con->query("SELECT Id FROM users WHERE Username='$Sento'");
$Idrow=$Idquery->fetch_row();
$IdTo=$Idrow[0];
if ($IdTo=="") {
echo "Error";
}else{
$Insert=$con->query("INSERT INTO chat (Message,From_,To_,`Date`,img) VALUES('$ChatText','$NameId','$IdTo',NOW(),'$file_new_name')");
if($Insert){
?>
<div class="fm">
<span><?php echo $ChatText; ?></span>
</div>
<br />
<?php
} //if insert
}
}
当我将$ _FILES ['file_in']和$ _GET ['ChatSent']分开时,两者都有效。但是它们不起作用。我不能将它们分开,因为我必须将它们都插入数据库