這是我的html表單,它通過php驗證了ajax請求。但提交後,它顯示下面的錯誤我:Php文件沒有上傳ajax請求
Notice: Undefined index: img in D:\Software Installed\xampp\htdocs\wix\users\insert_galary.php on line 12
如果我刪除表格的文件部分,然後它的正常工作,也當我檢查
if(isset($_POST['Submit']) && $_POST['Submit'] == "Upload galary")
那麼它無法驗證我的數據。
HTML表單
<p><a href="javascript:void(null);" onclick="showDialog2();">Add more</a></p>
<div id="dialog-modal2" title="Upload your galary image" style="display: none;">
<form method="post" action="insert_galary.php" id="myForm2" enctype="multipart/form-data" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Image title</td>
<td><input type="text" name="g_title" placeholder="Image title" class="tr" /></td>
</tr>
<tr>
<td>Upload image</td>
<td><input type="file" name="file" class="tr"/></td>
</tr>
<tr>
<td>Image link</td>
<td><input type="text" name="g_link" placeholder="Image link" class="tr"/> <span style="color:#030;">* must have http://</span>
</td>
</tr>
<tr>
<td><input type="submit" id="sub2" value="Upload Galary" name="Submit" class="submit"/></td>
</tr>
</table>
</form>
<span id="result2"></span>
<script type="text/javascript">
$("#sub2").click(function() {
$.post($("#myForm2").attr("action"),
$("#myForm2 :input").serializeArray(),
function(info){ $("#result2").html(info);
});
clearInput();
});
$("#myForm2").submit(function() {
return false;
});
</script>
</div>
function showDialog2()
{
$("#dialog-modal2").dialog(
{
width: 610,
height: 350,
open: function(event, ui)
{
var textarea = $('<textarea style="height: 276px;">');
$(textarea).redactor({
focus: true,
autoresize: false,
initCallback: function()
{
this.set('<p>Lorem...</p>');
}
});
}
});
}
PHP代碼:(insert_galary.php)
if(isset($_POST['Submit']) && $_POST['Submit'] == "Upload Galary")
{
$current_date = date('Y-m-d H:i:s');
$g_title = inputvalid($_POST['g_title']);
$g_link = inputvalid($_POST['g_link']);
$gid = mt_rand(100000, 999999);
$file = $_FILES["file"]["name"];
$type = $_FILES["file"]["type"];
$size = ($_FILES["file"]["size"]/1024);
$temp = $_FILES["file"]["tmp_name"];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$galary_pic = $gid.".".$extension;
$galary_directory = "../galary_images/";
$err = array();
if(isset($g_link) && isset($g_title) && isset($file))
{
if(empty($g_link) && empty($g_title) && empty($file))
$err[] = "All field require";
else
{
if(empty($g_title))
$err[] = "blog title require";
elseif(strlen($g_title) > 250)
$err[] = "blog title is too long";
elseif(strlen($g_title) < 2)
$err[] = "blog title is too small";
if(strlen($g_link) > 250)
$err[] = "image link is too long";
if(empty($file))
$err[] = "Upload your picture";
elseif(!in_array($extension, $allowedExts))
$err[] = "Uploaded file must be gif, jpeg, jpg, png format";
elseif($size > 500)
$err[] = "Uploaded file must be within 500kb";
}
}
if(!empty($err))
{
echo "<div class='error'>";
foreach($err as $er)
{
echo "<font color=red>$er.</font><br/>";
}
echo "</div>";
echo "<br/>";
}
else
{
if($insert = mysql_query("INSERT INTO galary VALUES('', '', '', '', '$username', '$g_title', '', '$galary_pic', '$g_link', '$current_date', '')"))
{
define('DESIRED_IMAGE_WIDTH', 338);
define('DESIRED_IMAGE_HEIGHT', 338);
$source_path = $_FILES['file']['tmp_name'];
list($source_width, $source_height, $source_type) = getimagesize($source_path);
switch ($source_type) {
case IMAGETYPE_GIF:
$source_gdim = imagecreatefromgif($source_path);
break;
case IMAGETYPE_JPEG:
$source_gdim = imagecreatefromjpeg($source_path);
break;
case IMAGETYPE_PNG:
$source_gdim = imagecreatefrompng($source_path);
break;
}
$source_aspect_ratio = $source_width/$source_height;
$desired_aspect_ratio = DESIRED_IMAGE_WIDTH/DESIRED_IMAGE_HEIGHT;
if ($source_aspect_ratio > $desired_aspect_ratio) {
$temp_height = DESIRED_IMAGE_HEIGHT;
$temp_width = (int) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio);
} else {
$temp_width = DESIRED_IMAGE_WIDTH;
$temp_height = (int) (DESIRED_IMAGE_WIDTH/$source_aspect_ratio);
}
$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
imagecopyresampled(
$temp_gdim,
$source_gdim,
0, 0,
0, 0,
$temp_width, $temp_height,
$source_width, $source_height
);
$x0 = ($temp_width - DESIRED_IMAGE_WIDTH)/2;
$y0 = ($temp_height - DESIRED_IMAGE_HEIGHT)/2;
$desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
imagecopy(
$desired_gdim,
$temp_gdim,
0, 0,
$x0, $y0,
DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
);
$desired_gdim;
//$file = $_FILES["file"]["name"];
//header('Content-type: image/jpeg');
//imagejpeg($desired_gdim, $file);
//imagejpeg($desired_gdim, $source_path);
imagejpeg($desired_gdim, "$upload_directory/$galary_pic");
//header("Location: img/");
echo "<div class=success>";
echo "successfully uploaded new image.";
echo "</div>";
header("Refresh:3, url=all_galary.php");
}
else
{
echo "Sorry, I can't upload your galary";
echo mysql_error();
}
}
}
?>
誰能告訴我,爲什麼它顯示我的錯誤信息?
更新錯誤消息:
說明:未定義指數:文件d:\軟件安裝\ XAMPP \ htdocs中\威克斯\用戶\上線insert_galary.php 16
說明:未定義指數: D:\ Software Installed \ xampp \ htdocs \ wix \ users \ insert_galary.php中的文件在線18
注意:未定義指數:文件中的d:未定義指數:文件在d:\軟件安裝\ XAMPP \ htdocs中\威克斯安裝\ XAMPP \ htdocs中\威克斯\用戶\在線insert_galary.php 19
通知\ SOFTWARE \用戶\ insert_galary.php第21行 對不起,我不能上傳galaryColumn數在1行
我修改,但仍然我得到同樣的錯誤消息。 – Alex
您是否重新加載頁面?請給我確切的錯誤信息。 @Alex –
是的,我重新加載了頁面。看到我的更新錯誤消息。 – Alex