1
votes

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
Where do you set $uploadedfile?Barmar
in the code, i have written //some variable declaration and image validation, this is declaration variable section. it's not the point since image can uploaded successfully, and when i filling the image input, it was update successfully. The problem is when i left the image input empty. the data was submitted to database successfully, but the content doesnt updated automatically (need refresh page). but when I filling the image input (whether with filling status input or not), the content will updated automaticallysetyo
You're probably getting an error when you process the form with no uploaded file. Have you checked the Network tab for errors?Barmar
The bug is probably in the code that the comments stand in for.Barmar
there is no error.. it seems fine. when i change this code like this (by changing position that insert to database first, print something, than upload image), it works perfectly. <?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"; } } ?>setyo

1 Answers

0
votes

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