Подтвердить что ты не робот

Как передать данные на другую страницу с помощью 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 на другой странице, studentNumber - undefined. Почему это?

4b9b3361

Ответ 1

Просто измените свой код следующим образом:

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) Пожалуйста, используйте полный URL 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);
        }
    });
});

Ответ 2

<!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 located in ". $city ;

?>

Ответ 3

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

как exsample, если вы эхо-текст на php, он вернется с данными $('# curriculum'). html (data);

попытайтесь изменить

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

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

проверить, что произойдет. напишите нам также php файл curriculum.php

Ответ 4

Вы можете использовать через 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);
 }
 ?>

Ответ 5

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