我正在尝试创建一个迷你聊天页面。我尝试提交一个带有一些文本和文件的表单,通过另一个php页面,而不是表单所在的位置。文本输入有效,但FILES []变为空白。我尝试测试错误可能是什么,但我的尝试都没有成功。任何帮助将非常感谢。 HTML:
<div class="inp">
<form id="dacform" name="chatf" method="post" enctype="multipart/form-data">
<div class="datpad">
<label id="txtcht"><h2>Title</h2></label>
<input onfocus="inputchf(this)" onblur="inputchb(this)" type="text"
name="chat_title" />
<label><h2>Description </h2><p style="display:inline-block;">(Max 140
characters)</p></label>
<!--<input onfocus="inputchf(this)" onblur="inputchb(this)" type="text"
name="chat_title" />-->
<!--<input type="textarea" name="chat_desc"/>-->
<textarea name="chat_desc" onfocus="textfoc(this)" onblur="textblur(this)">
</textarea>
<label><h2>Background Image</h2></label>
<input type="file" id="upload" class="custom-file-input" name="chat_back">
<!--<input type="submit" name="subm_chat" class="btn-style" value="Create
Chat">-->
<div class="btn-style">Create Chat</div>
</form>
</div>
</div>
</div>
Javascript:
if($.post('chatcre.php', $('#dacform').serialize())){alert("sent");}
chatcre.php文件:
<?php
session_start();
include 'dbconnect.php';
$username = $_SESSION['username'];
//start chat
function slugify($text)
{
$text = str_replace(' ', '_', $text);
$text = trim($text, '_');
if (empty($text)) {
return 'n-a';
}
return $text;
}
if(isset($_POST['chat_title']) ? $_POST['chat_title'] : null){
$chat_title = $_POST['chat_title'];
$chat_titleEnc = md5($chat_title);
$chat_desc = $_POST['chat_desc'];
$chat_date = date("Y/m/d h:i:sa");
$chat_index = $chat_title.$username.$chat_date;
$chat_index = md5($chat_index);
$chat_index = $chat_index.".html";
$errors= array();
$file_name = $_FILES['chat_back']['name'];
$file_size = $_FILES['chat_back']['size'];
$file_tmp = $_FILES['chat_back']['tmp_name'];
$file_type = $_FILES['chat_back']['type'];
$file_ext= explode('.',$_FILES['chat_back']['name']);
$file_ext = end($file_ext);
$file_ext= strtolower($file_ext);
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if(in_array($file_ext,$expensions) == ".jpg"){
$file_name = "1";
$file_name .=$username;
$file_name .= $chat_titleEnc;
$file_name=$file_name.time().".jpg";
$file_name = slugify($file_name);
}else if(in_array($file_ext,$expensions) == "jpeg"){
$file_name = "1";
$file_name .=$username;
$file_name .= $chat_titleEnc;
$file_name=$file_name.time().".jpeg";
$file_name = slugify($file_name);
}else{
$file_name = "1";
$file_name .=$username;
$file_name .= $chat_titleEnc;
$file_name=$file_name.time().".png";
$file_name = slugify($file_name);
}
if($file_size > 2097152) {
$errors[]='File size must be less than 2 MB';
}
if(empty($errors)==true) {
if(move_uploaded_file($file_tmp,"cimages/".$file_name)){
$chat_img = "cimages/".$file_name;
$sql_chat = "INSERT INTO `id1251041_udata`.`chats` (
`id` ,
`chat_title` ,
`chat_desc` ,
`chat_img` ,
`chat_wall`,
`chat_date` ,
`chat_authr`,
`chat_index`
)
VALUES (
NULL , '$chat_title', '$chat_desc', '$chat_img', 'None' , '$chat_date','$username','$chat_index'
)";
$chat_create = mysqli_query($conn,$sql_chat);
if($chat_create){echo "si"." ".$chat_date;}else{echo "nien".mysqli_error($chat_create);}
}//file upload yes
}
//end img
}
//end ifset chat