我有一个弹出窗体,我通过AJAX和PHP提交。问题是表单提交成功但脚本既没有上传pic也没有将pic名称插入mysql。
如果我不使用带有相同脚本的弹出窗体而不是它正常工作。
任何帮助??
Html表格:
<form action="" method="post" class="signupform">
<label>Full Name</label>
<input type="text" Placeholder="Name" name="name" pattern="[A-Z a-z]{3,25}" title="Name should contain 3 to 25 characters" Required="required"/>
<br />
<label>Email Address</label>
<input type="email" Placeholder="Email-id" name="email" Required="required"/>
<br />
<label>Password</label>
<input type="password" Placeholder="Password" name="pass" pattern="[A-Za-z0-9@]{6,15}" title="Password should be alphanumeric. Only A-Z,a-z,0-9 and @ allowed and it must be 6 to 15 digits long." Required="required"/>
<br />
<label>Sex</label>
<span>Male<input type="radio" name="sex" checked="checked" value="M"/> Female<input type="radio" name="sex" value="F"/></span>
<br />
<label>City</label>
<input type="text" Placeholder="City" name="city" Required="required"/>
<br />
<label>Profile pic</label>
<input type="file" Placeholder="Profile pic" name="dp"/>
<br />
<div class="checkbox">
<input id="send_updates" type="checkbox" Required="required"/>
<label for="send_updates">I accept the <a href="termsofuse.php">terms and conditions</a></label>
</div>
<div class="action_btns">
<div class="one_half"><a href="#" class="btn back_btn"><i class="fa fa-angle-double-left"></i> Back</a></div>
<div class="xyx xyxy"><input type="submit" value="Register" name="submitp" class="signsub"/></div>
</div>
</form>
的Ajax:
$(document).ready(function()
{
$('.signsub').click(function()
{
$.ajax({
type: "POST",
url: "ajaxsignup.php",
data: $('.signupform').serialize(),
cache: false,
success: function(data) {
if (data)
{
$('.user_register').hide();
$(".error").html(" Thank you for joining us you are successfully logged in !!").show().delay(30000).fadeOut('slow');
window.location.reload().delay(30000);
}
else
{
$(".signsub").val('Register')
$(".error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
}
}
});
return false;
});
});
ajaxsignup.php
<?php
session_start();
include('includes/db.php');
$name=$_POST['name'];
$pass=$_POST['pass'];
$email=$_POST['email'];
$sex=$_POST['sex'];
$city=$_POST['city'];
$dp=$_FILES['dp']['name'];
include('includes/uploadfiledp.php');
$queryb="INSERT INTO login VALUES('','$name','$pass','$email','$sex','$city','$chckfil')";
$resultb=mysql_query($queryb);
if($resultb)
{
$_SESSION['user']=$email;
echo ok;
}
?>
uploadfiledp.php
$allowedExts = array("jpeg", "jpg");
$extension = end(explode(".", $_FILES["dp"]["name"]));
if (in_array($extension, $allowedExts))
{
if ($_FILES["dp"]["error"] > 0)
{
echo "Return Code: " . $_FILES["dp"]["error"] . "<br>";
}
else
{
if (file_exists("images/" . $_FILES["dp"]["name"]))
{
$b=explode(".", $_FILES["dp"]["name"]);
$first=$b[0];
$ext=$b[1];
$i=1;
do
{
$fname1=$first;
$fname1=$fname1.$i;
$i++;
$chckfil=$fname1.".".$ext;
}
while(file_exists("images/" . $chckfil));
move_uploaded_file($_FILES["dp"]["tmp_name"],
"images/" . $chckfil);
}
else
{
move_uploaded_file($_FILES["dp"]["tmp_name"],
"images/" . $_FILES["dp"]["name"]);
$chckfil=$_FILES["dp"]["name"];
}
}
}
else
{
echo "Invalid file";
}
答案 0 :(得分:0)
.serialize()
方法使用标准的URL编码表示法创建文本字符串。它可以作用于选择了单独表单控件的jQuery对象。来自文件选择元素的数据未被序列化。这样的东西可能有助于你...
您可以使用Formdata()
,
$(document).ready(function()
{
$("#formID").submit(function(){
{
var formData = new FormData($(this)[0]);
$.ajax({
type: "POST",
url: "ajaxsignup.php",
data: formData,
contentType: false,
processData: false,
cache: false,
success: function(data) {
if (data)
{
$('.user_register').hide();
$(".error").html(" Thank you for joining us you are successfully logged in !!").show().delay(30000).fadeOut('slow');
window.location.reload().delay(30000);
}
else
{
$(".signsub").val('Register')
$(".error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
}
}
});
return false;
});
});
<强> FYI 强>
ProcessData 设置为false,以防止jQuery自动将数据转换为查询字符串