0

I am using jQuery AJAX to lead a page with information, from here (on a click event) information is sent to the server to be dealt with and processed, but for some reason it is not working.

My jQuery looks like this:

$( document ).ready(function () {
    $.ajax({
        url: "../content/page-items/header.bootstrap.php",
        type: "GET",
        success: function(R) {
            $("head").append(R);
        }
    });

    $.ajax({
        url: "src/page/reg.php",
        type: "GET",
        success: function(R) {
            $("#register-area").html(R);
        }
    });

    $("#register").click(function() { alert("Clicked"); })

    $("#register").on("click", function() {
        alert("Clicked");
        $.ajax({
            url: "src/script/register_user.php?username=" + $( "#username" ).val() + "&password=" + $( "#password" ).val() + "&email=" + $( "#email" ).val() + "&role=" + $( "#roles option:selected" ).val(),
            type: "GET",
            success: function(R) {
                $("#area").html(R);
            }
        });
    });
});

My basic HTML page is thus:

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="../scripts/ajax/register.document.ready-functions.js"></script>
</head>

<body class="container-fluid">
    <div class="row">
        <div class="col-sm-3"></div>
        <div class="col-sm-6" id="register-area"></div>
        <div class="col-sm-3"></div>
    </div>
    <div class="row">
        <div class="col-sm-3"></div>
        <div class="col-sm-6" id="area"></div>
        <div class="col-sm-3"></div>
    </div>
</body>
</html>

And the src page that handles the data going into the form is:

<?php require_once 'func.php'; ?>
<h1 style="text-align: center;">
    Register new user
</h1>
<hr />
<table class="table table-condensed">
    <tr>
        <td><label for="username">Username</label></td>
        <td><input type="text" id="username" placeholder="Username" class="form-control" /></td>
    </tr>
    <tr>
        <td><label for="password">Password</label></td>
        <td><input type="password" id="password" placeholder="Password" class="form-control" /></td>
    </tr>
    <tr>
        <td><label for="email">Email</label></td>
        <td><input type="text" id="email" placeholder="Email Address" class="form-control" /></td>
    </tr>
    <tr>
        <td><label for="roles">Role</label></td>
        <td><?php print UserRoleSelect(); ?></td>
    </tr>
    <tr>
        <td colspan="2"><button id="register" class="form-control btn btn-primary">Register user</button></td>
    </tr>
</table>

And (hopefully) finally, the script that deals with the request is built as such:

<?php
require_once '../../../config.php';    

function UserRoleSelect()
{
    global $link;
    $query = "SELECT * FROM `user_roles`;";
    $roles = mysqli_fetch_all($link->query($query));
    $return = '<select id="roles" class="form-control">';
    $return .= '<option value="">-- Please select --</option>';
    foreach ($roles as $role)
    {
        $return .= '<option value="'.$role[1].'">'.$role[1].'</option>';
    }
    $return .= "</select>";
    return $return;
}
?>

As you can see I have used alerts in the jQuery script to be able to show me where the script is up to, but this does not show anything, I have tried both the .on("click"... function and the .click(function... to try and get the alert to show and I am not getting any errors from the console in Chrome's inspector nor FireBug.

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
Can O' Spam
  • 2,459
  • 1
  • 15
  • 42

0 Answers0