提交表单后无法检索POST数据


unable to retrieve POST data after submiting form

大家好,

我试图使用jQuery将一些(POST(数据发送到PHP文件,但每次提交表单时都会出现"未定义索引"错误。

upload.html

<form id="formx" action="upload.php" onSubmit="return false" method="post" enctype="multipart/form-data">
<b>Upload new image</b> <br />
            <input type="button" id="get_file" value="Grab file">
            <div id="customfileupload">Select a file</div>
            <input type="file" id="file_input" name="uploadedfile">
            <br />
            <input type="submit" name="upload" id="upload" value="Upload image" />
        <div id="status"></div>
    </div>
</form>

upload.js

var options = { 
        target: '#status',  
        type: "POST",
        data: "dir=music",
        dataType: "html",
        beforeSubmit:  beforeSubmit, 
        uploadProgress: OnProgress,
        success:       afterSuccess,
        error: function(hr, ajaxOptions, thrownError)
        {
            status.html("<font color='red'> ERROR: unable to upload file.</font>"+thrownError);
        },
        resetForm: true  
    }; 
 $('#formx').submit(function(event) { 
        $(this).ajaxSubmit(options);             
        event.preventDefault();
    });

upload.php

<?php
echo "->".$_POST['dir'];
?>

很明显,我没有正确发送数据。我尝试过更改dataType,但仍然没有得到想要的结果。如果有人能就如何解决这个问题向我提供建议,我将不胜感激。

提前感谢

Alex

尝试将数据作为JavaScript对象发送,并添加要发送到的url。默认情况下,您的脚本不会从form属性中获取url。

data: {
    "dir": "music"
},
url: "upload.php"

取自jQuery文档

要发送到服务器的数据。如果还不是字符串。它被附加到GET请求的url中。看见processData选项以阻止此自动处理对象必须是键/值对如果值是Array,jQuery会序列化多个基于传统设置值的具有相同键的值(如下所述(。

https://api.jquery.com/jQuery.ajax/

dataType参数实际上是用于从php文件中获得的数据,它与您发送的数据无关。

话虽如此,我认为选项数组中应该有一个url元素,它应该将数据发送到upload.php,这样你就会有这样的东西:

var options = { 
        target: '#status',
        url: '/path/to/upload.php',
        type: "POST",
        data: "dir=music",
        dataType: "html",
        beforeSubmit:  beforeSubmit, 
        uploadProgress: OnProgress,
        success:       afterSuccess,
        error: function(hr, ajaxOptions, thrownError)
        {
            status.html("<font color='red'> ERROR: unable to upload file.</font>"+thrownError);
        },
        resetForm: true  
    };