在同一页面上使用下拉结果

时间:2014-02-21 12:08:52

标签: javascript php jquery html

我有一个带下拉菜单的页面。我需要在停留在同一页面上时发布页面,然后使用该页面上选定的下拉选项来查询数据库。除了捕获变量之外,我有一切工作。我一遍又一遍地搜索,找到类似的例子,但没有一个能奏效。有人可以帮忙吗?

test.php的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>TheNuttWedding.com</title>
        <link href='http://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css' />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function rsvp_question() {
jQuery.noConflict()
document.getElementById('rsvp_question').style.display = "block";
document.getElementById('rsvp_no').style.display = "none";
};
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function rsvp_no() {
jQuery.noConflict()
document.getElementById('rsvp_no').style.display = "block";
};
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function rsvp_yes() {
jQuery.noConflict()
document.getElementById('continue').style.display = "block";
};
</script>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
<script language="JavaScript" type="text/javascript">
jQuery.noConflict()
function disable() 
{
length=document.main.rsvp_coming.length;

for (i=0;i<length;i++) 
{
    if (document.main.rsvp_coming[i].checked) 
    {
        for(i=0;i<length;i++)
            document.main.rsvp_coming[i].disabled="disabled";
    }
}

}
</script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" onSubmit="return false;" id="main" name="main" method="post">

    Select your name from the list below <br/><select name="rsvp_select" id="rsvp_select" onchange="rsvp_question();">
    <option 'select'>Select your Name Here</option>
<?php
$host = "localhost";
$user = "test";
$pass = "mandstest";
$db = "test";
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
$sql = "Select * FROM rsvp WHERE Name != '' ORDER BY Name ASC";
$query_result=mysql_query($sql,$connection);
while($row = mysql_fetch_array($query_result)) 
{
    echo "<option value='" . $row['Name'] . "'>" . $row['Name'] . "</option>";
}
?>
    </select>
<div id="rsvp_question" style="display:none;">
Is anyone in your party able to join us on our special day?<br/>
    <input type="radio" name="rsvp_coming" value="Yes" onclick="rsvp_yes();disable();"/>Yes&nbsp;&nbsp;
    <input type="radio" name="rsvp_coming" value="No" onclick="rsvp_no();disable();"/>No
    <input type="hidden" name="rsvp_coming" checked="checked"/>
</div>
<div id="continue" style="display:none;">
</form>
<?php

if(isset($_POST))
{
$names=$_POST['rsvp_select'];
$host = "localhost";
$user = "test";
$pass = "test";
$db = "test";
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
$sql = "Select * FROM rsvp WHERE Name = '.$names.'";
$query_result=mysql_query($sql,$connection);
$row = mysql_fetch_array($query_result);

if ($row['Quantity'] == 1){echo "Your invitation was for 1 person.<br/>";}
if ($row['Quantity'] != 1){echo "Your invitation was for ".$row['Quantity']." people.<br/>";}

if ($row['Quantity'] == 2){echo "Please tell us the full names of both people invited seperately.<br/>";}
if ($row['Quantity'] >= 3){echo "Please tell us the full names of everyone invited seperately.<br/>";}

if ($row['Quantity'] != 1){echo "This will tell us by name who is able to come or not and<br/>will allow everyone attending or not but were invited to sign<br/>our online guestbook.<br/><br/>";}

echo "Invitation is for ";
echo $row['Name'];
echo "<br/><br/>";
if ($row['Quantity'] == 2){
echo "...something...";}

if ($row['Quantity'] == 3){
echo "...something...";}    

if ($row['Quantity'] == 4){
echo "...something";}

if ($row['Quantity'] == 5){
echo "...something...";}

if ($row['Quantity'] == 6){
echo "...something";}

echo "<br/><br/>";

        ?><input type='hidden' name='Name' value='<?php echo         $_GET['rsvp_select']; ?> '/>
          <input type='hidden' name='coming' value='<?php echo $_GET['rsvp_coming']; ?> '/><?php

echo "Thank you for responding to your RSVP.<br/>";

echo "<br/><br/><input type='submit' name='submit' value='Complete RSVP'/>";
}   
?>
</div>
</body>
</html>

发布后,工作正常并保持在同一页面上,我正在使用

if(isset($_POST)){$names=$_POST['rsvp_select'];}

捕获下拉选择,然后我使用该变量来查询数据库。 它没有看到变量。

1 个答案:

答案 0 :(得分:1)

我一直在谷歌搜索,并找到了答案。我发布在下面。它运作得很好。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="form" method="POST" action="<?=$PHP_SELF?>">
  <select name="number" onchange="this.form.submit();" method="post">
    <option value="">Select Number</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
  </select>
</form>
<?PHP
  $num = $_POST['number'];
  $number = trim($num);
  if(!isset($num)){
print "Please select from the menu";
} else{
print $number;
}
?>
</body>
</html>