尝试这个:
$(document).on('click','#save',function(e) { var data = $("#form-search").serialize(); $.ajax({ data: data, type: "post", url: "insertmail.php", success: function(data){ alert("Data Save: " + data); }}); });和在
insertmail.php:
<?php if(isset($_REQUEST)){ mysql_connect("localhost","root","");mysql_select_db("eciticket_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.";}}?>不要使用
mysql_已弃用的。
another method:
实际上,如果您的问题是将空值插入数据库,则尝试此操作,这里不需要ajax。
<?phpif($_POST['email']!=""){ mysql_connect("localhost","root",""); mysql_select_db("eciticket_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."; setcookie("msg","You have been successfully subscribed.",time()+5,"/"); header("location:yourphppage.php"); } if(!$sql) die(mysql_error()); mysql_close();}?> <?php if(isset($_cookie['msg'])){?> <span><?php echo $_cookie['msg'];setcookie("msg","",time()-5,"/");?></span> <?php }?><form id="form-search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <span><span >Enter you email here</span>:</span> <input name="email" type="email" id="email" required/> <input type="submit" value="subscribe" /></form>


