Returning PHP to Ajax
<p>I have a form to upload a new avatar but I am now trying to use AJAX to give the user an error message on the same page if there is one. Currently the AJAX is not receiving anything back from the PHP in the separate file. Do I remove the form action? </p>

<p><strong>AJAX:</strong> </p>

<pre><code>$("#avatar_form").submit(function(){
        $.ajax({url: "photo_system.php", success: function(result){
            //Print the error message here
            $("#status").html(result);
        }});
    });
</code></pre>

<p><strong>FORM:</strong></p>

<pre><code>$avatar_form =&lt;div class="bhoechie-tab-content" id="uploadphoto"&gt;';
$avatar_form .= '&lt;center&gt;';
$avatar_form .= '&lt;form id="avatar_form"" method="post" enctype="multipart/form-data"&gt;';
$avatar_form .= '&lt;h1&gt;Change avatar&lt;/h1&gt;';
$avatar_form .= '&lt;input type="file" name="avatar" required&gt;';
$avatar_form .= '&lt;p&gt;&lt;input type="submit" value="Upload"&gt;&lt;/p&gt;';
$avatar_form .= '&lt;p id="status"&gt;&lt;/p&gt;';
$avatar_form .= '&lt;/form&gt;';
$avatar_form .= '&lt;/center&gt;&lt;/div&gt;';
</code></pre>

<p><strong>PHP:</strong></p>

<pre><code>&lt;?php
include_once("check_login_status.php");
if($user_ok != true || $log_username == "") {
    exit();
}
?&gt;&lt;?php 
$result = "";
if (isset($_FILES["avatar"]["name"]) &amp;&amp; $_FILES["avatar"]["tmp_name"] != ""){
    $fileName = $_FILES["avatar"]["name"];
    $fileTmpLoc = $_FILES["avatar"]["tmp_name"];
    $fileType = $_FILES["avatar"]["type"];
    $fileSize = $_FILES["avatar"]["size"];
    $fileErrorMsg = $_FILES["avatar"]["error"];
    $kaboom = explode(".", $fileName);
    $fileExt = end($kaboom);
    list($width, $height) = getimagesize($fileTmpLoc);
    if($width &lt; 10 || $height &lt; 10){
        $result = "That image has no dimensions";
        echo $result;
        exit();
    }
    $db_file_name = rand(100000000000,999999999999).".".$fileExt;
    if($fileSize &gt; 1048576) {
        $result = "Your image file was larger than 1mb";
        echo $result;
        exit();
    } else if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
        $result = "Please only JPG, GIF or PNG images";
        echo $result;
        exit();
    } else if ($fileErrorMsg == 1) {
        $result = "An unknown error occurred";
        echo $result;
        exit();
    }
    $sql = "SELECT profilePicture FROM User WHERE username='$log_username' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row = mysqli_fetch_row($query);
    $avatar = $row[0];

        //delete old pic if set
    if($avatar != ""){
        $picurl = "users/$log_username/$avatar"; 
        if (file_exists($picurl)) { unlink($picurl); }
    }
        //move file from temp folder to users folder
    $moveResult = move_uploaded_file($fileTmpLoc, "users/$log_username/$db_file_name");
    if ($moveResult != true) {
        $result = "File upload failed";
        echo $result;
        exit();
    }
    include_once("image_resize.php");
        //replace original file with resized version
    $target_file = "users/$log_username/$db_file_name";
    $resized_file = "users/$log_username/$db_file_name";
    $wmax = 400;
    $hmax = 600;
    img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
    $sql = "UPDATE User SET profilePicture='$db_file_name' WHERE username='$log_username' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    mysqli_close($db_conx);
    //header("location: user.php?u=$log_username");
       $result = "upload_success";
        echo $result;

    exit();
}
?&gt;
</code></pre>

<p>Just to confirm that the photo upload process did work before I added the AJAX but now it does not upload a photo. That is because I have removed the form action</p>