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

麻烦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. :)

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;">