【问题标题】:ajax not uploading file pic to database and serverajax没有将文件图片上传到数据库和服务器
【发布时间】:2014-02-18 11:41:09
【问题描述】:

我的问题是,当我出于某种原因使用 ajax 提交时,我的脚本没有在 serv 上上传图片并将其插入数据库。如果我使用 php action="file.php" 提交表单,它可以工作。这是我的 ajax 脚本和 php 之一。我看不出问题出在哪里以及为什么它适用于 php submit 而不适用于 ajax。提前谢谢。

<script>
$(function() {
//twitter bootstrap script
    $("button#submit").click(function(){
            $.ajax({
            type: "POST",
        url: "engine/app/process.php",
        data: $(\'form.edit\').serialize(),
        dataType:\'json\',
            success: function(data){
                    $("#bday").html(data.a)
                     $("#locatie").html(data.b)
                     $("#descriere").html(data.c)
                     $("#interes").html(data.d)
                     $("#status").html(data.e)
                     $(".img").html(data.f)

                   $("#myModal").modal(\'hide\');    
                },
        error: function(){
            alert("failure");
            }
              });
    });
});
</script>

php 脚本

<?php
require_once('../core/dbconfig.php');
$dbc = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASS, DB_NAME);
$nume=$_POST['nume'];
$bday=$_POST['bday'];
$locatie=$_POST['locatie'];
$status=$_POST['status'];
$interese=$_POST['interese'];
$despre=$_POST['descriere'];
$id_user=$_POST['id_user'];

$query="Update `users` SET username='$nume',bday='$bday',Locatie='$locatie',Relatie='$status',Interese='$interese',Descriere='$despre' where id='$id_user' ";   
                                $result=mysqli_query($dbc,$query) or die("query failed: " . mysqli_error($dbc));


$path = '../../images/users/'.$id_user.'/'; 
if(!is_dir($path)){
  mkdir($path,0755);
}
    $valid_formats = array("jpg", "png", "gif", "bmp", "jpeg", "JPG");

            $name = $_FILES['img1']['name'];
            $size = $_FILES['img1']['size'];


            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;*/
                            $actual_image_name=$id_user.'.'.$ext;
                            $tmp = $_FILES['img1']['tmp_name'];
                            if(move_uploaded_file($tmp, $path.$actual_image_name))
                                {
                                $queryz="Insert into Galerie (ID_User,Poza,Poza_Profil) VALUES ('$id_user','$actual_image_name',1)";    
                                $resultz=mysqli_query($dbc,$queryz) or die("query failed: " . mysqli_error($dbc));                              
                                }
                            else
                                echo "failed";
                        }
                        else
                        echo "Image file size max 1 MB";                    
                        }
                        else
                        echo "Invalid file format..";   
                }   



echo json_encode(array("a" => $bday, "b" => $locatie,"c" => $despre,"d" => $interese,"e" => $name,"f" => ""));
?>

【问题讨论】:

标签: php jquery mysql ajax


【解决方案1】:

serialize 方法将您的表单字段数据放在符合 application/x-www-form-urlencoded 内容类型的字符串中,用于将表单提交给服务器进行处理,而文件则在 multipart/form- 编码的请求中提交数据内容类型,因此,序列化忽略文件输入。

你应该使用formData

var form = new FormData(document.getElementById('your_frm_id'));
var file = document.getElementById('img1').files[0];
    if (file) {
        $('#sent_progress').show();
        form.append('img1', file);
    }

在 ajax 中使用

data: form,

在此处阅读更多信息https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects

或者你可以使用一些 Jquery 插件来上传你的文件和表单 希望对你有帮助

【讨论】:

  • thnx,这是很好的信息,但我无法修改我的脚本,我只是为上传图像制作了一个新脚本。所以实际上在 1 次提交时,我调用 2 个函数,一个用于正常信息,1 个用于上传:)
【解决方案2】:

仅通过 Ajax 请求,您无法将文件上传到服务器。更好的选择是使用jQuery Form

此链接还包括一些示例代码,按照文档操作并实现您想要的。

看看如何在Ajax based image upload上实现jQuery Form插件

【讨论】:

  • Thnx,我又添加了一个用于上传图片的脚本。 Coudnt 修改我的我不太擅长 ajax,我也想保留 json 返回。但它也适用于 2 个脚本,因此请寻求帮助 :).&lt;script type="text/javascript" src="js/jquery.form.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" &gt; $(document).ready(function() { $("#imageform").ajaxForm({ target: \'#preview \' }) }); &lt;/script&gt;
  • @Downvoters 我可以知道在这里投反对票的原因是什么吗?
猜你喜欢
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 2019-02-20
相关资源
最近更新 更多