为什么不向数据库提交这个值?

时间:2022-01-15 16:10:00

JS: The JS for client side validation of email field

JS:用于客户端验证电子邮件字段的JS

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"> </script>
<script type="text/javascript" src="src/js/validate.js"> </script> 
   <script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function() { alert("submitted!"); }
    });

    $().ready(function() {
        // validate alphaRegister form on keyup and submit
        $("#alphaRegister").validate({
            rules: {
                email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                email: "*"
            }
        });
    });
    </script>

HTML The form

HTML表单

    <form id="alphaRegister" action="src/php/newSubscriber.php" method="post">
        <input type="email" name="email" id="cemail" value="" class="required" />
        <div style="float:right; margin:0 5px 2px 0;"><input type="submit" id="submit" name="submit" value="" /></div>
    </form>

When I enter a validname@adomain.com, it just says submitted!!

当我输入validname@adomain.com时,它只是说提交!!

PHP File The file handling the input of the html

PHP文件处理html输入的文件

 <?php
$host="localhost"; // Host name 
$username="myuser"; // Mysql username       
    $username2="root"; #localhost
$password="mypass"; // Mysql password  
    $password2=""; #localhost
$db_name="mydbname"; // Database name 
$tbl_name="mydbtblname"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username2", "$password2")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot connect to database");

/* Obliterate bad input */
$goodEmail = mysql_real_escape_string($_POST['email']);

$sql= "INSERT INTO alphaSubscribers (alphaSEmAddr) VALUES('$goodEmail')";

$result = mysql_query($sql);
if (!$result) {
    die('Invalid Email, please retry.');
}
else {
    header( 'Location: /index.php' ) ;
    echo "<meta HTTP-EQUIV='REFRESH' content='0; url=/index.php'>";
}

?>

Anyone see what Im doing wrong?

谁知道我做错了什么?

3 个解决方案

#1


3  

You are sending the form via GET but test for the POST parameter. Change your form tag to

您是通过GET发送表单但测试POST参数。将表单标记更改为

<form id="alphaRegister" action="src/php/newSubscriber.php" method="post">

Also remove the javascript submitHandler, otherwise the form might not get submitted at all:

同时删除javascript submitHandler,否则表单可能根本无法提交:

$.validator.setDefaults({
    submitHandler: function() { alert("submitted!"); } // <= remove that line
});

#2


0  

Your form is using the GET method, while your back-end PHP code is handling it as $_POST

您的表单使用GET方法,而您的后端PHP代码将其作为$ _POST处理

Your HTML method should be method="POST">

你的HTML方法应该是method =“POST”>

#3


0  

From jquery docs: Use submitHandler to implement your own form submit, eg. via Ajax.

来自jquery docs:使用submitHandler实现自己的表单提交,例如。通过Ajax。

Your submit handler validator prevents that for was submited in a regular way.

您的提交处理程序验证程序会阻止以常规方式提交。

Remove this code and try again:

删除此代码,然后重试:

$.validator.setDefaults({
    submitHandler: function() { alert("submitted!"); }
});

#1


3  

You are sending the form via GET but test for the POST parameter. Change your form tag to

您是通过GET发送表单但测试POST参数。将表单标记更改为

<form id="alphaRegister" action="src/php/newSubscriber.php" method="post">

Also remove the javascript submitHandler, otherwise the form might not get submitted at all:

同时删除javascript submitHandler,否则表单可能根本无法提交:

$.validator.setDefaults({
    submitHandler: function() { alert("submitted!"); } // <= remove that line
});

#2


0  

Your form is using the GET method, while your back-end PHP code is handling it as $_POST

您的表单使用GET方法,而您的后端PHP代码将其作为$ _POST处理

Your HTML method should be method="POST">

你的HTML方法应该是method =“POST”>

#3


0  

From jquery docs: Use submitHandler to implement your own form submit, eg. via Ajax.

来自jquery docs:使用submitHandler实现自己的表单提交,例如。通过Ajax。

Your submit handler validator prevents that for was submited in a regular way.

您的提交处理程序验证程序会阻止以常规方式提交。

Remove this code and try again:

删除此代码,然后重试:

$.validator.setDefaults({
    submitHandler: function() { alert("submitted!"); }
});