使用AJAX传递数组

时间:2016-02-18 11:43:08

标签: php jquery arrays ajax

我习惯使用以下结构编写AJAX,我最终将变量发送到PHP

function requestToggle(type, user, elem) {
                    _(elem).innerHTML = 'please wait ...';
                    var ajax = ajaxObj("POST", "request_system.php");
                    ajax.onreadystatechange = function () {
                        if (ajaxReturn(ajax) == true) {
                            if (ajax.responseText == "request_sent") {
                                _(elem).innerHTML = 'OK Request Sent';
                            } else if (ajax.responseText == "unrequest_ok") {
                                _(elem).innerHTML = '<button onclick="requestToggle(\'request\',\'<?php echo $u; ?>\',\'requestBtn\')">Request Number</button>';
                            } else {
                                alert(ajax.responseText);
                                _(elem).innerHTML = 'Try again later';
                            }
                        }
                    }
                    ajax.send("type=" + type + "&user=" + user);
                }

我想要处理的示例是照片上传表单,PHP脚本使用的是$ _FILES数组,但我不确定如何使用AJAX将此数组传递给PHP。

这是PHP

<?php 
$result = "";
if (isset($_FILES["avatar"]["name"]) && $_FILES["avatar"]["tmp_name"] != ""){
    $fileName = $_FILES["avatar"]["name"];
    $fileTmpLoc = $_FILES["avatar"]["tmp_name"];
    $fileType = $_FILES["avatar"]["type"];
    $fileSize = $_FILES["avatar"]["size"];
    $fileErrorMsg = $_FILES["avatar"]["error"];
    $kaboom = explode(".", $fileName);
    $fileExt = end($kaboom);
    list($width, $height) = getimagesize($fileTmpLoc);
    if($width < 10 || $height < 10){
        $result = "That image has no dimensions";
        echo $result;
        exit();
    }
    $db_file_name = rand(100000000000,999999999999).".".$fileExt;
    if($fileSize > 1048576) {
        $result = "Your image file was larger than 1mb";
        echo $result;
        exit();
    } else if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
        $result = "Please only JPG, GIF or PNG images";
        echo $result;
        exit();
    } else if ($fileErrorMsg == 1) {
        $result = "An unknown error occurred";
        echo $result;
        exit();
    }
    $sql = "SELECT profilePicture FROM User WHERE username='$log_username' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);
    $avatar = $row[0];

        //delete old pic if set
    if($avatar != ""){
        $picurl = "users/$log_username/$avatar"; 
        if (file_exists($picurl)) { unlink($picurl); }
    }
        //move file from temp folder to users folder
    $moveResult = move_uploaded_file($fileTmpLoc, "users/$log_username/$db_file_name");
    if ($moveResult != true) {
        $result = "File upload failed";
        echo $result;
        exit();
    }
    include_once("image_resize.php");
        //replace original file with resized version
    $target_file = "users/$log_username/$db_file_name";
    $resized_file = "users/$log_username/$db_file_name";
    $wmax = 400;
    $hmax = 600;
    img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
    $sql = "UPDATE User SET profilePicture='$db_file_name' WHERE username='$log_username' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    mysqli_close($db_conx);
    //header("location: user.php?u=$log_username");
       $result = "upload_success";
        echo $result;

    exit();
}
?>

上传表格

$avatar_form = '<div class="bhoechie-tab-content" id="uploadphoto">';
        $avatar_form .= '<center>';
        $avatar_form .= '<form id="avatar_form"" method="post" enctype="multipart/form-data">';
        $avatar_form .= '<h1>Change avatar</h1>';
        $avatar_form .= '<input type="file" name="avatar" required>';
        $avatar_form .= '<p><input type="submit" value="Upload"></p>';
        $avatar_form .= '<p id="status"></p>';
        $avatar_form .= '</form>';
        $avatar_form .= '</center></div>';

1 个答案:

答案 0 :(得分:1)

你可以很容易地传递数组,例如ajax.send(&#34; type =&#34; + type +&#34;&amp; user =&#34; + user +&#34;&amp; files =&#34; +文件);

没有看到你的其余代码,我无法提供完整的答案,但我假设你以某种方式在js中创建一个文件数组并希望将其传递给php?如果是这样,变量&#39;文件&#39;然后将在PHP中使用:

$ files = $ _REQUEST [&#39; files&#39;];