0
我想通過ajax插入數據並通過php上傳圖片,問題是,當我嘗試將數據與圖片一起插入單個按鈕時,數據將不會被插入並且圖片被上傳。當我試圖只插入數據,而不是圖片文件(我把它留空),它運作良好(只需插入數據(文本))。通過ajax插入數據並通過php錯誤上傳圖片
這是我AJAX:
$(document).ready(function() {
$("#submit1").click(function(){
jQuery(function($) {
vals = [];
$('select.menu_paket').each(function() { vals.push($(this).val()); });
vals2=vals.join(',')
});
alert('clicked!');
//alert(vals2);
var nama_paket=document.getElementById('nama_paket').value;
var keterangan_paket=document.getElementById('keterangan_paket').value;
var harga=document.getElementById('harga').value;
$.ajax({
type: "POST",
url: "insert_paket_f.php",
data: { data : vals2 ,nama_paket : nama_paket,keterangan_paket: keterangan_paket,harga : harga},
cache: false,
success: function(){
alert("OK");
}
});
});
});
,這是我形式:
<form id="form2" name="form2" method="post" action="upload_pic_paket.php" enctype="multipart/form-data"><!-- -->
<p>Nama Paket :
<input name="nama_paket" type="text" id="nama_paket" />
</p>
<p>Keterangan Paket :
<input name="keterangan_paket" type="text" id="keterangan_paket" />
</p>
<p>Harga Paket :
<input name="harga" type="text" id="harga" />
</p>
<p>Gambar Paket:
<input type="file" name="picture" id="picture"/>
</p>
<table border="1">
<tr><td colspan="4">Masukan Menu Paket</td></tr>
<tr><td><label>Menu Paket : </label></td><td><select id="menu_paket" name="menu_paket" class="menu_paket">
</select></td>
<td><input type="button" class="addRow" value="Add Row"/></td>
<td><input type="button" class="delRow" value="Delete Row"/></td></tr>
</table>
<p>
<input type="button" id="submit1" name="Submit" value="Simpan" onclick="saveForm(); return false;"/>
</p>
</form>
,這是我upload_pic_paket.php
<?php
$fileName = $_FILES['picture']['name'];
$fileSize = $_FILES['picture']['size'];
$fileError = $_FILES['picture']['error'];
if($fileSize > 0 || $fileError == 0){
$move = move_uploaded_file($_FILES['picture']['tmp_name'], 'images/paket/'.$fileName);
?>
我做錯了什麼?
我改變了我的想法,找到一些簡單的解決方案。通過ajax上傳圖片too.got一些相當不錯的tutorial.thanks。 –