注册表单不起作用(PHP / MySQL)

时间:2013-12-28 10:22:36

标签: php mysql ajax forms

我有一个表单,我需要验证然后更新我的数据库。我想我错过了什么。 home.php中有一个表单(在公共目录中),有一个名为signup.php(../signup.php)的文件,还有一个“注册”按钮。问题是单击它时没有调用signup()函数。我提供所有相关代码。如果有人帮助我,我会很高兴:)

由于

这是表格

<div class="body">
<form name = "signupform" class="sky-form" onsubmit="return false;">
<header>Sign-up as an instructor <br/>
    <span style="font-size:14px;">Already Registered ?<a class="loginButton"      style="text-decoration:none; color:#0CF; cursor:pointer;">Login</a> </span>
</header>               
    <fieldset>                  
        <section>

        <label class="input" style="padding:20px;">
                <input type="text" placeholder="First Name" name="firstname" required>
                <b class="tooltip tooltip-top-right">Enter your First Name</b>
            </label>

            <label class="input" style="padding:20px;">
                <input type="text" placeholder="Last Name" name="lastname" required>
                <b class="tooltip tooltip-top-right">Enter your Last Name</b>
            </label>


            <label class="input" style="padding:20px;">
                <input type="text" placeholder="Username" name="username" onblur="checkusername()" onkeyup="restrict('username')" maxlength="10" required>
                <b class="tooltip tooltip-top-right">Enter your username</b>
            </label>
            <span id = "unamestatus"></span>

            <label class="input" style="padding:20px;">
                <input type="password" placeholder="Password" name="pass1" onfocus="emptyElement('status')" required>
                <b class="tooltip tooltip-top-right">Enter your password</b>
            </label>

            <label class="input" style="padding:20px;">
                <input type="password" placeholder="Confirm Password" name="pass2" onfocus="emptyElement('status')" required>
                <b class="tooltip tooltip-top-right">Confirm your password</b>
            </label>

            <label class="input" style="padding:20px;">
                <input type="text" placeholder="email" name="email" onblur="checkemail()" onkeyup="restrict('email')" maxlength="100" onfocus="emptyElement('status')" required>
                <b class="tooltip tooltip-top-right">Enter your email address</b>
            </label>

            <label class="input" style="padding:20px;">
                <input type="text" placeholder="Address" name="address" onfocus="emptyElement('status')" required>
                <b class="tooltip tooltip-top-right">Where can we contact you?</b>
            </label>

        </section>
   </fieldset>     
   <footer>
    <button id = "signup_button" name = "signup_button" class="button" onclick="signup()"    >Create my account</button>
    <button type="button" class="button button-secondary back1" Back</button>

    <span id = "status"></span>
</footer>
</form>
 </div>
 </div>

这是signup.php文件

<?php
session_start();
// If user is logged in, header them away
if(isset($_SESSION["username"])){
header("location: message.php?msg=You are already logged in");
exit();
}
?>

<?php
// Ajax calls this NAME CHECK code to execute
if(isset($_POST["usernamecheck"])){
include_once("connect.inc.php");
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']);
$sql = "SELECT id FROM instructors WHERE user_name ='$username' LIMIT 1";
$query = mysqli_query($connect, $sql); 
$uname_check = mysqli_num_rows($query);
//if (strlen($username) < 3 || strlen($username) > 16) {
//   echo '<strong style="color:#F00;">3 - 16 characters please</strong>';
//    exit();
//}
if (is_numeric($username[0])) {
    echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>';
    exit();
}
if ($uname_check < 1) {
    echo '<strong style="color:#009900;">' . $username . ' is OK</strong>';
    exit();
} else {
    echo '<strong style="color:#F00;">' . $username . ' is taken</strong>';
    exit();
}
}
?>


<?php
// Ajax calls this REGISTRATION code to execute
if(isset($_POST["u"])){
// CONNECT TO THE DATABASE
include_once("connect.inc.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$u = preg_replace('#[^a-z0-9]#i', '', $_POST['u']);
$e = mysqli_real_escape_string($db_conx, $_POST['e']);
$p = $_POST['p'];
//$g = preg_replace('#[^a-z]#', '', $_POST['g']);
//$c = preg_replace('#[^a-z ]#i', '', $_POST['c']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
$sql = "SELECT id FROM users WHERE username='$u' LIMIT 1";
$query = mysqli_query($db_conx, $sql); 
$u_check = mysqli_num_rows($query);
// -------------------------------------------
$sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
$query = mysqli_query($db_conx, $sql); 
$e_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if($u == "" || $e == "" || $p == ""){
    echo "The form submission is missing values.";
    exit();
} else if ($u_check > 0){ 
    echo "The username you entered is alreay taken";
    exit();
} else if ($e_check > 0){ 
    echo "That email address is already in use in the system";
    exit();
} else if (strlen($u) < 3 || strlen($u) > 16) {
    echo "Username must be between 3 and 16 characters";
    exit(); 
} else if (is_numeric($u[0])) {
    echo 'Username cannot begin with a number';
    exit();
} else {
// END FORM DATA ERROR HANDLING
    // Begin Insertion of data into the database
    // Hash the password and apply your own mysterious unique salt
    $cryptpass = crypt($p);
    include_once ("randStrGen.php");
    $p_hash = randStrGen(20)."$cryptpass".randStrGen(20);
    // Add user info into the database table for the main site table
    $sql = "INSERT INTO instructors (username, email, password, ip, signup, lastlogin, notifications_checked)       
            VALUES('$u','$e','$p_hash','$ip',now(),now(),now())";
    $query = mysqli_query($connect, $sql); 
    $uid = mysqli_insert_id($connect);
    // Establish their row in the useroptions table
    //$sql = "INSERT INTO useroptions (id, username, background) VALUES ('$uid','$u','original')";
    //$query = mysqli_query($connect, $sql);
    // Create directory(folder) to hold each user's files(pics, MP3s, etc.)
    if (!file_exists("../instructors/$u")) {
        mkdir("instructors/$u", 0755);
    }
    // Email the user their activation link
    $to = "$e";                          
    $from = "auto_responder@skiandridelessons.com";
    $subject = 'skiandridelessons Account Activation';
    $message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>skiandridelessons Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.skiandridelessons.com"><img src="http://www.skiandridelessons.com/images/logo.png" width="36" height="30" alt="skiandridelessons" style="border:none; float:left;"></a>skiandridelessons Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$u.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://www.skiandridelessons.com/activation.php?id='.$uid.'&u='.$u.'&e='.$e.'&p='.$p_hash.'">Click here to activate your account now</a><br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    mail($to, $subject, $message, $headers);
    echo "signup_success";
    exit();
}
exit();
}
?>

告诉我是否遗漏了什么:)

4 个答案:

答案 0 :(得分:0)

像这样更新您的表单标签..

<form name = "signupform" class="sky-form"  action="signup.php">

答案 1 :(得分:0)

添加动作=“singup.php”method =“POST”

<form name = "signupform" class="sky-form" onsubmit="return false;" method="POST" action="signup.php">

答案 2 :(得分:0)

<form name = "signupform" class="sky-form"  action="signup.php">

<!--input elements-->

<input type ="submit" id = "signup_button" name ="signup_button" class="button" value="Create my account"/>

</form>

答案 3 :(得分:0)

我注意到这段代码来自Adam Khoury的PHP社交网络教程。你以糟糕的方式篡改了代码。

问题#1:

您的提交按钮正在尝试调用Ajax,但它在表单页面上不存在。 (来自您提交的代码)

问题#2:

如果不知何故神奇地Ajax处理了这个请求,它将无法返回任何信息,因为它不在同一页面上。 MySQL查询和变量也不是。

问题#3:

同样,如果Ajax处理了信息,则表单字段的名称不正确。 PHP正在寻找$ _POST [&#39; u&#39;]和用户名字段是$ _POST [&#39;用户名&#39;]。

问题#4:

如果Ajax与表单位于同一页面上,那么您仍然会遇到很多问题。例如,我知道Adam调用字段的ID,而不是名称,所以Ajax不会处理任何事情。

问题#5:

从我所看到的你在名称,ID和其他值等中使用空格。从不使用空格

列表继续!

所以在这一点上你可能会想:&#34;我该怎么做才能解决这个问题?&#34;

不要使用Ajax!它没有添加任何安全性。

以下是我非常友好地推荐您使用的代码。 工作。

如果表单,如果您需要特定的外观可能需要工作,我不会为您执行此操作

<div class="body">
<form name="signupform" class="sky-form" method="POST" action="signup.php" >
<header>Sign-up as an instructor <br/>
<span style="font-size:14px;">Already Registered ?<a class="loginButton"      style="text-decoration:none; color:#0CF; cursor:pointer;">Login</a> </span>
</header>               
<fieldset>                  
    <section>

    <label class="input" style="padding:20px;">
            <input type="text" placeholder="First Name" name="firstname" required>
            <b class="tooltip tooltip-top-right">Enter your First Name</b>
        </label>

        <label class="input" style="padding:20px;">
            <input type="text" placeholder="Last Name" name="lastname" required>
            <b class="tooltip tooltip-top-right">Enter your Last Name</b>
        </label>


        <label class="input" style="padding:20px;">
            <input type="text" placeholder="Username" name="username" maxlength="10" required>
            <b class="tooltip tooltip-top-right">Enter your username</b>
        </label>

        <label class="input" style="padding:20px;">
            <input type="password" placeholder="Password" name="pass1" required>
            <b class="tooltip tooltip-top-right">Enter your password</b>
        </label>

        <label class="input" style="padding:20px;">
            <input type="password" placeholder="Confirm Password" name="pass2" required>
            <b class="tooltip tooltip-top-right">Confirm your password</b>
        </label>

        <label class="input" style="padding:20px;">
            <input type="text" placeholder="email" name="email" maxlength="100" required>
            <b class="tooltip tooltip-top-right">Enter your email address</b>
        </label>

        <label class="input" style="padding:20px;">
            <input type="text" placeholder="Address" name="address" required>
            <b class="tooltip tooltip-top-right">Where can we contact you?</b>
        </label>

    </section>
    </fieldset>     
    <footer>
<button id ="signup_button" name ="signup_button" class="button">Create my account</button>
<button type="button" class="button button-secondary back1" Back</button>
</footer>
</form>
</div>
</div>

对于PHP代码,您将需要:

<?php
session_start();
// If user is logged in, header them away
if(isset($_SESSION["username"])){
header("location: message.php?msg=You are already logged in");
exit();
}
?>

<?php
// Ajax calls this NAME CHECK code to execute
if(isset($_POST["usernamecheck"])){
include_once("connect.inc.php");
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']);
$sql = "SELECT id FROM instructors WHERE user_name='$username'";
$query = mysqli_query($connect, $sql); 
$uname_check = mysqli_num_rows($query);

if (is_numeric($username[0])) {
  echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>';
  exit();
}
if ($uname_check < 1) {
  //header the user somewhere...
  exit();
} else {
  //header the user somewhere
  exit();
}
}
?>


<?php
if(isset($_POST["username"])){
// CONNECT TO THE DATABASE
include_once("connect.inc.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$u = preg_replace('#[^a-z0-9]#i', '', $_POST['username']);
$e = mysqli_real_escape_string($db_conx, $_POST['email']);
$p = $_POST['pass1'];
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
$sql = "SELECT id FROM instructors WHERE username='$u' LIMIT 1";
$query = mysqli_query($db_conx, $sql); 
$u_check = mysqli_num_rows($query);
// -------------------------------------------
$sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
$query = mysqli_query($db_conx, $sql); 
$e_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if($u == "" || $e == "" || $p == ""){
  echo "The form submission is missing values.";
  exit();
} else if ($u_check > 0){ 
  echo "The username you entered is alreay taken";
  exit();
} else if ($e_check > 0){ 
  echo "That email address is already in use in the system";
  exit();
} else if (strlen($u) < 3 || strlen($u) > 16) {
  echo "Username must be between 3 and 16 characters";
  exit(); 
} else if (is_numeric($u[0])) {
  echo 'Username cannot begin with a number';
  exit();
} else {
// END FORM DATA ERROR HANDLING
// Begin Insertion of data into the database
// Hash the password and apply your own mysterious unique salt
$cryptpass = crypt($p); **crypt is okay. I recommend Phpass**
include_once ("randStrGen.php");
$p_hash = randStrGen(20)."$cryptpass".randStrGen(20);
// Add user info into the database table for the main site table
$sql = "INSERT INTO instructors (username, email, password, ip, signup, lastlogin, notifications_checked)       
        VALUES('$u','$e','$p_hash','$ip',now(),now(),now())";
$query = mysqli_query($connect, $sql); 
$uid = mysqli_insert_id($connect);
// Establish their row in the useroptions table
//$sql = "INSERT INTO useroptions (id, username, background) VALUES ('$uid','$u','original')";
//$query = mysqli_query($connect, $sql);
// Create directory(folder) to hold each user's files(pics, MP3s, etc.)
if (!file_exists("../instructors/$u")) {
    mkdir("instructors/$u", 0755);
}
// Email the user their activation link
$to = "$e";                          
$from = "auto_responder@skiandridelessons.com";
$subject = 'skiandridelessons Account Activation';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>skiandridelessons Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.skiandridelessons.com"><img src="http://www.skiandridelessons.com/images/logo.png" width="36" height="30" alt="skiandridelessons" style="border:none; float:left;"></a>skiandridelessons Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$u.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://www.skiandridelessons.com/activation.php?id='.$uid.'&u='.$u.'&e='.$e.'&p='.$p_hash.'">Click here to activate your account now</a><br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
mail($to, $subject, $message, $headers);
echo "signup_success";
exit();
}
exit();
}
?>

这可能不是完美的使用,但正如我所说,我没有为你做所有这些代码。如果您希望我为您构建代码,那么它将花费。正如我评论的那样,crypt函数可以正常使用,但我建议使用Phpass。它的安全性非常可靠,并且使用起来并不困难。

还有很多问题......在W3学校的PHP表格提交等方面接受基础教程。