doulou1989 2012-05-10 09:40
浏览 26
已采纳

使用ajax将数据传递到php脚本

Am stuck here. I've been trying to upload some photos and at the same time pass the id of the album (value is in a hidden form in the form) to the same php script that processes the upload. but i dont know how to pass the album id seprately this is the code.

Js

input.addEventListener("change", function (evt) {
        document.getElementById("response").innerHTML = "<img src='../assets/admin/images/loading.gif' />"
        var i = 0, len = this.files.length, img, reader, file;

    for ( ; i < len; i++ ) {
        file = this.files[i];

        if (!!file.type.match(/image.*/)) {
            if ( window.FileReader ) {
                reader = new FileReader();
                reader.onloadend = function (e) { 
                    showUploadedItem(e.target.result, file.fileName);
                };
                reader.readAsDataURL(file);
            }
            if (formdata) {
                formdata.append("images[]", file);
            }
        }   
    }

    if (formdata) {
        $.ajax({
            url: "../assets/admin/ajaxupload/upload.php",
            type: "POST",
            data: formdata,
            processData: false,
            contentType: false,
            success: function (res) {
                document.getElementById("response").innerHTML = res; 
            }
        });
    }

PHP

//how do i retrieve the given album id value that was passed.

foreach ($_FILES["images"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $name = $_FILES["images"]["name"][$key];
        move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "../../uploads/pics/" .$_FILES['images']['name'][$key]);


    }
}
echo "<p>Successfully Uploaded Images</p>";

Please i need a reply asap thanks.

  • 写回答

1条回答 默认 最新

  • doudu5029 2012-05-10 09:58
    关注

    (sorry i have not big time to explain but hope help)

    in your ajax php file

    $errors = array();  // initialize empty error array
    if (sizeof($errors) == 0) {
           ...
           if ($securimage->check($captcha) == false) {
                $errors['captcha_error'] = 'wrong code';
            }
     }
      if (sizeof($errors) == 0) {
            // no errors, send the form
            $return = array('error' => 0, 'message' => 'OK');
            die(json_encode($return));
        } else {
           $errmsg = '';
            foreach ($errors as $key => $error) {
                // set up error messages to display with each field
                $errmsg .= " - {$error}
    ";
            }
            $return = array('error' => 1, 'message' => $errmsg);
            die(json_encode($return));
       }
    

    your js

    url: 'ajax.php',
            type: "POST",
            data: $('#formID').serialize(),
            success: function(msg) {
                try {
                    //                    alert( "Data Saved: " + msg );
                    json = jQuery.parseJSON(msg);
                    //                      alert(json.error)
                    if (json.error == 0) {
                    } catch(e) {
                    alert("Sorry, there was an error parsing the json");
                }
            },
            error: function(msg) {
                alert("Ajax request failed");
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角