简体   繁体   English

如何使用AJAX和表单数据发送多个数据

[英]How to send multiple data using AJAX along with the Form Data

I have this ajax code to send form data to a php file called upload.php 我有这个ajax代码将表单数据发送到一个名为upload.phpphp文件

My Problem is: I can pass the form-data without any problem, but i i cannot pass another data with it . 我的问题是:我可以毫无问题地传递表单数据,但是我i cannot pass another data with it Everytime I do that, ajax is not pasing data to upload.php 每次我这样做时, ajax都不粘贴数据以upload.php

Here's my ajax : 这是我的ajax

    function upload(fileid, imgno) {
                $(document).ready(function () {

                    var data = new FormData();
                    jQuery.each(jQuery('#' + fileid)[0].files, function (i, file) {
                        data.append('file-' + i, file);
                    });


                    $.ajax({
                        url: 'includes/upload.php',
                        dataType: 'text', // what to expect back from the PHP script, if anything
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: {
                            data: data,
                            imgno: imgno
                        },
                        type: 'post',
                        success: function (php_script_response) {
                            alert(php_script_response); // display response from the PHP script, if any
                        }

                    });
                });
            }

and is is my upload.php 这是我的upload.php

    if (isset($_FILES['file0'])) { //it doesn't pass this condition
      echo 'B';

    }
    $imageNO = $_POST['imgno']; //this gives undefined index.

How can I fix this problem? 我该如何解决这个问题?

You can append any extra field to the formData object like so: 您可以将任何其他字段附加到formData对象,如下所示:

formData.append(name, value);

and then send it to server side using ajax: 然后使用ajax将其发送到服务器端:

data: formData,

Then fetch on the server side using $_POST[''] OR $_GET[''] whichever method you use 然后使用$_POST['']$_GET['']在服务器端获取,无论使用哪种方法

You are sending "file-0" by ajax, but in a condition (PHP) you are checking "file0". 您正在通过ajax发送“ file-0”,但是在条件(PHP)中,您正在检查“ file0”。 Run network monitor and see what the browser is really sending. 运行网络监视器,查看浏览器实际发送的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM