我无法正确创建注册表单

时间:2015-12-18 22:54:04

标签: javascript php mysql ajax database

我有以下包含符号提升形式的框:

 <!-- sign up form -->
            <div id="cd-signup">
                <form class="cd-form" action = "signup.php" > <?php echo "$error" ?>
                    <p class="fieldset">
                        <label class="image-replace cd-username" for="signup-username">Username</label>
                        <input class="full-width has-padding has-border" id="signup-username" type="text" placeholder="Username" name = "user" <?php echo "value='$user'"?>>

                    </p>

                    <p class="fieldset">
                        <label class="image-replace cd-password" for="signup-password">Password</label>
                        <input name = "pass" class="full-width has-padding has-border" <?php echo "value='$pass'" ?> id="signup-password" type="text"  placeholder="Password">

                        <!-- <span class="cd-error-message">Password must be at least 6 characters long</span> -->
                    </p>


                    <p class="fieldset">
                        <input class="full-width has-padding" type="submit" value="Create account">
                    </p>
                </form> 
<!-- more text here -->
<span class="section-nav">
            <ul>
                <li><a id="signup" class="cd-signup" href="#0">Get Started</a></li>
                <li><a id="learnmore" class="cd-learnmore" href="#section2">Learn More</a></li>
            </ul>
        </span>

<span class="section-nav">
            <ul>
                <li><a id="signup" class="cd-signup" href="#0">Get Started</a></li>
            </ul>

这些是作为按钮形状实现的。

我在我的html文件(index.php)的头部包含以下php代码:

<?php
    require_once 'functions.php';
    require_once 'signup.php';

    $userstr = '';
    if (isset($_SESSION['user'])) {
        $user     = $_SESSION['user'];
        $loggedin = TRUE;
        $userstr  = " ($user)";
    }
    else $loggedin = FALSE;
       if ($loggedin) {
           header(home.php);
       }
       else {
    ?>

signup.php:

session_start();
<?php

 $error = $user = $pass = "";
  if (isset($_SESSION['user'])) destroySession();

  if (isset($_POST['user']))
  {
    $user = sanitizeString($_POST['user']);
    $pass = sanitizeString($_POST['pass']);

    if ($user == "" || $pass == "")
      $error = "Not all fields were entered<br><br>";
    else
    {
      $result = queryMysql("SELECT * FROM members WHERE user='$user'");

      if ($result->num_rows)
        $error = "That username already exists<br><br>";
      else
      {
        queryMysql("INSERT INTO members VALUES('$user', '$pass')");
        die("<h4>Account created</h4>Please Log in.<br><br>");
      }
    }
  }
  ?>

的functions.php:

<?php


      $dbhost  = 'localhost';    // Unlikely to require changing
      $dbname  = 'socialmedia';   // Modify these...
      $dbuser  = 'root';   // ...variables according
      $dbpass  = 'mysql';   // ...to your installation
      $appname = "Social Media"; // ...and preference

      $connection = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
      if ($connection->connect_error) die($connection->connect_error);

      function queryMysql($query)
      {
        global $connection;
        $result = $connection->query($query);
        if (!$result) die($connection->error);
        return $result;
      }

      function destroySession()
      {
        $_SESSION=array();

        if (session_id() != "" || isset($_COOKIE[session_name()]))
          setcookie(session_name(), '', time()-2592000, '/');

        session_destroy();
      }

      function sanitizeString($var)
      {
        global $connection;
        $var = strip_tags($var);
        $var = htmlentities($var);
        $var = stripslashes($var);
        return $connection->real_escape_string($var);
      }

      function showProfile($user)
      {
        if (file_exists("$user.jpg"))
          echo "<img src='$user.jpg' style='float:left;'>";

        $result = queryMysql("SELECT * FROM profiles WHERE user='$user'");

        if ($result->num_rows)
        {
          $row = $result->fetch_array(MYSQLI_ASSOC);
          echo stripslashes($row['text']) . "<br style='clear:left;'><br>";
        }
      }
    ?>

代码在我得到它的地方工作得很好(来源:学习Php,MySql和Javascript),所以我决定将它应用到我自己的网站。

但是,当我点击创建帐户按钮时,没有任何反应。正确设置数据库以及相应的表(在原始代码上测试)以及正确的Ajax请求。

我认为问题出在index.php的某个地方,也许我错过了什么? 谢谢您的帮助 ! :)

编辑:

这里是按钮实现的javascript

jQuery(document).ready(function ($) {
    var formModal = $('.cd-user-modal'),
        formSignup = formModal.find('#cd-signup'),
        tabSignup = formModalTab.children('li').eq(1).children('a'),
    ,
        backToLoginLink = formForgotPassword.find('.cd-form-bottom-message a'),
        mainNav = $('.main-nav'),
        sectionNav = $(".section-nav");

    //open modal
    mainNav.on('click', function (event) {
        $(event.target).is(mainNav) && mainNav.children('ul').toggleClass('is-visible');
    });

    //open sign-up form
    sectionNav.on('click', '.cd-signup', signup_selected);
    //open login-form form
    mainNav.on('click', '.cd-signin', login_selected);

    //close modal
    formModal.on('click', function (event) {
        if ($(event.target).is(formModal) || $(event.target).is('.cd-close-form')) {
            formModal.removeClass('is-visible');
        }
    });
    //close modal when clicking the esc keyboard button
    $(document).keyup(function (event) {
        if (event.which == '27') {
            formModal.removeClass('is-visible');
        }
    });

    //switch from a tab to another
    formModalTab.on('click', function (event) {
        event.preventDefault();
        ($(event.target).is(tabLogin)) ? login_selected() : signup_selected();
    });

function signup_selected() {
        mainNav.children('ul').removeClass('is-visible');
        formModal.addClass('is-visible');
        formLogin.removeClass('is-selected');
        formSignup.addClass('is-selected');
        formForgotPassword.removeClass('is-selected');
        tabLogin.removeClass('selected');
        tabSignup.addClass('selected');
    }

1 个答案:

答案 0 :(得分:-2)

看看这一行:

<p class="fieldset">
     <input class="full-width has-padding" type="submit" value="Create account">
 </p>

您的提交按钮的名称应为user

<p class="fieldset">
     <input class="full-width has-padding" name="user" type="submit" value="Create account">
 </p>