最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass data to another page using jquery ajax - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited May 19, 2015 at 6:10 miensol 41.6k9 gold badges125 silver badges115 bronze badges asked May 19, 2015 at 0:14 kathleen55kathleen55 3412 gold badges9 silver badges29 bronze badges 9
  • 8 Use { studentNumber: $('#StudentID').val() } – Jake Opena Commented May 19, 2015 at 0:15
  • 1 Agree with Jake, I would also place a "console.log($('#StudentID').val()) just before the ajax call to see whether it has the expected value. – Jobst Commented May 19, 2015 at 0:22
  • You may also want take a look at the doc jQuery AJAX – Jake Opena Commented May 19, 2015 at 0:28
  • I have change the data but still it is undefined. i also use console.log it has a value. i dont know what i've done wrong. – kathleen55 Commented May 19, 2015 at 0:28
  • 1 How are you accessing studentNumber in the other file? – Gary Holiday Commented May 19, 2015 at 1:00
 |  Show 4 more comments

5 Answers 5

Reset to default 3

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);
        }
    });
});
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.post("test1.php",
        {
          name: "Makemelive Technologies",
          city: "Mumbai"
        },
        function(data,status){
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});
</script>
</head>
<body>

<button>Send an HTTP POST request to a page and get the result back</button>

</body>
</html>

The above will make a call to test1.php and its code will be

<?php

$fname=$_REQUEST['name'];
$city= $_REQUEST['city'];

echo "Company Name is ". $fname. " and it's located in ". $city ;

?>
 $('#Subjects').click(function() {
      $.ajax({
      type: 'POST',
      url: '../portal/curriculum.php',
      data: { studentNumber: $('#StudentID').val() },
      success: function(data)
       {
        //here data is means the out put from the php file it is not $('#StudentID').val()
        $('#curriculum').html(data);
       }
      });
    });

as exsample if you echo some text on php it will return with data $('#curriculum').html(data);

try to change

//change
success: function(data)
{
   $('#curriculum').html(data); 

//to 
success: function(result)
{
   $('#curriculum').html(result);

check what will happen. post us php file too curriculum.php

You can use through Jquery,Ajax and php

step 1. index.php

<div id="div_body_users">
</div>
<form method="post" id="frm_data" action="">
<input type="button"  id="target" name="submit" value="submit">
<input type="key" id="key" name="key" value="1234">
</form>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
$(document).ready(function(){
   $( "#target" ).click(function() {
//  alert("test");
  var frm_mail = document.getElementById("frm_data");
  var frm_mail_data = new FormData(frm_mail);
    $.ajax({
        url: "http://localhost/test.php",
      data: frm_mail_data,
                    cache: false,
                    processData: false,
                    contentType: false,
        type: 'POST',
        success: function (result) {
             document.getElementById('div_body_users').innerHTML=result;
        }
    });

 });

});
</script>

step 2. create test.php

  <?PHP

 //print_r($_POST);

if($_POST['key']=='1234'){
    echo "success";
    exit(0);
 }
 ?>
$.ajax({
    type: "GET",
    url: "view/logintmp.php?username="+username+"&password="+password,
}).done(function( msg ) {
    var retval = printmsgs(msg,'error_success_msgs');
    if(retval==1){
        window.location.href='./';
    }
});
发布评论

评论列表(0)

  1. 暂无评论