【发布时间】:2014-01-13 04:42:44
【问题描述】:
时事通讯:
<form id="form-search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<span><span class="style2">Enter you email here</span>:</span>
<input name="email" type="email" id="email" required/>
<input type="submit" value="subscribe" class="submit" onclick="return fun()" />
</form>
<?php
mysql_connect("localhost","","");
mysql_select_db("");
error_reporting(E_ALL && ~E_NOTICE);
$email=$_POST['email'];
$sql="INSERT INTO newsletter_email(email) VALUES ('$email')";
$result=mysql_query($sql);
if($result){
echo "You have been successfully subscribed.";
}
if(!$sql)
die(mysql_error());
mysql_close();
?>
但我想通过 Ajax 将我的电子邮件插入数据库。我不希望我的页面被重定向,因为每次刷新页面时,都会将空值插入到数据库中..
我只想通过 Ajax 将我的电子邮件插入数据库,然后是邮箱 即
<input name="email" type="email" id="email" required/>
<input type="submit" value="subscribe" class="submit" onclick="return fun()" />
应该消失,应该有“你已成功订阅”这行..
任何简短的代码都会非常有用..提前谢谢你:)
【问题讨论】:
-
你有没有尝试过。在你的代码中添加 ajax
-
使用
PDO或mysqli函数,因为mysql_*已被弃用 -
sarmishtha : 你确定你需要 ajax 吗?我可以看到不需要 ajax。
-
@MazIqbal 是的,但我在 ajax 中不是很方便,所以无法通过它
-
@Mrcoder 但我的意思是 php 中的任何其他方法都会使我的页面重定向 na .. 我不想要