Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data to another page using jquery ajax

I have a problem on ajax call.

Here is my code regarding the ajax:

$('#Subjects').click(function() {
    $.ajax({
        type: 'POST',
        url: '../portal/curriculum.php',
        data: 'studentNumber='+$('#StudentID').val(),
        success: function(data)
        {
            $('#curriculum').html(data);
        }
    });
});

When I echo studentNumber on another page, the studentNumber is undefined. Why is that?

like image 833
kathleen55 Avatar asked May 19 '15 00:05

kathleen55


People also ask

How do I pass data from one ASPX page to another in Javascript?

Using the Codeajax() method says Welcome. aspx/getUserName, so this method will send the user name to the welcome. aspx page, precisely to the method named getUserName() . I have sent the user name to the code behind and on success of that transaction, I am redirecting/ opening a new window with Welcome.

How Ajax get data from another page in PHP?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="" src="action. js"></script> <? php $id = $_GET['id']; echo $id; ?>


1 Answers

Simply modify your code like this:

JS

$('#Subjects').click(function() {
    $.ajax({
        type: 'POST',
        url: '../portal/curriculum.php',
        data: { studentNumber: $('#StudentID').val() },
        success: function(data)
        {
            $('#curriculum').html(data);
        }
    });
});

PHP

<?php

    $var = $_POST['studentNumber'];

?>

If you still can not make it works.. other things you should consider..

url: '../portal/curriculum.php',

1) Please use full URL http://yourdomain.com/portal/curriculum.php or absolute path like /portal/curriculum.php

2) Add an error callback to check out the error message

$('#Subjects').click(function() {
    $.ajax({
        type: 'POST',
        url: '../portal/curriculum.php',
        data: { studentNumber: $('#StudentID').val() },
        success: function(data)
        {
            $('#curriculum').html(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
});
like image 96
Terry Lin Avatar answered Sep 23 '22 13:09

Terry Lin