Php html表单中的文件上载字段具有多个字段,仅对一个字段使用ajax
你好! 我有一个困难的时间与我的html表单之一,它应该有一个上传图像的字段。 我想做的是能够选择一个文件(图像)并使用ajax将其发送到php文件,而无需提交整个表单。如何在主表单中为该文件类型添加其他表单 例如,我有一张表格:Php html表单中的文件上载字段具有多个字段,仅对一个字段使用ajax,php,jquery,html,ajax,forms,Php,Jquery,Html,Ajax,Forms,你好! 我有一个困难的时间与我的html表单之一,它应该有一个上传图像的字段。 我想做的是能够选择一个文件(图像)并使用ajax将其发送到php文件,而无需提交整个表单。如何在主表单中为该文件类型添加其他表单 例如,我有一张表格: <form action="submit.php" method="POST" > <input type="text" name="title" /> <input type="text" name="descriptin" />
<form action="submit.php" method="POST" >
<input type="text" name="title" />
<input type="text" name="descriptin" />
<input type="file" name="images" onchange="save_img()" id="file" />
</form>
但它不起作用。有人能帮我举个简单的例子吗?谢谢大家!
PHP代码:
<?php
define('AJAX_REQUEST', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if(!AJAX_REQUEST) {die();}
require_once("../classes/admin.Class.php");
// Checking nline status
$Admin = new Admin;
$Admin -> OnLine();
$link = 'www.'.$_SERVER['SERVER_NAME'];
$name = time().'-'.$_FILES['file']['name'];
$extension = strtolower(substr($name, strpos($name, '.') + 1));
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$max_size = 20097152;
$tmp_name = $_FILES['file']['tmp_name'];
if(strlen($_FILES['file']['name'])>34)
exit('Image name must contain less than 30 characters!');
if(isset($name)) {
if(!empty($name)) {
if(($extension=='jpg'||$extension=='jpeg')&&($type=='image/jpeg'||$type='image/jpg')&&$size<=$max_size) {
//picture location
$location = '../../upload/foto/';
if(move_uploaded_file($tmp_name, $location.$name)) {
$size = getimagesize($location.$name);
if($size[0]>550){
$ratio = $size[0]/$size[1]; // width/height
if( $ratio > 1) {
$width = 550;
$height = 550/$ratio;
}
else {
$width = 550*$ratio;
$height = 550;
}
$new_image = imagecreatetruecolor($width,$height);
$image_source = imagecreatefromjpeg($location.$name);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagejpeg($new_image,$location.$name,100);
imagedestroy($new_image);
imagedestroy($image_source);
}
$db = new Connect;
$user_id = $db->prepare("SELECT id FROM vr_users WHERE id=:id");
$user_id -> execute(array(
'id' => intval($_COOKIE['check'])
));
$check = $user_id->fetch(PDO::FETCH_ASSOC);
$user_id = $check['id'];
$insert = $db->prepare("INSERT INTO vr_images SET id_post=0, link=:link,
id_user=:id_user");
$insert -> execute(array(
'link' => '/upload/foto/'.$name,
'id_user' => $user_id
));
echo "<div class='alerta_upload_index'><img src='/upload/ok.png' /><br/>Your image was uploaded successfully!<br />
<strong>http://$link/upload/foto/$name</strong></div>";
$images=$db->prepare("SELECT id, link FROM vr_images
WHERE id_user=:id_user AND id_post=:id_post ORDER BY id DESC");
$images -> execute(array(
'id_user' => $user_id,
'id_post' => 0
));
while ($check = $images->fetch(PDO::FETCH_ASSOC))
{
$dimensiune = getimagesize('../../'.$check['link']);
echo "<div class='fisier_incarcat'>
<div class='info_fisier'>
$dimensiune[0] x $dimensiune[1] - <a onclick='delete_img(".$check['id'].");' style='cursor:pointer'>[x]</a>
</div>
<img style='width:auto;height:auto;max-width:100px;max-height:90px;' src='".$check['link']."' />
</div>";
}
} else {
echo "Eroare";
}
} else {
echo "<div class='alerta_upload_eroare'>Your file should be a jpg/jpeg picture and less than 2 MB!</div>";
}
} else {
echo "Please, choose a file!";
}
}else {
echo "Error!";
}
?>
OnLine();
$link='www.$\u服务器['SERVER\u NAME'];
$name=time().'-'.$\文件['file']['name'];
$extension=strtolower(substr($name,strpos($name,'.')+1));
$type=$_文件['file']['type'];
$size=$_文件['file']['size'];
$max_size=20097152;
$tmp_name=$_FILES['file']['tmp_name'];
如果(strlen($_FILES['file']['name'])>34)
退出('图像名称必须包含少于30个字符!');
如果(isset($名称)){
如果(!空($name)){
如果($extension=='jpg'| |$extension=='jpeg')&&($type=='image/jpeg'| |$type='image/jpg')&&&$size550){
$ratio=$size[0]/$size[1];//宽度/高度
如果($ratio>1){
$width=550;
$height=550/$ratio;
}
否则{
$width=550*$ratio;
$height=550;
}
$new_image=imageCreateTureColor($width,$height);
$image\u source=imagecreatefromjpeg($location.$name);
imagecopyresampled($new_image,$image_source,0,0,0,$width,$height,$size[0],$size[1]);
图像JPEG($new_image,$location.$name,100);
imagedestroy($new_image);
imagedestroy($image\u source);
}
$db=新连接;
$user\u id=$db->prepare(“从vr\u用户中选择id,其中id=:id”);
$user\u id->执行(数组)(
'id'=>intval($\u COOKIE['check']))
));
$check=$user\U id->fetch(PDO::fetch\U ASSOC);
$user_id=$check['id'];
$insert=$db->prepare(“插入到vr_图像集id_post=0,link=:link,
id_user=:id_user”);
$insert->execute(数组)(
'link'=>'/upload/foto/'。$name,
'id\u user'=>$user\u id
));
echo“
您的图像已成功上载!
http://$link/upload/foto/$name”;
$images=$db->prepare(“选择id,从vr\U图像链接
其中id_user=:id_user和id_post=:id_post ORDER BY id DESC”);
$images->execute(数组)(
'id\u user'=>$user\u id,
“id_post”=>0
));
而($check=$images->fetch(PDO::fetch_ASSOC))
{
$dimensune=getimagesize(“../../.”.$check['link']);
回声“
$dimensune[0]x$dimensune[1]-[x]
";
}
}否则{
回声“爱神”;
}
}否则{
echo“您的文件应该是jpg/jpeg图片,并且小于2MB!”;
}
}否则{
echo“请选择一个文件!”;
}
}否则{
回声“错误!”;
}
?>
粘贴您的php代码我想了解如何将图像发送到我的php文件中,从那里我就可以开始了。非常感谢。在js上,在success函数中尝试console.log(JSON.stringify(data)),看看我们得到了什么。在调用AjaxNothing changed之前,还要对映像进行控制台日志。如何从该字段获取值?我使用var data=new FormData();每个(jQuery('#file')[0]。文件,函数(i,file){data.append('file-'+i,file);});但它不明白。
<?php
define('AJAX_REQUEST', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if(!AJAX_REQUEST) {die();}
require_once("../classes/admin.Class.php");
// Checking nline status
$Admin = new Admin;
$Admin -> OnLine();
$link = 'www.'.$_SERVER['SERVER_NAME'];
$name = time().'-'.$_FILES['file']['name'];
$extension = strtolower(substr($name, strpos($name, '.') + 1));
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$max_size = 20097152;
$tmp_name = $_FILES['file']['tmp_name'];
if(strlen($_FILES['file']['name'])>34)
exit('Image name must contain less than 30 characters!');
if(isset($name)) {
if(!empty($name)) {
if(($extension=='jpg'||$extension=='jpeg')&&($type=='image/jpeg'||$type='image/jpg')&&$size<=$max_size) {
//picture location
$location = '../../upload/foto/';
if(move_uploaded_file($tmp_name, $location.$name)) {
$size = getimagesize($location.$name);
if($size[0]>550){
$ratio = $size[0]/$size[1]; // width/height
if( $ratio > 1) {
$width = 550;
$height = 550/$ratio;
}
else {
$width = 550*$ratio;
$height = 550;
}
$new_image = imagecreatetruecolor($width,$height);
$image_source = imagecreatefromjpeg($location.$name);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagejpeg($new_image,$location.$name,100);
imagedestroy($new_image);
imagedestroy($image_source);
}
$db = new Connect;
$user_id = $db->prepare("SELECT id FROM vr_users WHERE id=:id");
$user_id -> execute(array(
'id' => intval($_COOKIE['check'])
));
$check = $user_id->fetch(PDO::FETCH_ASSOC);
$user_id = $check['id'];
$insert = $db->prepare("INSERT INTO vr_images SET id_post=0, link=:link,
id_user=:id_user");
$insert -> execute(array(
'link' => '/upload/foto/'.$name,
'id_user' => $user_id
));
echo "<div class='alerta_upload_index'><img src='/upload/ok.png' /><br/>Your image was uploaded successfully!<br />
<strong>http://$link/upload/foto/$name</strong></div>";
$images=$db->prepare("SELECT id, link FROM vr_images
WHERE id_user=:id_user AND id_post=:id_post ORDER BY id DESC");
$images -> execute(array(
'id_user' => $user_id,
'id_post' => 0
));
while ($check = $images->fetch(PDO::FETCH_ASSOC))
{
$dimensiune = getimagesize('../../'.$check['link']);
echo "<div class='fisier_incarcat'>
<div class='info_fisier'>
$dimensiune[0] x $dimensiune[1] - <a onclick='delete_img(".$check['id'].");' style='cursor:pointer'>[x]</a>
</div>
<img style='width:auto;height:auto;max-width:100px;max-height:90px;' src='".$check['link']."' />
</div>";
}
} else {
echo "Eroare";
}
} else {
echo "<div class='alerta_upload_eroare'>Your file should be a jpg/jpeg picture and less than 2 MB!</div>";
}
} else {
echo "Please, choose a file!";
}
}else {
echo "Error!";
}
?>