我是ajax的新手,我在尝试从我的php上传页面打印回显值时遇到了一些麻烦。我的目标是获得回声,这是结果,并在我的跨度div中打印。
这是我的JavaScript。
$(document).ready(function (e) {
$('#myForm').on('submit',(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
type:'POST',
url: $(this).attr('action'),
data:formData,
cache:false,
contentType: false,
processData: false,
success:function(data){
console.log("success");
console.log(data);
$("#myForm")[0].reset();
$("#result").append(data);
},
error: function(data){
console.log("error");
console.log(data);
}
});
}));
});
这是我的HTML
<form action="postphoto" id="myForm" method="POST" enctype="multipart/form-data">
<input type="file" onchange="readURL(this);" name="profilepic" id="ImageBrowse"/>
<br>
<img id="blah" src="#" alt="" height="300" width="400" />
<br>
<br>
<textarea class="form-control" name="description" rows="1" cols="40" placeholder="Description"></textarea>
<br>
<textarea class="form-control" name="tagone" rows="1" cols="10" placeholder="Tag"></textarea>
<textarea class="form-control" name="tagtwo" rows="1" cols="10" placeholder="Tag"></textarea>
<textarea class="form-control" name="tagthree" rows="1" cols="10" placeholder="Tag"></textarea>
<br>
<textarea class="form-control" name="content" rows="5" cols="40" placeholder="Inspiration behind this.."></textarea> <br>
<textarea class="form-control" name="url" rows="1" cols="10" placeholder="Url"></textarea>
<em>Separate words in the url by a dash. For example, fashion-fashion.</em><br>
<span id="result"></span>
<br><input type="submit" class="form-control" id="sub" name="uploadpic" value="Upload">
</form>
这是php
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
$rand_dir_name = mysqli_real_escape_string($db_conx,$rand_dir_name);
mkdir("userdata/user_photos/$rand_dir_name");
if (file_exists("userdata/user_photos/$rand_dir_name/".@$_FILES["profilepic"]["name"]))
{
echo @$_FILES["profilepic"]["name"]." Already exists";
}
else
{
$query = "SELECT * FROM photos WHERE link='$url'";
$query = $db_conx->query($query);
$checks = mysqli_num_rows($query);
if ($checks == 0) {
move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"userdata/user_photos/$rand_dir_name/".$_FILES["profilepic"]["name"]);
//echo "Uploaded and stored in: userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"];
$profile_pic_name = @$_FILES["profilepic"]["name"];
$profile_pic_name = mysqli_real_escape_string($db_conx,$profile_pic_name);
$img_id_before_md5 = "$rand_dir_name/$profile_pic_name";
$img_id = md5($img_id_before_md5);
$profile_pic_query = "INSERT INTO photos VALUES ('','test','$user','$date','$description','http://website.com/userdata/user_photos/$rand_dir_name/$profile_pic_name','no','$url','$tag1','$tag2','$tag3','$content')";
$profile_pic_query1 = $db_conx->query($profile_pic_query);
echo "Your photo was uploaded!";
}
else
{
echo "Link has already been taken.";
}
}
}
}
?>
问题仍然存在。为什么我的ajax不会在我的结果范围内打印回声。如果你能帮助我,我将不胜感激。