之前可能有人问过这个问题,但我在这里和谷歌上搜索过,我读过的每个答案都不起作用。
我必须解决的问题是制作一个包含名字、姓氏、电子邮件和图像的表格。然后将数据传递到数据库并将文件也上传到数据库。目前我的代码在我按下提交后没有做任何事情。在我添加文件框之前,它会将数据插入到我的数据库中。
HTML
<form id="myForm" method ="post" enctype="multipart/form-data">
First Name: <input type="text" name="fname" id="fname"> <br>
Last Name: <input type="text" name="lname" id="lname"> <br>
Email: <input type="text" name="email" id="email"> <br>
Image: <input type="file" name="image" id="image"> <br>
<button type="button" name="btnSubmit" id="btnSubmit"> Submit </button>
</form>
AJAX/JS
$("#btnSubmit").click(function(){
var formData = new FormData($(this)[0]);
$.ajax({
type: 'POST',
url: 'form2.php',
data: formData,
success: function (data) {
alert(data)
},
});
});
PHP
$upload = basename($_FILES['image']['name']);
$type = substr($upload, strrpos($upload, '.') + 1);
$size = $_FILES['image']['size']/1024;
if ($_FILES["image"]["error"] > 0)
{
echo "Error: " . $_FILES["image"]["error"] . "<br>";
}
else
{
echo "Upload: " . $upload . "<br>";
echo "Type: " . $type . "<br>";
echo "Size: " . $size . " kB<br>";
}
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
echo "You Entered <br />";
echo "<b>First Name:</b> ". $fname . "<br />";
echo "<b>Last Name:</b> ". $lname . "<br />";
echo "<b>Email:</b> ". $email . "<br />";