Javascript PHP和Mysql中的电子邮件订阅不起作用
index.phpJavascript PHP和Mysql中的电子邮件订阅不起作用,javascript,phpmyadmin,Javascript,Phpmyadmin,index.php <?php // set error reporting level if (version_compare(phpversion(), '5.3.0', '>=') == 1) error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); else error_reporting(E_ALL &
<?php
// set error reporting level
if (version_compare(phpversion(), '5.3.0', '>=') == 1)
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
else
error_reporting(E_ALL & ~E_NOTICE);
session_start();
if (isset($_POST['submit']))
{
if (strlen($sEmail) >= 1 and strlen($sEmail) <= 55) {
if (ereg('^[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $sEmail)) {
// Here you can add him to database
$con=mysqli_connect("localhost","root","mypuppy29","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$EMAIL = mysqli_real_escape_string($con, $_POST['email']);
$sql="INSERT INTO subscribers VALUES (DEFAULT, '$EMAIL')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
}
}
else {
$sErrors = 'Email is wrong';
}
}
// display step 2
$aParams = array(
'{email}' => $sEmail,
);
<代码>您应该真正考虑格式化此代码。此外,如果你发布此代码的相关部分,你更有可能得到答案…+1对于格式,它不会更正你的代码:但请更改一些内容:type=电子邮件而不是文本。看看这里的js电子邮件验证:。是的,我已经做了,并重新格式化了代码,但按钮仍在工作,但没有将记录存储在数据库中。错误是:注意:未定义变量:第12行C:\wamp\www\Landingpage\manage.php中的电子邮件
<!DOCTYPE html>
<html lang="en" >
<head>
<title>Form Validation with Javascript and PHP</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="main.css" type="text/css">
<link rel="stylesheet" href="main.css" type="text/css"/>
<script src="js/script.js"></script>
</head>
<body>
<header>
<h2>Form Validation with Javascript and PHP</h2>
</header>
<div class="container">
<form action="index.php" method="post" id="form" onsubmit="return
<table cellspacing="10">
<tr><td>Email</td><td><input type="text" name="email" maxlength="50"
validate_all('results');">
id="email" ><br /><div id="email_length"></div></td></tr>
<tr><td> colspan="2"><input type="submit" name="submit" value="Register">
</tr></td></table>
<h3 id="results"></h3>
</form>
</div>
</body>
</html>
function check_v_mail(field) {
fld_value = document.getElementById(field).value;
is_m_valid = 0;
if (fld_value.indexOf('@') >= 1) {
m_valid_dom = fld_value.substr(fld_value.indexOf('@')+1);
if (m_valid_dom.indexOf('@') == -1) {
if (m_valid_dom.indexOf('.') >= 1) {
m_valid_dom_e = m_valid_dom.substr(m_valid_dom.indexOf('.')+1);
if (m_valid_dom_e.length >= 1) {
is_m_valid = 1;
}
}
}
}
if (is_m_valid) {
update_css_class(field, 2);
m_valid_r = 1;
} else {
update_css_class(field, 1);
m_valid_r = 0;
}
return m_valid_r;
}
function validate_all(output) {
t4 = check_v_mail('email');
errorlist = '';
}
if (! t4) {
errorlist += 'Mail is wrong<br />';
}
<?php
include("include.php");
//Creating a form block
if(!$_POST)
{
$display_block = "
<form method = \"POST\" action =\"".$_SERVER["PHP_SELF"]."\">
Email: <input type = \"text\" name = \"email\">
Submit: <input type = \"submit\" name = \"submit\">
</form>";
} else if(($_POST)&&($_POST["action"]=="sub"))
{
//trying to subscribe;validate email address
if($_POST["email"]== "")
{
header("Location:manage.php");
exit;
}
else
{
//connect to database
doDB();
//check the email is in list
emailChecker($_POST("email"));
//get number of results and do the action
if (mysqli_num_rows($check_res)<1)
{
//free results
mysqli_free_result($check_res);
//add record
$add_sql = "INSERT INTO subscribers(email)
VALUES('".$_POST["email"]."')";
$add_res = mysqli_query($mysqli,$addsql)
or die (mysqli_error($mysqli));
$display_block = "<p>Thanks for signing up!</p>";
//close connection to mysql
mysqli_close($mysqli);
}
else{
//print faliure message
$display_block = "<p>You are already subscribed!</p>";
}
}
}
?>
<html>
<head></head>
<body>
<?php echo "$display_block";?>
</body>
</html>