如何更改方法POST以命名提交的属性

时间:2016-05-20 13:01:38

标签: php ajax post

对于uploadform,我使用带有ajax的表单,如下所示:

表格:

HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient httpclient = builder.build();
HttpGet httpget = new HttpGet(TEST_WEB_PAGE);
HttpResponse response = httpclient.execute(httpget);
InputStream content = response.getEntity().getContent();
OutputStream htmlStream = null;
File htmlFile = new File(ROOT + "etc/html/demo_apache_" + new Date() + ".html");
try {
    htmlStream = new FileOutputStream(htmlFile);
    byte[] buffer = new byte[8 * 1024];
    int bytesRead;
    while ((bytesRead = content.read(buffer)) != -1) {
        htmlStream.write(buffer, 0, bytesRead);
    }
} finally {
    if (htmlStream != null)
        htmlStream.close();
}

ajax:

<form id="sfmFiler" enctype="multipart/form-data">
    <input type="file" name="multiUpload" id="multiUpload" multiple />
    <input type="submit" name="submitHandler" id="submitHandler" class="buttonUpload" value="Upload">
    </form>

和php:

$.ajax({
            type:"POST",
            url:this.config.uploadUrl,
            data:data,
            cache: false,
            contentType: false,
            processData: false,
            success:function(rponse){
                $("#"+ids).hide();
                var obj = $(".sfmfiles").get();
                $.each(obj,function(k,fle){
                    if($(fle).attr("rel") == rponse){
                        $(fle).slideUp("normal", function(){ $(this).remove(); });
                    }
                });
                if (f+1 < file.length) {
                    self._uploader(file,f+1);
                }
            }
        });

现在我想将方法​​POST更改为submit:

的名称attrib

所以所需的php看起来像这样:

if($_SERVER['REQUEST_METHOD'] == "POST")
{
    if(move_uploaded_file($_FILES['file']['tmp_name'],   "uploads/".$_FILES['file']['name']))
    {
        echo($_POST['index']); // to validate
    }
    exit;
}

而不是

if($_POST['submitHandler'])
{
...

我应该在ajax中更改什么才能使其正常工作?

更新ajax:

if($_SERVER['REQUEST_METHOD'] == "POST")
{
...

1 个答案:

答案 0 :(得分:0)

就我而言,只需添加方法就可以了:&#34; POST&#34;在我的ajax电话中

数据参数传递类型参数中的

将允许您区分两个表单值。 如果条件

,您可以使用$ _POST [&#39; type&#39;]获取类型值
$.ajax({
            method:"POST", //add method post so you can access with $_POST['']
            url:this.config.uploadUrl,
            data:{'data':data,'type':'passTypeName1'},
            cache: false,
            contentType: false,
            processData: false,
            success:function(rponse){
                $("#"+ids).hide();
                var obj = $(".sfmfiles").get();
                $.each(obj,function(k,fle){
                    if($(fle).attr("rel") == rponse){
                        $(fle).slideUp("normal", function(){ $(this).remove(); });
                    }
                });
                if (f+1 < file.length) {
                    self._uploader(file,f+1);
                }
            }
        });

所以你的服务器端代码就像这样

if($_POST['type']=='passTypeName1'){
//code
}elseif($_POST['type']=='passTypeName2'){
//  code
}