在将文件与其他数据一起发送时,表单数据为空

时间:2015-10-07 05:26:50

标签: php jquery html ajax

大家好我想尝试发送表单数据和文件,但表单数据总是空的,无法上传文件

这是我的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>

和发送formdata到php的ajax是

$('#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');
                        }
});

和用于在pds文件夹中上传该文档的php文件

 <?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是一个文件夹位于这个php文件的位置但是我无法上传文件任何帮助请我在最后一天遇到这个问题

0 个答案:

没有答案