如何使用AJAX将数据发送到我的PHP脚本?

时间:2013-03-07 20:19:10

标签: php javascript ajax

我是PHP和Javascript / Ajax的新手,所以请耐心等待。

我需要做的就是从Ajax获取一个变量并将其设置为php中的变量。我试图用超级全局GET来做这件事,但有些事情是不对的。我不希望通过提交表格。

这是我的JS:

function myFunction(){
var hora= document.getElementById("hora").value;
$.ajax({ 
        type : 'GET',
         url : 'reservation.php',
         data : {hora: hora},
         success : function(data) {
                console.log(hora);//This is because I was curious as to
                             // what the console would say. I found
                             // that this sets the super global if I
                             // change the url to something else that
                             // doesn't exist. Console would say
                             // -GET http://localhost/bus/(somepage).php?hora=4
                             // 404 (Not Found)-  
                alert(hora);
               }
})
}

这是我的PHP:

Hora:
 <select name="hora" id="hora" onchange="myFunction()">
     <?php
         $query = "SELECT * FROM vans";
         $horas_result = mysql_query($query);
         while ($horas = mysql_fetch_array($horas_result)) {
        echo "<option value=\"{$horas["van_id"]}\">{$horas["time"]}</option>";
         }
    ?>
 </select>&nbsp;
 Asientos Disponibles:
    <?php echo $_GET["hora"]; ?>
        //Right now I only want to echo this variable..

正如你所看到的,现在我只想回显这个变量,稍后我将使用它来编写查询。

2 个答案:

答案 0 :(得分:0)

例如,以下脚本

$.ajax({
            type: "POST",
            url: "test.php",
            data: {value:1}
        }).done(function(msg) {
                    // msg contains whatever value test.php echoes. Whether it is code, or just raw data. 
            if(msg=="Success") { 
              alert("hello world"); 
              } else {
                alert("Hello Hell")
            }
        });

将变量$_POST['value']设置为1

我的test.php看起来像:

<?php
 if($_POST['value'] == "1") {
     echo "Success";
 } else {
     echo "Failure";
 }
?>

如果您运行该示例,网页将显示一个警告框,其中包含“Hello World”文本 如果您将值更改为任何其他数字,它将显示带有“Hello Hell”文本的警报

希望能回答你的问题。

答案 1 :(得分:0)

查看我发布的代码,ajax用于发布/获取数据而无需刷新页面,但如果您只想发布数据并在其他页面中提供结果,请使用表单。

<?php

    if (isset($_GET["hora"]))
    {
        echo $_GET["hora"];

        exit;
    }
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Page title</title>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

</head>

<body>

<script type="text/javascript">

        $(document).ready(function()
            {
                $("#hora").change(function ()
                    {
                        $.ajax(
                            { 
                                type : 'GET',
                                url : '',
                                data : $('select[name=\'hora\']'),
                                success : function(data)
                                    {
                                        $('#ajax_result').html('Asientos Disponibles:  ' + data);
                                    },
                                error: function(xhr, ajaxOptions, thrownError)
                                    {
                                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                                    }
                            }
                        )
                    }
                )

            }
        )

</script>

<select name="hora" id="hora">
     <?php
         $query = "SELECT * FROM vans";
         $horas_result = mysql_query($query);
         while ($horas = mysql_fetch_array($horas_result)) {
        echo "<option value=\"{$horas["van_id"]}\">{$horas["time"]}</option>";
         }
    ?>
 </select>&nbsp;
    <div id="ajax_result">
    </div>
</body>
</html>
相关问题