如何使用jquery ajax将数据传递到另一个页面

时间:2015-05-19 00:14:13

标签: javascript php jquery ajax

我在ajax调用时遇到问题。

这是关于ajax的代码:

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

当我在另一页上回显studentNumber时,studentNumberundefined。那是为什么?

5 个答案:

答案 0 :(得分:3)

只需修改您的代码:

<强> 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'];

?>

如果你仍然无法使它工作..你应该考虑的其他事情..

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

1)请使用完整网址http://yourdomain.com/portal/curriculum.php或绝对路径,例如/portal/curriculum.php

2)添加错误回调以查看错误消息

$('#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);
        }
    });
});

答案 1 :(得分:1)

<!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>

以上将调用test1.php,其代码为

<?php

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

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

?>

答案 2 :(得分:1)

 $('#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);
       }
      });
    });

例如,如果你在php上回显一些文本,它将返回数据$('#curriculum')。html(data);

尝试改变

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

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

检查会发生什么。 发布我们的php文件too curriculum.php

答案 3 :(得分:0)

您可以通过Jquery,Ajax和php使用

步骤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>

第2步。创建test.php

  <?PHP

 //print_r($_POST);

if($_POST['key']=='1234'){
    echo "success";
    exit(0);
 }
 ?>

答案 4 :(得分:-3)

$.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='./';
    }
});