使用POST将Ajax变量发送到PHP

时间:2022-11-16 09:09:48

I am trying to find the best way to send variables from Javascript to PHP without GET method. I found a way to send through POST method with AJAX:

我试图找到从Javascript发送变量到没有GET方法的PHP的最佳方法。我找到了一种通过AJAX发送POST方法的方法:

<form method="POST" id="post" enctype="multipart/form-data">
                <input type="file" name="image_upload[]" id="img1" />
                <input type="file" name="image_upload[]" id="img2" />
                <input type="file" name="image_upload[]" id="img3" />
                <input type="text" name="description" id="description" />
                <textarea class="intext" name="editor" id="editor"></textarea>
                <input type="text" name="state" id="state" disabled="true" />
                <input type="text" name="city" id="city" disabled="true" />
                <input type="submit" id="submit" />
            </form>

And I am trying to submit the form with jQuery:

我试图用jQuery提交表单:

$('#post').submit(function (event) {
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: "cpage.php",
        data: {
            'variable1': 'content var1',
                'variable2': 'content var2'
        },
        success: function () {
            $('#post'), $('form').unbind('submit').submit();
        },
        error: function (name, err, desc) {
            alert(desc);
        }
    });

NOTE: the variable "position" has been declared before and works fine.

注意:变量“position”之前已经声明并且正常工作。

Result: I get "Internal Server Error" in the alert. Any ideas?

结果:警报中出现“内部服务器错误”。有任何想法吗?

1 个解决方案

#1


0  

First of All - show us what is going on the server side.

首先 - 告诉我们服务器端的情况。

And now about the files being sent:

现在关于发送的文件:

You should use FormData element for file submit threw Ajax, its not supported by old browser, the browsers that would support this are : ie>9, chrome > 7, opera > 12 safari >5, android > 3 gecko mobile > 2, opera mobile >12.

您应该使用FormData元素进行文件提交抛出Ajax,旧浏览器不支持,支持它的浏览器是:ie> 9,chrome> 7,opera> 12 safari> 5,android> 3 gecko mobile> 2,opera手机> 12。

Use something like this:

使用这样的东西:

    $('#post').submit(function (event) {
               event.preventDefault();

        if( window.FormData !== undefined ) //make sure that we can use FormData
        {

            var formData = new FormData($('form#post'));
                        $.ajax({
                                type: "POST",
                                url: "cpage.php",
                                data: formData ,
                                //Options to tell jQuery not to process data or worry about content-type. 
                                cache: false,
                                contentType: false,
                                processData: false,
                                success: function (data) {
                                                       console.log(data); // <- for debugging
                                                       $('#post'), $('form').unbind('submit').submit();
                               },
                               error: function (name, err, desc) {
                                     alert(desc);
                               }
                        });
            } else {
                //fallback 
            }
      });

As you can see I added console.log(data), try looking at the returned data to identify any other problems.

如您所见,我添加了console.log(数据),尝试查看返回的数据以识别任何其他问题。

#1


0  

First of All - show us what is going on the server side.

首先 - 告诉我们服务器端的情况。

And now about the files being sent:

现在关于发送的文件:

You should use FormData element for file submit threw Ajax, its not supported by old browser, the browsers that would support this are : ie>9, chrome > 7, opera > 12 safari >5, android > 3 gecko mobile > 2, opera mobile >12.

您应该使用FormData元素进行文件提交抛出Ajax,旧浏览器不支持,支持它的浏览器是:ie> 9,chrome> 7,opera> 12 safari> 5,android> 3 gecko mobile> 2,opera手机> 12。

Use something like this:

使用这样的东西:

    $('#post').submit(function (event) {
               event.preventDefault();

        if( window.FormData !== undefined ) //make sure that we can use FormData
        {

            var formData = new FormData($('form#post'));
                        $.ajax({
                                type: "POST",
                                url: "cpage.php",
                                data: formData ,
                                //Options to tell jQuery not to process data or worry about content-type. 
                                cache: false,
                                contentType: false,
                                processData: false,
                                success: function (data) {
                                                       console.log(data); // <- for debugging
                                                       $('#post'), $('form').unbind('submit').submit();
                               },
                               error: function (name, err, desc) {
                                     alert(desc);
                               }
                        });
            } else {
                //fallback 
            }
      });

As you can see I added console.log(data), try looking at the returned data to identify any other problems.

如您所见,我添加了console.log(数据),尝试查看返回的数据以识别任何其他问题。