单击“提交”按钮后不执行PHP代码

时间:2018-10-08 23:17:26

标签: javascript php jquery mysql sql

我已经坚持了大约一天。因此,我有一个“创建帐户”页面,该页面只接受用户输入的表单并将其输入到MySQL数据库中。这里有很多代码,但简单地说来就是问题:当我单击“提交”按钮时,它什么也不做。我已经将server.php中的PHP代码指向createaccount.php中的“ Submit”按钮,但是它实际上根本不起作用,我不知道为什么。

我也有一个登录页面,但问题完全相同,但是一旦我弄清楚该如何处理,就可以自行修复,因为这基本上是相同的问题,我只是在运行一个不同的SQL查询

我还意识到我丢失了一些从SQL“ INSERT INTO”查询中的表单中捕获的变量值。我排除了那些可能引起复杂性的原因(日期格式不正确,图像没有文件存储位置等),这是解决此问题的唯一目的,因为我知道SQL查询可与简单的字符串变量一起使用。简而言之,我正在尝试缩小问题范围,并想排除SQL查询作为问题。

在此先感谢您提供的帮助。

这里是createaccount.php

<?php include('server.php') ?>

<html lang="en">
<head>
  <link rel="stylesheet" type="text/css" href="createaccountstyle.css">
</head>

<!-- multistep form -->
<form method="post" action="createaccount.php" id="msform">
  <?php include('errors.php'); ?>
  <!-- progressbar -->
  <ul id="progressbar">
    <li class="active">Account Setup</li>
    <li>Social Profiles</li>
    <li>Personal Details</li>
  </ul>
  <!-- fieldsets -->
  <fieldset>
    <h2 class="fs-title">Create your account</h2>
    <input type="text" name="username" placeholder="Username" value="<?php echo $username; ?>" />
    <input type="password" name="pass" placeholder="Password" />
    <input type="password" name="cpass" placeholder="Confirm Password" />
    <button type="button" name="next" class="next action-button">Next</button>
  </fieldset>
  <fieldset>
    <h2 class="fs-title">Personal Details</h2>
    <input type="text" name="firstName" placeholder="First Name" />
    <input type="text" name="middleName" placeholder="Middle Name or Initial" />
    <input type="text" name="lastName" placeholder="Last Name" />
    <p>Date of Birth</p>
    <input type="date" name="dateOfBirth" />
    <button type="button" name="previous" class="previous action-button">Previous</button>
    <button type="button" name="next" class="next action-button">Next</button>
  </fieldset>
  <fieldset>
    <h2 class="fs-title">Add a Profile Picture!</h2>
    <h3 class="fs-subtitle">Put the Cherry on Top of your Profile</h3>
    <input type="file" name="image">
    <button type="button" name="previous" class="previous action-button">Previous</button>
    <button type="submit" name="register" class="submit action-button">Submit</button>
  </fieldset>
</form>
<center><div class="fs-title">
  <p>Already Have an Account?</p>
  <p><a href="login.php">Login</a></p>
</div></center>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script type="text/javascript" src="createaccount.js"></script>

</html>

这里是server.php

<?php
session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array();

// connect to the database
$db = mysqli_connect('127.0.0.1', 'root', 'password', 'mydb');

// REGISTER USER
if (isset($_POST['register'])) {
  // receive all input values from the form
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password_1 = mysqli_real_escape_string($db, $_POST['pass']);
  $password_2 = mysqli_real_escape_string($db, $_POST['cpass']);
  $firstName = mysqli_real_escape_string($db, $_POST['firstName']);
  $middleName = mysqli_real_escape_string($db, $_POST['middleName']);
  $lastName = mysqli_real_escape_string($db, $_POST['lastName']);
  $dateOfBirth = mysqli_real_escape_string($db, $_POST['dateOfBirth']);
  $profilePic = mysqli_real_escape_string($db, $_POST['profilePicture']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($firstName)) { array_push($errors, "First Name is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }
  if (empty($lastName)) { array_push($errors, "Last Name is required"); }
  if (empty($dateOfBirth)) { array_push($errors, "Date of Birth is required"); }

  // first check the database to make sure
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM users WHERE username='$username' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO users (username, password, firstName, middleName, lastName)
              VALUES('$username', '$password', '$firstName', '$middleName', '$lastName')";
    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: index.php');
  }
}

// LOGIN USER
if (isset($_POST['login'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if (empty($username)) {
    array_push($errors, "Username is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
    $password = md5($password);
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
      header('location: index.php');
    }else {
        array_push($errors, "Wrong username/password combination");
    }
  }
}

 ?>

这与问题相关(我认为),但以防万一,这里是createaccount.js文件,因为由于我设置的动画,如果没有此文件,php文件将无法工作。

//jQuery
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches

$(".next").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show();
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            left = (now * 50)+"%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({
        'transform': 'scale('+scale+')',
        'position': 'absolute'
      });
            next_fs.css({'left': left, 'opacity': opacity});
        },
        duration: 800,
        complete: function(){
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

$(".previous").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    previous_fs = $(this).parent().prev();

    //de-activate current step on progressbar
    $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

    //show the previous fieldset
    previous_fs.show();
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale previous_fs from 80% to 100%
            scale = 0.8 + (1 - now) * 0.2;
            //2. take current_fs to the right(50%) - from 0%
            left = ((1-now) * 50)+"%";
            //3. increase opacity of previous_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({'left': left});
            previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
        },
        duration: 800,
        complete: function(){
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

0 个答案:

没有答案