dongye9228 2016-05-08 07:10
浏览 19

无法获得AJAX注册表单提交

I am trying to incorporate this code to allow me to register a user after their details are given, and then load the new page, all done using AJAX.

Step 1: User registers enters their details in the form within register.php. Upon submit using the input with the ID "reg-submit", the details are passed to click.js.

Step 2: If the user is successful, the information is processed in click.js, and prepared via ajax to be passed to usersubmit.php for DB insertion.

Step 3: data has been successfully inserted into the DB and next-page.php loads into the "#main-content" div (located within index.php).

To help keep things in context. All pages are loaded within the #main-content div within index.php. They are all loaded via the same function used that you will see in the click.js portion upon ajax success. Register.php is simply one of the pages that loads within this div.

I need the registration to happen, and then load the next page while all of the back end database information is inserted appropriately.

register.php

<script src="js/click.js"></script>

<form action="click.js" method="post">

   <label for="first_name">First Name:</label>
   <input type="text" id="first_name" name="first_name" /><br>

   <label for="last_name" >Last Name:</label>
   <input type="text" id="last_name" name="last_name" /><br>

   <label for="username">Username:</label>
   <input type="text" id="username" name="username" /><br>

   <label for="password">Password:</label>
   <input type="text" id="password" name="password" /><br>

   <label for="email">Email:</label>
   <input type="email" id="email" name="email" /><br>

   <button type="submit" id="reg-submit" name="submit">Submit</button><br>

</form>

click.js

$(document).ready(function(){

  $('#reg-submit').click(function() {
     var firstName = $('#first_name').val();
     var lastName = $('#last_name').val();
     var userName = $('#username').val();
     var password = $('#password').val();
     var email = $('#email').val();
     var dataString = 'name1=' + firstName + '&lastname1=' + lastName + '&user1=' + userName + '&password1=' + password + '&email1=' + email;

     if (firstName == "" || lastName == "" || userName == "" || password == "" || email == "") {
         alert('missing some information');
     }  else {
         $.ajax({
           type: "POST",
           url: "usersubmit.php",
           data: dataString,
           cache: false,
           success: function(){
             $('#main-content').load('php/next-page.php').hide().fadeIn('slow');                
          }
        });
      };
   return false;
   });
});

the DB connection takes place within the users_db.php.

usersubmit.php

<?php 

   include 'users_db.php';

   $first1=$_POST['name1'];
   $last1=$_POST['lastname1'];
   $username1=$_POST['user1'];
   $pass1=$_POST['password1'];
   $email01=$_POST['email1']; 

   $userinfo = $conn->prepare("INSERT INTO registered_users (FirstName, LastName, Username, Password, Email) VALUES ('$first1', '$last1', '$username1'', '$pass1', '$email01')");

   $userinfo->execute();


   $conn = null;
?>

Much appreciated!

If you see any other problems I may have here outside of the form simply not submitting, feel free to point them out.

  • 写回答

2条回答 默认 最新

  • douyan5481 2016-05-08 07:28
    关注

    Change the

    <input type="button" id="reg-submit" name="submit" value="Submit"/>

    to

    <button type="button" id="reg-submit" name="submit">Submit</button>

    In order to avoid the form submitting and te ajax call will be triggered.

    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上