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

麻烦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;">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路