2

Hi All I am trying to send a form data along with file but form data is always empty and unable to upload a file

here is my html

    <form class="form-class" id="userjob_form" enctype="multipart/form-data">
        <div class="form_button">Name</div>
        <input type="text" name="username" id="user_name" class="form_button" required>
        <br>
        <div class="form_button form_button_margin">Email</div>
        <input type="email" name="usermail" id="user_mail" class="form_button" required>
        <div class="form_button form_button_margin">Phone Number</div>
        <input type="number" name="userphone" id="user_phone" class="form_button" required="" max-length="10" pattern="[0-9]{10}" title="Enter a valid phone number.">
        <div class="form_button_margin">Resume</div>
        <input type="file" name="resume" id="upload_file" class="" required>
        <input type="submit" value="SUBMIT" id ="fill-jobform" class="form_button_margin-2"/>
    </form>

and ajax which send formdata to php is as

$('#userjob_form').on('submit', function(e) {
                        //var job_id = $('fieldset').attr('id');

                            e.preventDefault();
                            //alert($('#upload_file')[0].files[0]);
                            var fd = new FormData(this);
                            fd.append('resume', $('#upload_file')[0].files[0]);
                            fd.append("user_name", $('#user_name').val());
                            fd.append("user_name", $('#user_name').val());
                            fd.append("candidate_email", $('#user_mail').val());
                            fd.append("candidate_phone", $('#user_phone').val());
                            console.log(fd);
                var file_name = $('input[type=file]')[0].files[0].name.split(' ').join('-');
    var ext = file_name.split('.').pop();
                        if(ext == "txt" || ext == "html" || ext == "htm" || ext == "doc" || ext == "docx" || ext == "pdf" || ext == "rtf"){
                            //$('.submitting').show();
                            $.ajax({ 
                                    url: 'data/formprocessing.php',
                                    method: 'POST',
                                    dataType:'JSON',
                                    data: fd,
                                    processData: false,  // tell jQuery not to process the data
                                    contentType: false,
                                    success: function(data) {

                                        alert(data);

                                    }

            });
                        }else{
                            alert('wrong format');
                        }
});

and php file which is used to uplaod that document in pds folder

 <?php
header("Content-Type: text/javascript; charset=utf-8");
$username = "root";
$password = '';
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("rr",$dbhandle) 
  or die("Could not select examples");


if(!isset($_FILES['resume'][['name']])) {
    echo $response_array['status'] = 'success1';
}
else {
    // Validate uploaded file
    $fileName = $_FILES['resume']['name']; // file name
    $fileExt = substr($fileName, strrpos($fileName, '.') + 1); // file extension
    $fileSize = $_FILES['resume']['size']/1024; // size in KBs
    $filePath = $_FILES['resume']['tmp_path']; // file path
    $id = $_POST['username'];
    $username = $_POST['useremail']['name'];
    $profileImg = $_POST['userphone']['name'];
    $displayImg = $_FILES['resume']['name'];
            move_uploaded_file($_FILES['resume']['tmp_path'],
              "pds/" . time().'-' . $_FILES['resume']['name']);
              echo $response_array['status'] = 'success2';
    }
?>

pds is a folder lies where is this php file but I am unable to uplaod file any help please I am stuck in this problem for last day

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
golucoder
  • 43
  • 1
  • 7
  • You are sending data with JSON format to the php file while in php code your header expects javascript as content type. And moreover JSON will not help uploading files. Please check this [link](http://stackoverflow.com/questions/23980733/jquery-ajax-file-upload-php) – Sagar Khatri Oct 07 '15 at 05:35
  • Thanks buddy a huge thanks you just saved my day – golucoder Oct 07 '15 at 05:55

0 Answers0