简体   繁体   中英

AJAX success function works, but file isn't running

I'm new to StackOverflow, web development, and I'm low on time for this assignment, so I apologize if I'm a bit slower at understanding web development vocabulary and any answers or tips. I'm having trouble with calling another php file through ajax. index.php:

<!--------------------------- HTML STARTUP ----------------------------------->

<!DOCTYPE html>
<html>
    <head>        
        <link type="text/css" rel="stylesheet" href="finalproject.css" />

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script type="text/javascript" src="finalproject.js"></script>

        <title>Final Project</title>
    </head>
    <body>

<!--------------------------- LOGIN ------------------------------------------>

<?php
    require_once 'dbconnection.php';

//----------------------- CREATE USERS TABLE -----------------------------------

$userspass = "CREATE TABLE usersPass (
    userID VARCHAR(60),
    password VARCHAR(60)
);";

$connected->query($userspass);

$selectAllInfo = "SELECT * FROM usersPass;";
$connected->query($selectAllInfo);

//-------------------------- LOG IN OR REGISTER --------------------------------
?>
    <form id="trial"><button onclick="OpenRegister()">Sign Up!</button>
    <script>
$(document).on("click" , "#Register", function(e)
    {
       var datastring = $("#trial").serialize();
        $.ajax({
            type: 'POST',
            url: 'userlogin.php',
            data: datastring,
            success: function(data){
            alert('Sign Up function is a success!');
        }
    });
    e.preventDefault();
});
        </script></form>
    <br/><br/>
    <button onclick="InputInfo()">Login</button> 

<?php        
$connected->close();
?>

</body>
</html>

And here's the JavaScript when the function is called. finalproject.js:

/*jslint browser: true*/
/*global $, jQuery, alert*/

function OpenRegister() {
'use strict';

$('form').append("<div id=SignInContainer>
<span style='font-size: 20px'>UserID</span>
<input type='text' name='UserID' value='Type userID here...'>
<span style='font-size: 20px'>Password</span>
<input type='text' name='UserID' value='Type password here...'>
<button id='Register'>Submit</button>
</div>");
}

$(document).ready(function () {
    'use strict';

    $(document).on('focus', 'input', function () {
        $('input').focus(function () {
            $(this).val('');
        });
    });
});

And this is the php file I'm trying to load. userlogin.php:

<?php echo "If this displays, you win." ?>

dbconnection.php

<?php
$connected = new mysqli('localhost', 'Username', 'Password', 'Username');
mysqli_select_db($connected, 'cferna50');

// Check connection
if ($connected->connect_error) {
    die("Connection failed: " . $connected->connect_error);
}

I'm just trying to get the "userlogin.php"file to run through AJAX. I'm running this thing on Chrome. I've heard that there is a bit of a problem with using AJAX on local files, but I tried that --access-file-from-files thing and it didn't help. I tried running it on FireFox and Internet Explorer and it still doesn't help. I'm sure I have everything in the same directory. I'm using an online school server thing to do all this. I've been hurting my head over this for endless hours, any help would GREATLY be appreciated.

Simply because you are not passing (data) through the method, either you Serialize the data or pass them individually, and i suppose you will use the first method to save time

<script>
    $(document).on("submit","#loginForm", function(e)
        {
           var datastring = $("#loginForm").serialize();
            $.ajax({
                type: 'POST',
                url: 'userlogin.php',
                data: datastring,
                success: function(data){
                 alert(data);
                //or
                //alert('Sign Up function is a success!');
            },
                error: function(){
                 alert("error handling");
            }
        });
        e.preventDefault();
    });
</script>

HTML

<form id="loginForm" method="POST">
  <input type="text" name="userid" id="userid" placeholder="user ID" />
  <input type="text" name="password" id="password" placeholder="Password" />
  <button type="submit" >Submit</button>
</form>

PHP

<?php
if(isset($_POST['userid'])){
   $username = $_POST['userid'];
   $pass = $_POST['password'];

   //do your magic now
  }
 ?>

Correct concatenation of html string at .append() remove if($("#Register").click())

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
  function SignUp() {
  if ($("[name=username]").val().length 
     && $("[name=password]").val().length) {
    $.ajax({
      type: 'POST',
      url: 'userlogin.php',
      data: {
          username: $("[name=username]").val(),
          password: $("[name=password]").val()
      },
      success: function(data) {
        alert('Sign Up function is a success!');
      },
      error: function() {
        alert("stacsnippets success")
      }
    });
    }
  }
</script>
<script>
  function OpenRegister() {
    'use strict';

    $('body').append("<div id=SignInContainer>" 
      + "<span style='font-size: 20px'>UserID</span>" 
      + "<input type='text' name='username' placeholder='Type userID here...'>" 
      + "<span style='font-size: 20px'>Password</span>" 
      + "<input type='password' name='password' placeholder='Type password here...'>" 
      + "<button onclick='SignUp()' id='Register'>Submit</button>" 
      + "</div>");
  }
</script>
<button onclick="OpenRegister()">Sign Up!</button>

php

if (isset($_POST["username"]) && isset($_POST["password"])) {
  // do username, password validation stuff here
}

jsfiddle https://jsfiddle.net/qnrnn5b5/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM