douqie1852 2016-07-25 21:27
浏览 24

JQUERY结合了两个ajax调用

I have got this html

<div class="headChat"></div>
        <input type="file" id="chatfil" accept="image/*" name="file_in">
        <label for="chatfil">
            <img src="../img/camera.png" class="addphc">
        </label>

and this jquery

$(".Mesform").submit(function(){
        event.preventDefault();
        var val=$(this).siblings('textarea').val();
        var who=$(".headChat").text();
        var formData = new FormData($(".Mesform")[0]);
            if (val!="") { 
                $.ajax({
                    url: '../files/ajax.php',
                    type: 'GET',
                    cache: false,
                    contentType: false,
                    processData: false,
                    data:"ChatSent="+val+"&sento="+who,
                    success: function (data) {
                        $(".ChatDiv").append(data); 
                    }
                });
                $.ajax({
                    url: '../files/ajax.php',
                    type: 'POST',
                    cache: false,
                    contentType: false,
                    processData: false,
                    data:formData,
                    success: function (data) {
                        alert(data);
                    }
                });
            }
       });

and this in ajax.php

if(isset($_FILES['file_in']) && isset($_GET['ChatSent'])){
            $file_new_name="";
            $file=$_FILES['file_in'];
            $file_name=$file['name'];
            $file_tmp=$file['tmp_name'];
            $file_size=$file['size'];
            $file_error=$file['error'];
            $file_extension=explode('.', $file_name);
            $file_extension=strtolower(end($file_extension));
            $allowed=array('jpg','png');
            if (in_array($file_extension, $allowed)) {
                if ($file_error===0) {
                    if ($file_size<=2097152) {
                        $file_new_name=generateRandomString(15).rand(0,100).'.'.$file_extension;
                        $_SESSION['file_new_name']=$file_new_name;
                        $file_destination='../chatimg/'.$file_new_name;
                        if (!move_uploaded_file($file_tmp,$file_destination)) {
                        echo "<p class='filerror'>Error occurred.Please try again</p>";
                        exit();
                        }
                    }
                }
            }
            $ChatText=$_GET['ChatSent'];
            $ChatText=htmlspecialchars($ChatText);
            $ChatText=trim($ChatText);
            $Sento=$_GET['sento'];
            $Idquery=$con->query("SELECT Id FROM users WHERE Username='$Sento'");
            $Idrow=$Idquery->fetch_row();
            $IdTo=$Idrow[0];
            if ($IdTo=="") {
                     echo "Error";
                }else{
                        $Insert=$con->query("INSERT INTO chat (Message,From_,To_,`Date`,img) VALUES('$ChatText','$NameId','$IdTo',NOW(),'$file_new_name')");
                        if($Insert){
                        ?>  
                        <div class="fm">
                                <span><?php echo $ChatText; ?></span>
                        </div>
                        <br />
                        <?php
                        } //if insert
                }
    }

When i separate $_FILES['file_in'] and $_GET['ChatSent'] the both work.But together they do not work.I can't separate them because i have to insert both of them into database

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看