1

我正在构建一个 jQuery Mobile 站点,使用 PHP 与 MySQL 数据库交互以动态填充下拉列表。这是创建第一页的代码(表单中的简单选择列表,带有提交按钮):

app.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>
    </title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <style>
        /* App custom styles */
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
    </script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js">
    </script>
</head>
<body bgcolor="#000000">
    <div data-role="page" data-theme="a" id="page1">
        <div data-theme="a" data-role="header">
            <h3>
                Header
            </h3>
        </div>
        <div data-role="content">
            <div data-role="fieldcontain">
                <form action="showView.php" method="POST">
                <label for="selectmenu1">
                    Select a show:
                </label>
                <select name="selectmenu1" id="select_a_show">
                 <?php 
$con = mysql_connect('tfis.db.7386546.hostedresource.com','tfis','Kurdt4794');

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('tfis', $con);

$queryStr = 'SELECT * FROM Shows';      
$result = mysql_query($queryStr);
while($row = mysql_fetch_array($result))
{
    $episode_num = $row['episode_num'];
    echo "<option value=" . $episode_num . ">Episode " . $episode_num . "</option>";
} 
mysql_close($con);
?>
        </select>
                <input type="submit" value="Submit" />
            </form>
            </div>
        </div>
    </div>
</body>

然后我有第二个页面来处理选定的值,我在 PHP 查询中使用以下代码对数据库进行查询:

showView.php:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>
    </title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
    </script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js">
    </script>
</head>
    <body>

<?php 
$con = mysql_connect('tfis.db.7386546.hostedresource.com','tfis','Kurdt4794');

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('tfis', $con);

$episode = $_REQUEST["select_a_show"];
$queryStr = "SELECT * FROM Shows WHERE episode_num = $episode LIMIT 1";
echo "Episode" . $episode . $queryStr;
$result = mysql_query($queryStr);

while($row = mysql_fetch_array($result))
{
    $episode_title = $row['episode_title'];
    $description = $row['description'];
    $filename = $row['filename'];
    $image_filename = $row['image_filename'];
    $youtube = $row['youtube'];
}
echo "<div data-theme='a' data-role='header'> <h3> " . $episode_title . "</h3>
        </div>
        <div data-role='content'>
        <img src='http://foxtechnyc.com/TFIS/images/" . $image_filename . "' />
            </div>
            <div>" . $description . "
            <a data-role='button' data-transition='fade' href='http://foxtechnyc.com/TFIS/audio/" . $filename . "'>
            Listen To Show</a>
            <a data-role='button' data-transition='fade' href='http://www.youtube.com/watch?v=" . $youtube . "&feature=player_embedded>
            Watch YouTube Video
            </a></div>";
?>
<script>
    alert(selection);
</script>
</body>

问题是使用以下代码未正确获取该值:

$episode = $_REQUEST["select_a_show"];

我从服务器阅读了 AJAX 长轮询,因为 JQuery 是客户端而 PHP 是服务器端,但我不确定如何在这里实现它。

在此先感谢您的帮助!

4

1 回答 1

1

在您的表单中,您的select字段名称应为name="select_a_show"

换句话说,将 app.php 中的第 30 行从 <select name="selectmenu1" id="select_a_show">

<select name="select_a_show" id="select_a_show">

这应该够了吧。

于 2012-05-18T21:00:41.813 回答