I found this code in Upload Image Files without Refreshing Page using Ajax and Jquery and modification my form
<?
require_once("include/main.php");
dbconn(false);
loggedinorreturn();
if (get_user_class() < UC_SYSOP){
stderr("Извините", "Доступа нет.");
}
$path = "uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
if($_POST['addnew'] == 'yes'){
if(strlen($name)) {
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats)) {
if($size<(1024*1024)) {
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name)) {
echo "<img src='uploads/".$actual_image_name."'>";
} else echo "Ошибка!";
} else echo "Максимум 1 MB";
} else echo "Неверный формат файла";
} else echo "Выберите изображение..!";
$image = sqlesc($actual_image_name);
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
$added = sqlesc(get_date_time());
$name = sqlesc($_POST['name']);
$code_production = sqlesc($_POST['code_production']);
$catname = $_POST['catname'];
$napolnitel = $_POST['napolnitel'];
$price = sqlesc($_POST['price']);
$about_product = sqlesc($_POST['about_product']);
$info_pack = sqlesc($_POST['info_pack']);
//$somthing = sqlesc(implode(', ', $_POST['somthing']));
mysql_query("INSERT INTO production (added, image, name, code_production, catname, napolnitel, price, about_product, info_pack)
VALUES(".$added.", ".$image.", ".$name.", ".$code_production.", ".sqlesc($catname).", ".$napolnitel.", ".$price.", ".$about_product.", ".$info_pack.")") or sqlerr(__FILE__, __LINE__);
//echo '<script>document.location.replace("add.php");</script>';
header('Location: ./index.php');
}
}
?>
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.form.js"></script>
<script type="text/javascript" >
$(document).ready(function() {
$('#photoimg').live('change', function() {
$("#preview").html('');
$("#preview").html('<img src="loader.gif" title="Идет загрузка...." alt="Идет загрузка...."/>');
$("#imageform").ajaxForm({
target: '#preview'
}).submit();
});
});
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="2"><tr><td align="center">
<form id="imageform" method="post" enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="addnew" value="yes">
<table class="tableinborder" cellSpacing=1 cellPadding=6 width="100%" border=0>
<tr>
<td class="tablea" height="20" width="25%"><p align="left">Название:</td>
<td class="tablea" height="20" width="75%"><p align="left"><input size="60" name="name"></td>
</tr>
<tr>
<td class="tablea" height="20" width="25%"><p align="left">Наполнитель:</td>
<td class="tablea" height="20" width="75%"><p align="left"><input size="60" name="napolnitel"></td>
</tr>
<tr>
<td class="tablea" height="20" width="25%"><p align="left">Код:</td>
<td class="tablea" height="20" width="75%"><p align="left"><input size="10" name="code_production"></td>
</tr>
<?
$genre = "";
$res = mysql_query("SELECT id, catname FROM categories ORDER BY id ASC") or die;
while ($row = mysql_fetch_array($res)){
$ros = mysql_query("SELECT id, name FROM subcategories WHERE catid = '".$row['id']."' ORDER BY name ASC") or die;
$genre .= "<option class=\\"speciald\\" disabled value=\\"".$row['catname']."\\">".$row['catname']."</option>\
";
while ($showSub = mysql_fetch_array($ros)) {
$genre .= "<option value=\\"".$showSub['id']."\\"> - ".$showSub['name']."</option>\
";
}
}
?>
<tr>
<td class="tablea" height="20" width="25%"><p align="left">Каталог:</td>
<td class="tablea" height="20" width="75%"><p align="left"><select size="10" name="catname"><?echo $genre?></select></td>
</tr>
<tr>
<td class="tablea" height="20" width="25%"><p align="left">Описание товара:</td>
<td class="tablea" height="20" width="75%"><p align="left"><textarea name="about_product" rows="10" cols="60"></textarea></td>
</tr>
<tr>
<td class="tablea" height="20" width="25%"><p align="left">Упаковка и кол-во:</td>
<td class="tablea" height="20" width="75%"><p align="left"><input size="60" name="info_pack"></td>
</tr>
<tr>
<td class="tablea" height="20" width="25%"><p align="left">Цена:</td>
<td class="tablea" height="20" width="75%"><p align="left"><input size="7" name="price"> руб.</td>
</tr>
<tr>
<td class="tablea" height="20" width="25%"><p align="left">Картинка:</td>
<td class="tablea" height="20" width="75%"><p align="left">
<input type="file" name="photoimg" id="photoimg" /><div id='preview'></div></td>
</tr>
<tr>
<td class="tablea" height="20" width="100%" colspan="4"><center><input type="submit" value="Добавить товар"></center></td>
</tr>
</table>
</form>
The problem is, when i choose image, the form automatic submited.
Without give me upload another image or check form before.
And after the form automatic submitted, it dosn’t do header(). All action do in the <div id=‘preview’></div> with header error
How to do Only preview image after choose, and submit all the form with button submit (in my form in russian Добавить товар)
Thanks