这是我的代码,其中包括jquery php和html代码我想用ajax上传文件。 在这段代码中,我无法上传文件。 这适用于所有其他文本数据,但它不上传文件.file可能是pdf doc等
<script>
$(document).ready(function(){
$("#submit").click(function(){
var name = $("#name").val();
var email = $("#email").val();
//var password = $("#password").val();
var contact = $("#contact").val();
var applying_for = $("#applying_for").val();
var degree = $("#degree").val();
var experience = $("#experience").val();
var user_cv = $("#user_cv").val();
//var password = $("#password").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'name1='+ name + '&email1='+ email + '&contact='+ contact+ '°ree='+ degree+ '&applying_for='+ applying_for+ '&user_cv='+ user_cv+ '&experience='+ experience;
if(name==''||email==''||applying_for==''||contact==''||experience=='')
{
alert("Please Fill All Fields");
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "ajaxsubmit.php",
enctype: 'multipart/form-data',
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
}
return false;
});
});
</script>
<?php
$error="could not connected";
$connection = mysqli_connect("localhost", "root", "root","brainmob_web_db") or die($error); // Establishing Connection with Server..
//echo 'connected';
//$db = mysql_select_db("mydba", $connection); // Selecting Database
//Fetching Values from URL
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
@$file_name=$_FILES['user_cv']['name'];
$extension=strtolower(substr($file_name,strpos($file_name,'.')+1));
@$size=$_FILES['user_cv']['size'];
@$type=$_FILES['user_cv']['type'];
@$tmp_name=$_FILES['user_cv']['tmp_name'];
//$max_size=100000;//in bytes
//$error=$_FILES['file']['error'];
if(isset($file_name)){
if(!empty($file_name)){
/* to specify type of data
if(($extension=='jpeg'||$extension=='jpg')&&(type='image/jpeg'&&type='image/jpeg')){
}*/
//this for any type of data
$location='uploads_files/';
if(move_uploaded_file($tmp_name,$location.$file_name)){
echo 'uploaded';
}
}else{
echo 'please choose a file';
}
}
$name=$_POST['name1'];
$email=$_POST['email1'];
$experience=$_POST['experience'];
$contact=$_POST['contact'];
//$file=$_POST['file'];
$subject=$_POST['applying_for'];
$degree=$_POST['degree'];
$ip=get_client_ip();
//$browser = get_browser();
//print_r($browser);
$browser=$_SERVER['HTTP_USER_AGENT'];;
print_r($_POST);
print_r($_FILES);
//Insert query
echo $query = "insert into brain_career(career_name, career_email, career_contact,career_experience,career_highest_degree,career_subject,career_file,career_ip,career_browser_info) values ('$name', '$email','$contact','$experience','$degree','$subject',,'$file','$ip','$browser')";
if($query_run=mysqli_query($connection,$query)){
echo 'succesfully inserted';
}//echo "Form Submitted Succesfully";
//print_r(mysqli_error($connection));
//mysqli_close($connection); // Connection Closed
?>
<form name="contactus_form" id="contactus_form" enctype="multipart/form-data">
<div class="form-group">
<input type="text" id="name" class="form-control contro" value="" placeholder="Name*" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Name*'">
</div>
<div class="form-group">
<input type="text" id="email" class="form-control contro" value="" placeholder="Email*" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Email*'">
</div>
<div class="form-group">
<input type="text" id="contact" class="form-control contro" value="" placeholder="Contact Number*" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Contact number'">
</div>
<div class="form-group">
<input type="text" id="degree"class="form-control contro" value="" placeholder="Highest Degree*" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Highest Degree'">
<!-- <select id="degree"class="form-control">
<option value="">Highest Degree </option>
</select>-->
</div>
<div class="form-group">
<input type="text" id="applying_for"class="form-control contro" value="" placeholder="Applying For" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Applying For'">
</div>
<div class="form-group">
<input type="text" id="experience" class="form-control contro" value="" placeholder="Experince in Year" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Experince in Year'">
</div>
<div class="form-group">
<div class="field">
<input type="file" name="user_cv" id="user_cv" class="inputfile inputfile-6" />
<label for="user_cv" class="file-upload"> <span class="form-control attach" id="user_cv_name">Upload Resume</span>
<strong class="browse-btn butn">Browse</strong></label>
</div>
</div>
<button id="submit" type="submit" class="btn-style sub">SUBMIT</button>
</form>
答案 0 :(得分:1)
要使用ajax()
上传文件,您必须使用FormData
,因为ajax中没有enctype
。
例如:
var file_data = $('#pic').prop('files')[0]; // here pic is the id of input type image
var form_data = new FormData();
form_data.append('file', file_data);
// You can add as many key : value pair here like
form_data.append('var1', val1);
$.ajax({
url : 'upload.php', // point to server-side PHP script
dataType : 'text', // what to expect back from the PHP script, if anything
cache : false,
contentType : false,
processData : false,
data : form_data,
type : 'post',
success : function(output){
alert(output); // display response from the PHP script, if any
}
});
upload.php的
// To receive file
$_FILES['file'];
// To receive other values
$_REQUEST['var1'];