如何使用Ajax将表单数据传递到PHP,并使用jQuery显示错误消息,而无需页面刷新
我有一个带文件上传器的表格。我想在不刷新页面的情况下显示错误消息。我想我需要用jQuery显示错误,并用Ajax将文件传递给PHP脚本。我不知道该怎么做 这是我当前的代码。提交后,它会将您重定向到如何使用Ajax将表单数据传递到PHP,并使用jQuery显示错误消息,而无需页面刷新,php,jquery,ajax,forms,file,Php,Jquery,Ajax,Forms,File,我有一个带文件上传器的表格。我想在不刷新页面的情况下显示错误消息。我想我需要用jQuery显示错误,并用Ajax将文件传递给PHP脚本。我不知道该怎么做 这是我当前的代码。提交后,它会将您重定向到upload.php,并在那里显示错误/成功消息,但它对用户不是很友好 <form method="post" action="upload.php" enctype="multipart/form-data"> <div id="file_upload">
upload.php
,并在那里显示错误/成功消息,但它对用户不是很友好
<form method="post" action="upload.php" enctype="multipart/form-data">
<div id="file_upload">
<input type="file" name="file">
<input type="submit" value="Upload">
</div>
</form>
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists. Rename and try again.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($fileType != "zip" ) {
echo "Sorry, only zip files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
您可以将Ajax与
FormData()一起使用。
:
下面是我们的HTML代码:
<form method="post" onsubmit="return submitForm()" action="upload.php" enctype="multipart/form-data">
<div id="file_upload">
<input type="file" name="file">
<input type="submit" value="Upload">
</div>
</form>
您可以将Ajax与
FormData()
一起使用:
下面是我们的HTML代码:
<form method="post" onsubmit="return submitForm()" action="upload.php" enctype="multipart/form-data">
<div id="file_upload">
<input type="file" name="file">
<input type="submit" value="Upload">
</div>
</form>
在这里,您可以使用此代码。它会帮助你的 Html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="upload.php" enctype="multipart/form-data" class="upload_form">
<div id="file_upload">
<input type="file" name="file">
<input type="submit" value="Upload">
</div>
</form>
PHP
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists. Rename and try again.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($fileType != "zip" ) {
echo "Sorry, only zip files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
在这里,您可以使用此代码。它会帮助你的 Html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="upload.php" enctype="multipart/form-data" class="upload_form">
<div id="file_upload">
<input type="file" name="file">
<input type="submit" value="Upload">
</div>
</form>
PHP
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists. Rename and try again.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($fileType != "zip" ) {
echo "Sorry, only zip files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
根据您的代码尝试此选项
HTML
和Script
File添加letest.js库:
按照你的代码试试这个
HTML
和Script
File添加letest.js库: 使用jquery
ajax()
和FormData
上传文件并获得响应。使用jqueryajax()
和FormData
上传文件并获得响应。
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['file-0']['name']);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
//echo $target_file;/*
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists. Rename and try again.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file-0"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($fileType != "zip" ) {
echo "Sorry, only zip files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file-0"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file-0"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>