1

I have a registraion php class that displays a form and when the registration button is clicked, calls a function in a login javascript file. This file uses ajax to post data to a index.php file. My index.php file cannot access this data, despite the post being a success (ajax success is true as the alert is being called).

Login.js

var loginData, urlPath;

// Allow users to log in or register
function Login() {

    loginData = "username=" + $("#usernameField").val() + "&email=" + $("#emailField").val() + "&password=" + $("#passwordField").val();
    urlPath = "../index.php?action=register";

    // Send the login/registration data to database
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: urlPath,
            data: loginData,
            success: function (result) {
                alert("success");
            }
        })
    })
}

index.php

<?php 

    require_once("Model/model.php");
    require_once("Controller/controller.php");
    require_once("View/view.php");

    $model = new Model();
    $view = new View();
    $controller = new Controller($model, $view);

    $controller->Begin();

    // Client wants to register
    if(isset($_GET['action'])) {
        if($_GET['action'] == "register") {
            echo '<script type="text/javascript">alert("hello")</script>';
        }
    }
?>
4
  • 2
    You are using post method in Ajax but receiving as get method in php try changing one Commented Nov 24, 2017 at 4:08
  • 1
    the reason GET is being used is to get the action from the url ../index.php?action=register i use post to get the post data i will need later Commented Nov 24, 2017 at 4:10
  • So when you put print_r($_POST); just to the beginning of the script, what do you get? Use Chrome's "Inspect element -> Network" to check ajax request. Additionally you can put your action to POST in ajax as well. Commented Nov 24, 2017 at 4:14
  • I checked the ajax request in network, it does contain the "action = registration" data Commented Nov 24, 2017 at 4:29

2 Answers 2

2

You used POST method of ajax. So send data also in POST manner like below:-

// Send the login/registration data to database
$(document).ready(function() {
    var username = $("#usernameField").val();
    var email = $("#emailField").val();
    var password = $("#passwordField").val();
    $.ajax({
        type: "POST",
        url: "../index.php",
        data: {"username":username,"email":email,"password":password,"action":"register"},
        success: function (result) { 
            alert(result);//check the change 
        }
    });
});

And then change GET to POST at php end:-

<?php 

    require_once("Model/model.php");
    require_once("Controller/controller.php");
    require_once("View/view.php");

    $model = new Model();
    $view = new View();
    $controller = new Controller($model, $view);

    $controller->Begin();

    // Client wants to register
    //single condition can do the job and use POST instead of GET
    if(isset($_POST['action']) && $_POST['action'] == "register" ) { 
      echo "hello"; //check the change
    }
?>

Note:- Please take care of comments too.(added in the code)

Sign up to request clarification or add additional context in comments.

1 Comment

@Jake glad to help you buddy:):)
0
loginData = "username=" + $("#usernameField").val() + "&email=" + $("#emailField").val() + "&password=" + $("#passwordField").val() + "&action=" + "register";
urlPath = "../index.php";

$(document).ready(function() {
        $.ajax({
            type: "POST",
            url: urlPath,
            data: loginData,
            success: function (result) {
                alert("success");
            }
        })
    });

Try adding the action also in post data and receive it as $_POST

if($_POST['action']) {
        if($_POST['action'] == "register") {
            echo '<script type="text/javascript">alert("hello")</script>';
        }
}

6 Comments

Didnt seem to work, the hello alert is not being called.
remove isset and try again
no change, except a php notice telling me "Undefined index: action"
try printing the $_POST values
i tried but nothing showed, im wondering is the reason the hello is not being alerted is because at the time of the post im on the registration.php page and not index.php, which is where the alert is triggered?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.