使用PHP验证提交JQuery Mobile表单

时间:2016-06-24 17:57:06

标签: php jquery-mobile

我正在制作一个表单,将人员注册到JQM Web应用程序。我有一个MySQL数据库,我正在使用PHP来处理表单验证&将结果发送到DB。

我在register.php上有2个JQM页面,其中所有的html& PHP驻留。主表单有一个页面,一个页面显示成功'允许用户点击进入下一个新页面之前的消息。

一切(差不多)都很有效但我无法弄清楚如何指导注册成功'数据库更新成功完成后的页面,同时保持我认为最佳安全性使用。表格看起来像这样。 header()可以重定向到多页JQM网页中的特定#page吗?如果,那么这是如何实现的?...

<div data-role="page">
<div data-role="header">
<a href="index.html" data-rel="back" class="ui-btn-right ui-btn  ui-btn-icon-notext ui-corner-all ui-icon-back">Back</a>
        <img src="images/logo.png" alt="" style="width: 50%; height: 25%" />
</div>

<div data-role="main" class="ui-content">
    <form name="register" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" data-ajax="false">        
        <span class="error">* <?php echo $firstnameErr;?></span>
        <input type="text" name="firstname" id="firstname" placeholder="First Name" value="<?php echo $new_firstName;?>"/>

        <span class="error">* <?php echo $lastnameErr;?></span>
        <input type="text" name="lastname" id="lastname" placeholder="Last Name" value="<?php echo $new_lastName;?>"/>

        <span class="error">* <?php echo $emailErr;?></span>
        <input type="email" id="email" name="email" placeholder="Email" value="<?php echo $new_email;?>"/>

        <span class="error">* <?php echo $password1Err;?></span>
        <input type="password" id="password1" name="password1" placeholder="Password" value="<?php echo $new_password1;?>"/>

        <span class="error">* <?php echo $password2Err;?></span>
        <input type="password" id="password2" name="password2" placeholder="Confirm Password" value="<?php echo $new_password2;?>"/>

        <input type="submit" data-inline="true" value="Submit" name="submit" data-theme="b">
    </form>
</div>
</div>

PHP表单验证有效,只有在满足验证条件时才会更新数据库。我无法弄清楚如何跳转到下一页 - 感谢任何帮助。

这里的代码......我想登陆&#39;注册成功&#39;成功更新数据库中的数据后的页面...

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>

  <style>
h1,h2,h3,h4,h5 {
    color:#0071bc;
}

.error {color: #FF0000;
}
  </style>
</head>
<body>



<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

//set variables for content
$new_firstName = $new_lastName = $new_email  = $new_password1 = $new_password2 = $new_status = "";
$firstnameErr = $lastnameErr = $emailErr = $password1Err = $password2Err = $globalErr = "";

$servername = "";
$username = "";
$password = "";
$dbname = "";

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


if ($_SERVER["REQUEST_METHOD"] == "POST") {

  if (empty($_POST["firstname"])) {
    $firstnameErr = "First Name is required";
    $globalErr = "Error";
  } else {
    $new_firstName = test_input($_POST["firstname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$new_firstName)) {
      $firstnameErr = "Only letters and white space allowed";
      $globalErr = "Error";
    }
  }

  if (empty($_POST["lastname"])) {
    $lastnameErr = "Last Name is required";
    $globalErr = "Error";
  } else {
    $new_lastName = test_input($_POST["lastname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$new_lastName)) {
      $lastnameErr = "Only letters and white space allowed";
      $globalErr = "Error"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
    $globalErr = "Error";
  } else {
    $new_email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($new_email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
      $globalErr = "Error"; 
    }
  }


  if (empty($_POST["password1"])) {
    $password1Err = "Password is required";
    $globalErr = "Error";
  }

  if (empty($_POST["password2"])) {
    $password2Err = "Confirm Password is required";
    $globalErr = "Error";
  }

  if($new_password1 == $new_password2) {
          $new_password1 = test_input($_POST["password1"]);
          $new_password2 = test_input($_POST["password2"]);
    } else {
      $password1Err = "Passwords do not match!";
      $password2Err = "Passwords do not match!";
      $globalErr = "Error";
    }

$new_status = false;


//create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

//check connection
if (!$conn) 
{
    die("Connection failed: " . mysqli_connect_error());
}   

if(empty($globalErr)){
    $sql = "INSERT INTO tblAgentLogin(agentFirstName, agentLastName, agentEmail, agentPassword, status)
    VALUES ('$new_firstName', '$new_lastName', '$new_email', '$new_password1', '$new_status')";

    if (mysqli_query($conn, $sql))
    {       
    } else 
    {
        echo "Error: " . $sql . mysqli_error($conn);
    }

    mysqli_close($conn);    
}
}
?>




<div data-role="page">

<div data-role="header">
<a href="welcome.html" data-rel="back" class="ui-btn-right ui-btn  ui-btn-icon-notext ui-corner-all ui-icon-back">Back</a>
        <img src="images/logo.png" alt="" style="width: 50%; height: 25%" />
</div>

<div data-role="main" class="ui-content">
    <form name="register" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" data-ajax="false">        
        <span class="error">* <?php echo $firstnameErr;?></span>
        <input type="text" name="firstname" id="firstname" placeholder="First Name" value="<?php echo $new_firstName;?>"/>

        <span class="error">* <?php echo $lastnameErr;?></span>
        <input type="text" name="lastname" id="lastname" placeholder="Last Name" value="<?php echo $new_lastName;?>"/>

        <span class="error">* <?php echo $emailErr;?></span>
        <input type="email" id="email" name="email" placeholder="Email" value="<?php echo $new_email;?>"/>

        <span class="error">* <?php echo $password1Err;?></span>
        <input type="password" id="password1" name="password1" placeholder="Password" value="<?php echo $new_password1;?>"/>

        <span class="error">* <?php echo $password2Err;?></span>
        <input type="password" id="password2" name="password2" placeholder="Confirm Password" value="<?php echo $new_password2;?>"/>

        <input type="submit" data-inline="true" value="Submit" name="submit" data-theme="b">
    </form>
</div>
</div>



<!-------------- Second page - successful submission----------->
<div data-role="page" id="register-success">
<div data-role="header">
        <img src="images/logo.png" alt="" style="width: 50%; height: 25%" />
</div>
<!-------------- First page main content ----------->
<div data-role="main" class="ui-content">
    <p>You have successfully submitted your registration request.</p><br>
    <p>We will process, and be in touch soon.</p><br>
    <p>Thank you.</p><br>

    <a href="index.html" class="ui-btn">Continue</a>
</div>
</div>

</body>
</html>

0 个答案:

没有答案