Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQueryAjax将数据传递到另一个页面_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 如何使用jQueryAjax将数据传递到另一个页面

Javascript 如何使用jQueryAjax将数据传递到另一个页面,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我在ajax调用中遇到问题 以下是我关于ajax的代码: $('#Subjects').click(function() { $.ajax({ type: 'POST', url: '../portal/curriculum.php', data: 'studentNumber='+$('#StudentID').val(), success: function(data) { $('

我在ajax调用中遇到问题

以下是我关于ajax的代码:

$('#Subjects').click(function() {
    $.ajax({
        type: 'POST',
        url: '../portal/curriculum.php',
        data: 'studentNumber='+$('#StudentID').val(),
        success: function(data)
        {
            $('#curriculum').html(data);
        }
    });
});
当我在另一页回显
studentNumber
时,
studentNumber
未定义的
。为什么会这样?


<!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>
$(文档).ready(函数(){ $(“按钮”)。单击(函数(){ $.post(“test1.php”, { 名称:“Makemelive Technologies”, 城市:“孟买” }, 功能(数据、状态){ 警报(“数据:+数据+”\n状态:+状态); }); }); }); 向页面发送HTTP POST请求并返回结果
上面将调用test1.php,其代码将

<?php

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

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

?>

只需像这样修改代码:

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

?>
1) 请使用完整的URL
http://yourdomain.com/portal/curriculum.php
或绝对路径,如
/portal/currency.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);
        }
    });
});
作为exsample,如果您在php上回显一些文本,它将返回数据$(“#课程”).html(数据)

尝试改变

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

//to 
success: function(result)
{
   $('#curriculum').html(result);
看看会发生什么。
也可以通过Jquery、Ajax和php使用post us php文件coursor.php

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

$(文档).ready(函数(){
$(“#目标”)。单击(函数(){
//警报(“测试”);
var frm_mail=document.getElementById(“frm_数据”);
var frm_mail_data=新表单数据(frm_mail);
$.ajax({
url:“http://localhost/test.php",
数据:frm_邮件_数据,
cache:false,
processData:false,
contentType:false,
键入:“POST”,
成功:功能(结果){
document.getElementById('div\u body\u users')。innerHTML=result;
}
});
});
});
第二步。创建test.php

  <?PHP

 //print_r($_POST);

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


使用
{studentNumber:$('#StudentID').val()}
同意Jake的观点,我还将放置一个“console.log($('#StudentID').val())就在ajax调用之前,查看它是否具有预期的值。您可能还想查看文档我已更改了数据,但它仍然未定义。我还使用console.log它具有一个值。我不知道我做错了什么。您如何访问另一个文件中的studentNumber?这不是答案。请立即检查..如果这对您有帮助..I我想你打给ajax时出错了。是的,它已经起作用了。我的问题似乎是echo的问题。我没有用$\u POST来回显结果。我只是回显学生号,谢谢你的帮助。谢谢,所以你能不能把我的回答标记为正确:)是的,它已经起作用了。我的问题似乎是echo的问题。我没有用$\u POST来回显结果我只是回显studentNumber,谢谢你的帮助。是的,它已经工作了。似乎我的问题是回显。。我没有使用$\u POST回显结果我只是回显studentNumber,谢谢你的帮助。介意提供一些解释,说明与其他答案相比,此代码有何帮助吗?
<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>
  <?PHP

 //print_r($_POST);

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