在 php 中使用弹出表单上传图片无效
Uploading image using a popup form in php not working
我有一个弹出表单,我正在通过 AJAX 和 PHP 提交。问题是表单提交成功但脚本既没有上传图片也没有插入图片名称到 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";
}
.serialize()
方法以标准 URL 编码符号创建文本字符串。它可以作用于具有 selected 单独表单控件的 jQuery 对象。来自文件 select 元素的数据未序列化。像这样的东西可能帮助你..
你可以利用 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;
});
});
仅供参考
ProcessData 设置为 false 以防止 jQuery 自动将数据转换为查询字符串
我有一个弹出表单,我正在通过 AJAX 和 PHP 提交。问题是表单提交成功但脚本既没有上传图片也没有插入图片名称到 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";
}
.serialize()
方法以标准 URL 编码符号创建文本字符串。它可以作用于具有 selected 单独表单控件的 jQuery 对象。来自文件 select 元素的数据未序列化。像这样的东西可能帮助你..
你可以利用 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;
});
});
仅供参考
ProcessData 设置为 false 以防止 jQuery 自动将数据转换为查询字符串