0

I'm trying to send a AJAX request to a PHP file. The PHP file does not receive the request.

I'm using the following code:

register.php:

$('#button_step1').click(function(e) {
        $.ajax({
            type: 'POST',
            url: '../core/views/cp/test.php',
            data: { message: 'works' },

            success: function(resp) {
                alert(resp);
            }
        });
        $('.step1 input').prop('disabled', true);
        window.location.replace("http://10.0.0.13/core/views/cp/test.php");
    });

test.php:

<?php

echo $_POST['message'];

?>

The message I get after I get redirected to test.php, "Notice: Undefined index: message in C:\xampp\htdocs\core\views\cp\test.php on line 3".

Issue 2:

When I require_once my register_view.php which includes the AJAX request into my register.php and let it send a AJAX request to the register.php file, it will show me the alert() which has all data from the whole PHP file, where it should only show 'works'.

Code,

register.php:

<?php

require_once '../core/init.php';

$_SESSION['message'] = $_POST['message'];
echo $_SESSION['message'];

require_once VIEW_ROOT . '/cp/register_view.php';

register_view.php:

$('#button_step1').click(function(e) {
        $.ajax({
            type: 'POST',
            url: '../core/views/cp/test.php',
            data: { message: 'works' },

            success: function(resp) {
                alert(resp);
            }
        });
        $('.step1 input').prop('disabled', true);
        //window.location.replace("http://10.0.0.13/core/views/cp/test.php");
    });
5
  • Remove the window.location.replace() part. Commented Mar 17, 2016 at 9:21
  • How would I check if the request is received on the test.php file? Commented Mar 17, 2016 at 9:22
  • It's in your success function parameter resp Commented Mar 17, 2016 at 9:22
  • Alright. That seems to be working alright. But there's another issue, let me add it to my post. Commented Mar 17, 2016 at 9:25
  • Note that you're not sending the datatype (for example: dataType: "html") Commented Mar 17, 2016 at 10:12

1 Answer 1

1

require_once VIEW_ROOT . '/cp/register_view.php'; // you are loading view there. you have to do something like this

 $a= $_POST['message'];
print_r(json_encode($a));exit;
Sign up to request clarification or add additional context in comments.

5 Comments

It still doesn't show me anything on the register.php page. Just null from the print_r().
are you getting value there at php ??
I don't understand your question. Sorry.
is php function is being called from your ajax .. what you see on console when you do console.log(resp);
It's blank, nothing logged

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.