gpt4 book ai didi

php - 上传文件不支持ajax

转载 作者:行者123 更新时间:2023-11-29 07:44:11 25 4
gpt4 key购买 nike

我厌倦了解决这个问题。我的表单无法使用ajax上传文件,但无需ajax即可工作。这是我的代码

插入页面.php

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>

<script type="text/javascript">

$('document').ready(function(){

$('#btn').click(function(event){
event.preventDefault();
var x=$('#form1').serializeArray();
$.post(
$('#form1').attr('action'),
x,
function(){
$('.hasil').html('Berhasil Insert');
$('.hasil').fadeOut(5000);
$('input').val("");
$('textarea').val("");
})
})

})

</script>

</head>
<?php

$tanggal = date("Y-m-d");


echo"

<body>
<form id='form1' enctype='multipart/form-data' action='simpanpage.php' method='post'>

<table>
<tr>
<td> Foto </td> <td> : </td> <td> <input type=file name=fotoh id=fotoh> </td>
</tr>
<tr>
<td> Judul </td> <td> : </td> <td> <input type=text name=judul id=judul maxlength='50' size='50' required placeholder='Masukan Judul'> </td>
</tr>
<tr>
<td> Deskripsi </td> <td> : </td> <td> <textarea required id=deskripsi name='deskripsi' > </textarea> </td>
</tr>
<tr>
<td> Isi </td> <td> : </td> <td> <textarea required id=isi name='isi'> </textarea> </td>
</tr>
<tr>
<td> Label </td> <td> : </td> <td> <input type=text id=label name=label placeholder='Masukan Label'><input type=hidden name=tgl_dibuat id=tgl_dibuat value=".$tanggal."> </td>
</tr>

</table>

<br>
<button id='btn'>Save</button>
<br> <div class='hasil'> </div>
</form>

</body>
";

?>

koneks.php

<?php

class database{

private $dbHost = "localhost";
private $dbUser = "root";
private $dbPass = "";
private $dbName = "oop_blog";

function connectMySQL(){
mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
mysql_select_db($this->dbName) or die("Database tidak ada!");
}

function manageinsertPage($judul, $isi, $tgl_dibuat, $deskripsi, $label){
$lokasi = $_FILES["fotoh"]["tmp_name"];
$namafoto = $_FILES["fotoh"]["name"];
$direktori = "gambar/$namafoto";

if (move_uploaded_file($lokasi, $direktori))
{
$qry = mysql_query("INSERT INTO pages(judul,isi,foto,tgl_dibuat,deskripsi,label) VALUES('$judul','$isi','$namafoto','$tgl_dibuat','$deskripsi','$label')");

echo"berhasil Insert Bersama Foto";
}

else{
$qry1 = mysql_query("INSERT INTO pages(judul,isi,tgl_dibuat,deskripsi,label) VALUES('$judul','$isi','$tgl_dibuat','$deskripsi','$label')");
echo"Berhasil";
}
}

?>

simpanpage.php

<?php 

include "koneks.php";

$dbi = new database;

$dbi->connectMYSQL();

$dbi->manageinsertPage($_POST["judul"],$_POST["isi"],$_POST["tgl_dibuat"],$_POST["deskripsi"],$_POST["label"]);

?>

我只是感到困惑,因为我的表单无法使用ajax读取$_FILES['fotoh']['name'];,这导致我上传失败。数据库中的答案始终为空。我需要帮助,非常感谢

最佳答案

检查此处,已测试

$('document').ready(function(){

$('#btn').click(function(event){
event.preventDefault();
$.ajax({
url : $('#form1').attr('action'),
type: "POST",
data : new FormData($('#form1')[0]),
processData: false,
contentType: false,
success:function(data){

}
});
})

})

关于php - 上传文件不支持ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28270951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com