我正在构建一个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是服务器端的,但我不确定如何在这里实现它。
提前感谢您的帮助!
答案 0 :(得分: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">
这应该可以解决问题。