douyicao2199 2015-01-02 02:57
浏览 214
已采纳

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

please help me.

I have form like this in 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

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

<?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)

but the fact is:

(1) when I completed the form (both status and picture), it works normally.

(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. I should refresh them to get the update.

(3) when i just filling the image input, it works normally like case (1).

Please help why if($_SERVER['REQUEST_METHOD'] == "POST") failed to echo the input by ajax request when data input (status) is blank/empty.

thousands of thanks. :)

  • 写回答

1条回答 默认 最新

  • douzen1896 2015-01-02 03:22
    关注

    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. The onclick function should return false to do this.

    <button type="button" name="submit" onClick="write_wall_post();return false;">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?