Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to send complete form through AJAX ?

When i receive file in php I am only getting post there not image

<form action="" method="post" enctype="multipart/form-data" id="posting">
      <img src="<?php echo $user_data['profile']; ?>" alt = "<?php echo $user_data['firstName']; ?>" class='post_pro'/>
      <textarea name="post" placeholder="Hey <?php echo $user_data['firstName'] ?>,want to share something with us..."></textarea>
      <input type="file" name="post_pic" id="post_pic" style="color:transparent">
      <input type="submit" value="SHARE">
   </form>
$("#posting").on('submit',function(e){
         e.preventDefault();
        $.ajax({

            url  : "request/post.php",
            type : "POST",
            data : new FormData(this),
            dataType : "text",
            beforeSend : function(http){
                $("#posting").val("SHARING...");
            },
            success : function(response,status,http){ 
                 $("#posting").val("SHARE");
                 alert(response);

            },
            error : function(http,status,error){
                $("#show").html("<span class=\"error\" data-icon ='&#xea0f;'>Something went wrong with server.Try Again</span>");
                $('#posting').val("SHARE");
            }

        })
    })

1 Answer

Shane Oliver
Shane Oliver
19,977 Points

You need to serialize your form data

$.ajax({
    data: $("posting").serialize(),
    // rest of code
});