weixin_33749242 2015-03-12 16:30 采纳率: 0%
浏览 16

在AJAX中使用GET方法

I have a PHP form which has a field called "Travelling from:". This field is a drop-down box which has 4 options (routes) to choose from.

The HTML for this field is as follows;

<form id="bookingform" action="http://www.example.com/wp-content/themes/theme1/ajax-getvalues.php" method="get"> 
    <p>
        <b>Travelling from:</b><br>
        <select name="from" id="from" style="-webkit-appearance: menulist-button; width: 300px; position: absolute; height: 30px; font-size: 13px; padding: 5px;">
            <option value="">Select a Route...</option>
            <?php
            while ($row= mysqli_fetch_array($route)) {
                echo "<option value='". $row['Route'] ."'>" . 
                          $row['Route'] . 
                     "</option>";
            }
            ?>
        </select>
    </p><br><br>
</form>

Within the the head section of this file, I have the following code which listens for the select list change and makes the AJAX call;

 <script type="text/javascript">
 $(document).ready(function($) {
     var route_id = 'from'; //Route ID
     $('#'+route_id).change(function(e) {
         //Grab the chosen value on route change
         var selectroute = $(this).val();
         $.ajax({
             url: 'http://www.example.com/wp-content/themes/theme1/ajax-getvalues.php?from='+ encodeURIComponent(selectroute),
             success: function(data) {
                 alert(data);
             },
             error: function (xhr, ajaxOptions, thrownError) {
                 alert(xhr.status + " "+ thrownError);
             }
         });
     });
 });
 </script>

The alert in the above code appears with the selected value from the dropdown box as expected - which adds to my confusion.

And finally, the PHP file (ajax-getvalues.php) has the following code to receive the value;

Yet $selectroute seems to be empty.

I used the following code in the PHP file to test if $select route was empty;

<?php
$connection = mysqli_connect("localhost", "user", "password", "dbname");

$selectroute = mysqli_real_escape_string($connection, $_GET['from']);
if  ($selectroute != "") {
    $result = mysqli_query($connection, "SELECT DATE_FORMAT(SailingTime,'%h:%i %p') AS FormattedTime FROM wp_timetable WHERE Route_ID = 1 AND '$selectvalue' BETWEEN StartDate AND EndDate");
}
echo '<option value="">Please select a time...</option>';

while($row = mysqli_fetch_array($result))
{
    echo '<option value="'.$row['FormattedTime'].'">' . $row['FormattedTime'] . "</option>";
    echo $row['FormattedTime'] ."<br/>";
}

mysqli_free_result($result);
mysqli_close($connection);

Basically, results are produced in another drop-down on the form if $selectroute is not empty. But the results are not produced - therefore $selectroute is empty.

  • 写回答

1条回答 默认 最新

  • weixin_33713503 2015-03-12 16:38
    关注

    The problem is two fold in your success callback function selectroute is just the value of your form element.

    var url = 'http://www.example.com/wp-content/themes/theme1/ajax-getvalues.php';
    $.ajax({
        url: url + '?from=' + encodeURIComponent(selectroute),
        success: function(data) {
            alert(data);
            // I think you want to append the ajax response to the form.
            $('#bookingform').append($(data));
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status + " "+ thrownError);
        }
    });
    

    Also in your PHP script you don't seam to be doing anything with $selectroute and there is an uninitialized variable $selectvalue

    <?php
    $connection = mysqli_connect("localhost", "user", "password", "dbname");
    
    $selectroute = mysqli_real_escape_string($connection, $_GET['from']);
    
    // initialize $selectvalue to today date
    $selectvalue = '2015-03-12';
    
    $query = "SELECT DATE_FORMAT(SailingTime,'%h:%i %p') AS FormattedTime
              FROM wp_timetable
              WHERE Route_ID = 1 
              AND '$selectvalue' BETWEEN StartDate AND EndDate";
    
    if  ($selectroute != "") {
        $result = mysqli_query($connection, $query);
    
        if ($result) {
            echo '<select>';
            echo '<option value="">Please select a time...</option>';
    
            while($row = mysqli_fetch_array($result)) {
                echo '<option value="'.$row['FormattedTime'].'">' .
                          $row['FormattedTime'] . 
                     '</option>';
            }
            echo '</select>';
            mysqli_free_result($result);
        }
    }
    
    mysqli_close($connection); 
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料