機能するコードがいくつかありますが、アップロード画像フィールドを追加したいのですが、成功しません。私の現在のコードは次のとおりです。
形:
<form id="add_product_form" enctype="multipart/form-data">
<input id="uploadFile" type="file" name="image" class="img" /><br />
<input id="status" type="checkbox" checked /><br />
<input id="product" type="text" /><br />
<label for="button" id="response"></label>
<input type="button" id="button" value="Добави" /><br />
</form>
jQuery:
<script type="text/javascript">
$('document').ready(function(){
$('#button').click(function(){
var image = $('input[type=file]').val().split('\\').pop();
function chkb(bool){ if(bool) return 1; return 0; } var status=chkb($("#status").is(':checked'));
if($('#product').val()==""){ alert("enter name"); return false; } else { var product = $('#product').val(); }
jQuery.post("products_add_process.php", {
image: image,
status: status,
product: product
},
function(data, textStatus) {
$('#response').html(data);
if(data == 1){
$('#response').html("OK");
$('#response').css('color','green');
document.getElementById("add_product_form").reset();
} else {
$('#response').html("Not OK");
$('#response').css('color','red');
}
});
});
});
</script>
products_add_process.php:
<?php
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
$status = $_POST['status'];
$product = $_POST['product'];
if($image_name == '') {
echo "<script>alert('Select image')</script>";
exit();
} else {
$random_digit=rand(0000000000,9999999999);
$image=$random_digit.$image_name;
move_uploaded_file($image_tmp_name,"uploads/$image");
$query=mysql_query("INSERT INTO products(product, image, status) VALUES ('$product', '$image', '$status')");
if(mysql_affected_rows()>0){
$response = 1;
echo "1";
} else {
$response = 2;
echo "2";
}
}
?>
私はアラート「画像を選択」を入れてから、それ$image_name
が空であることを理解し、データタイプを送信したいと言うコードをどこかに置く必要があるかもしれません。フォームに追加しようとしましenctype="multipart/form-data"
たが、うまくいきません。
このコードは多くのファイルに含まれており、コードの大部分を編集するのが難しいため、非常に大きな変更を加えることなく、データを送信するために既存のコードのどこに何を追加する必要があります。
おそらく、そのフォームとjQueryが他のマザーファイルで呼び出されるphpファイルにあり、構造が次のようなものであることを追加する必要があります。
products.php /call products_add.php/
products_add.php /where is the form and jQuery/
products_add_process.php /called from products_add.php to upload data/
ps私の問題をうまく説明していない場合は申し訳ありませんが、私はすぐにstackoverflowに参加し、質問を投稿する方法をまだ学んでいます:)