简体   繁体   English

if($ _ SERVER ['REQUEST_METHOD'] ==“ POST”)遇到麻烦

[英]trouble with if($_SERVER['REQUEST_METHOD'] == “POST”)

please help me. 请帮我。

I have form like this in index.php 我在index.php中有这样的形式

<form id="statusForm" enctype="multipart/form-data" method="post">
    <textarea name="statusText" role="textbox" id="wallpost"></textarea>
    <input id="photo_input" type="file" name="photo_input" />
    <input type="hidden" name="to_id" value="1" > 
    <button type="button" name="submit" onClick="write_wall_post();">
</form>
<div id="content"></div>

then i have .js file to handle this form 然后我有.js文件来处理这种形式

function write_wall_post()
{

var formData = new FormData($("#statusForm")[0]);
  $.ajax({
        type: "POST",
        url: "act_post_status.php",
        data: formData, 
        success: function(data){
            $("#wallpost").val("");             
            $("#photo_input").val('');
            var newStatus=data;
            $(newStatus).hide().prependTo("#content").fadeIn(2000);
        },
        processData: false,  // tell jQuery not to process the data
        contentType: false,
        cache:false
   });
}

and i have act_post_status.php to process this file submit 我有act_post_status.php来处理此文件提交

<?php 
//some configuration
if($_SERVER['REQUEST_METHOD'] == "POST")
{   
    //some variable declaration and image validation
    //Original Image
    if(move_uploaded_file($uploadedfile, $path.$time.'.'.$ext))
    {
        $is_image=1;
    }
    else
        echo "failed";
    }
}


 //inserting data to database
   $status = trim(strip_tags(htmlspecialchars($_POST["statusText"])));
   mysql_query("insert into news (status,is_image) values ('$status','$is_image')");
   echo "<div class='post'>$status</div>";
?>

the scenario i want is: when user input data (status), then click submit button, the content automatically show the update (handled by jquery) 我想要的情况是:当用户输入数据(状态),然后单击提交按钮时,内容自动显示更新(由jquery处理)

but the fact is: 但事实是:

(1) when I completed the form (both status and picture), it works normally. (1)当我填写表格(状态和图片)时,它可以正常工作。

(2) but when I completed just data form (filling status input only), it was submitted to database successfully, but the content don't update automatically. (2)但是,当我仅填写数据表单(仅填写状态输入)时,它已成功提交到数据库,但是内容不会自动更新。 I should refresh them to get the update. 我应该刷新它们以获取更新。

(3) when i just filling the image input, it works normally like case (1). (3)当我仅填充图像输入时,它的工作原理与情况(1)相同。

Please help why if($_SERVER['REQUEST_METHOD'] == "POST") failed to echo the input by ajax request when data input (status) is blank/empty. 请帮助为什么当数据输入(状态)为空白/空时if($ _ SERVER ['REQUEST_METHOD'] ==“ POST”)无法通过ajax请求回显输入。

thousands of thanks. 成千上万的感谢。 :) :)

I'm not sure why it matters whether you leave the file input unfilled, but you need to disable the normal form submission when you use AJAX. 我不确定为什么不填写文件输入为何重要,但是使用AJAX时需要禁用常规表单提交。 The onclick function should return false to do this. onclick函数应返回false来执行此操作。

<button type="button" name="submit" onClick="write_wall_post();return false;">

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

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