<input type="file" name="file">
с той же страницы не определено?вот мой код:
Обработчик PHP: (proccess.php, который ajax отправляет данные на эту страницу):
<?php
$mysqli = new mysqli('localhost', 'root', '', 'msgsys');
if(mysqli_connect_errno()) {
print_r("<h4 id='senterror'>Connection Failed: " . mysqli_connect_errno()."</h4>");
exit();
}
$email = $_POST['email'];
$file = $_FILES['file']['name']; //i'll try to give an object property to sure that the object exists;
$file2 = $_POST['file']; //the same attemp;
print_r($file);
if($stmt = $mysqli -> prepare("SELECT uid FROM user WHERE eaddress=?")) {
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
while ($row = mysqli_fetch_row($result)) {
$recUid = $row[0];
}
$stmt->close();
if (!$result || mysqli_num_rows($result) <= 0) {
print_r("<h4 id='senterror'>You Can not Mailing To Who doesn't exists!</h4>");
$mysqli->close();
exit();
} else {
date_default_timezone_set('ASIA/Tehran');
$today = date('m/d/Y-H:i:s');
$stmt = $mysqli->prepare("INSERT INTO message (sdeltag,rdeltag,rreadtag,timesent,body,subjecttxt,sender,receiver) VALUES ('0','0','0',?,?,?,'1',?)");
$stmt->bind_param("ssss",$today,$_POST['txt'], $_POST['subject'],$recUid);
$stmt->execute();
print_r("<h4 id='mailsent'>Message Sent Successfully!</h4>");
$stmt->close();
}
$mysqli->close();
}
?>
Ajax:
$(document).ready(function () {
$('#sendmail').submit(function () {
var that = this;
$('#response').html("<b>Loading response...</b>");
$.ajax({
type: 'POST',
url: 'proccess.php',
data: $(that).serialize()
})
.done(function (data) {
$('#response').html(data);
})
.fail(function () {
alert("Posting failed.");
});
this.reset();
return false;
});
});
Я перепишу часть AJAX, как показано ниже, и отлично работает для меня:
$(document).ready(function () {
$('#sendmail').submit(function () {
var formData = new FormData($('form')[0]);
$('#response').html("<b>Loading response...</b>");
$.ajax({
url: 'proccess.php', //Server script to process data
type: 'POST',
data: formData,
async: false,
success: function (msg) {
$('#response').html(msg);
},
cache: false,
contentType: false,
processData: false
});
this.reset();
return false;
});
});
Вы можете использовать этот AJAX для сохранения значений ваших полей.
$(document).ready(function (e) {
$("#YourButtonID").on('submit',(function(e) {
e.preventDefault();
var fileValue = $('#file').val();
if(fileValue !='')
{
$.ajax({
url: "submitFile.php",
type: "POST",
data: new FormData(this),
contentType: false,
processData:false,
success: function(data)
{
$("#message").html('Image Uploaded Successfully..!!');
$('#ShowImage').show();
//$('#file').val('');
$('#ShowImage').attr("src",data);
}
});
}
else
{
alert("Please Choose file!");
return false;
}
}));
});
Это HTML часть
<form id="uploadimage" method="post" enctype="multipart/form-data">
<div id="message"></div><br/>
<img src="" id="ShowImage" style="display:none;"><br/><br/>
<label>File:</label>
<input type="file" name="file" id="file" /><br/><br/>
<input type="submit" value="Upload" name="submit" id="submit" />
</form>
И на стороне PHP написать этот код
<?php
if($_FILES["file"] !='')
{
$name = strtolower($_FILES['file']['name']);
$File_Ext = substr($name, strrpos($name, '.'));
if($name !='' && $File_Ext !='')
{
$NewFileName = time().$File_Ext;
}
$cmpltPath = "uploads/".$NewFileName;
move_uploaded_file($_FILES['file']['tmp_name'],$cmpltPath) ;
echo $cmpltPath;
}
else
{
echo "Failure!!";
}
?>