如何在这个php ajax表单中发送多个数据

时间:2018-05-16 12:49:13

标签: php ajax

这里是带有id名称'文件'的ajax脚本输入没问题,我希望输入数据来自id' vid'还使用ajax发送如何以后的代码

<input type="file" name="file" id="file" /> 

<div style="background:url() no-repeat"> 
<span id="uploaded_image" ><img src=" '.$row["carimg"].' " height="150" width="225" class="img-thumbnail" /></span></div>

这里是ajax代码

<script> 
$(document).ready(function(){ 
    $(document).on('change', '#file', function(){ 
        var name = document.getElementById("file").files[0]... 
        var form_data = new FormData(); 
        var ext = name.split('.').pop().toLowerCase(); 
        if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1) 
        { 
            alert("Invalid Image File"); 
        } 
        var oFReader = new FileReader(); 
        oFReader.readAsDataURL(document.getEleme... 
        var f = document.getElementById("file").files[0]... 
        var fsize = f.size||f.fileSize; 
        if(fsize > 2000000) 
        { 
            alert("Image File Size is very big"); 
        } 
        else 
        { 
            form_data.append("file", document.getElementById('file').files[0]... 
            $.ajax({ 
                    url:"up1.php", 
                    method:"POST", 
                    data: form_data, 
                    contentType: false, 
                    cache: false, 
                    processData: false, 
                beforeSend:function(){ 
                    $('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>"); 
                }, 
                success:function(data) 
                { 
                    $('#uploaded_image').html(data); 
                } 
            }); 
        } 
    }); 
}); 
</script> 

这是php

<?php

//upload.php
$id = $_FILES["vid"];

if($_FILES["file"]["name"] != '')
{
    $test = explode('.', $_FILES["file"]["name"]);
    $ext = end($test);
    $name = gen_random_string(6)."n" . '.' . $ext;
    $location = 'assets/img/cars/' . $name;  
    move_uploaded_file($_FILES["file"]["tmp_name"], $location);
    echo $id;
    echo '<img src="'.$location.'" height="150" width="225" class="img-thumbnail" />';

    $sql = "UPDATE vehicles SET carimg='".$location."' WHERE vid='".$id."'";
}
mysqli_query($connect, $sql);  
?>

如何在这个php ajax表单中发送多个数据,这个只能发送一个数据请帮助

1 个答案:

答案 0 :(得分:0)

form_data.append("file", document.getElementById('file').files[0]下方添加

这句话

form_data.append("vid",document.getElementById('vid').innerHTML);

因为你是POST,所以从你的php文件:

$vid = $_POST['vid'];