过滤PHP中的下拉框

时间:2013-01-28 21:24:04

标签: php

我正在尝试过滤第一个掉落的第二个下拉框。第一个$box1工作正常。但是我不能让$box2建立第一个。如果我改变

SELECT Site FROM companiesandsitessql WHERE Company ='$box1'"); 

SELECT Site FROM companiesandsitessql WHERE Company =”CompanyA);
然后,它会在第二个框中拉出CompanyA的所有网站。我尝试了$box1的许多变体,但我必须遗漏一些东西。任何想法都赞赏。

<?php
$dbhost  = 'localhost';
$dbname  = 'escalations';
$dbuser  = 'root';

$con = mysql_connect($dbhost, $dbuser);
 {
    $box1 = array();
    mysql_select_db('escalations');
    $result = mysql_query("SELECT distinct Company FROM companiesandsitessql");
    while($row = mysql_fetch_array($result)) { $box1[] = $row; }

}
/* Generate select box contents */
$out1 = '<select name="box1">';
$out1 .= '<option>Select Company</option>';
if (!empty($box1)) {
foreach ($box1 as $k => $v) {
    $out1 .= '<option value="'.$v['Company'].'">'.$v['Company'].'</option>';
    }
}
$out1 .= '</select>';
/* Output */
echo $out1;


 {
$box2 = array();
    mysql_select_db('escalations');
    $result2 = mysql_query("SELECT Site FROM companiesandsitessql WHERE Company        ='$box1'");
    while($row2 = mysql_fetch_assoc($result2)) { $box2[] = $row2; }

}
/* Generate select box contents */
$out2 = '<select name="box2">';
$out2 .= '<option>Site list</option>';
if (!empty($box2)) {
    foreach ($box2 as $k1 => $v) {
        $out2 .= '<option value="'.$v['Site'].'">'.$v['Site'].'</option>';
    }
}
$out2 .= '</select>';
/* Output */
echo $out2;

?> 

1 个答案:

答案 0 :(得分:0)

Figured it out, here’s the code.

***File new.php***

<?php
$dbhost  = 'localhost';
$dbname  = 'escalations';
$dbuser  = 'root';

$con = mysql_connect($dbhost, $dbuser);
?>
<html>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

function autoSubmit() {
$.post('dropoutput.php', { name: box1form.CompanyName.value},
 function (output) {
    $('#info').html(output).show();
    });
    }
</script>

<body>
<form name="box1form" action="insert.php" method="post">
<?php

{
 $box1 = array();
 mysql_select_db('escalations');
 $result = mysql_query("SELECT distinct Company FROM companiesandsitessql ORDER BY Company ASC");
 while($row = mysql_fetch_array($result)) { $box1[] = $row; }

 }
/* Generate select box contents */
$CompanyName = '<select name="CompanyName" onchange="autoSubmit()">';
$CompanyName .= '<option selected="selected">Select Company</option>';
if (!empty($box1)) {
 foreach ($box1 as $k => $v) {
    $CompanyName .= '<option value="'.$v['Company'].'">'.$v['Company'].'</option>';
  }
 }
$CompanyName .= '</select>';
/* Output */
echo $CompanyName;

?>

<div id="info"></div><input name="send" type="submit" value="submit"><br>
</form>
</body>
</html>

***file dropoutput.php***

<?php
mysql_connect("localhost", "root", "") or die (mysql_error ());

$name = mysql_real_escape_string($_POST['name']);

{
$box2 = array();
 mysql_select_db('escalations');
$result2 = mysql_query("SELECT Site FROM companiesandsitessql WHERE Company ='$name' ORDER BY Site ASC");
while($row2 = mysql_fetch_assoc($result2)) { $box2[] = $row2; }

}
 /* Generate select box contents */
 $S = '<select name="SiteOptions[]" multiple="multiple">';
 $S .= '<option>Site list</option>';
 if (!empty($box2)) {
  foreach ($box2 as $k1 => $v) {
    $S .= '<option value="'.$v['Site'].'">'.$v['Site'].'</option>';
  }
 }
$S .= '</select>';
/* Output */
echo $S;

?>

 ***file  insert.php...

<?php
$dbhost  = 'localhost';
$dbname  = 'escalations';
$dbuser  = 'root';
$dbpass  = '';

$con = mysql_connect($dbhost, $dbuser);

if($con == FALSE)
{
echo 'Cannot connect to database' . mysql_error();
}
else
{
 echo " Connected to database: ";
}

$Site = implode(', ', $_POST['SiteOptions']);


mysql_select_db("escalations", $con);
$sql="INSERT INTO escalationtrackersql (CompanyName, Site)
VALUES
('$_POST[CompanyName]', '$Site')";

echo "Added $_POST[CompanyName] ";
print $Site;

if (!mysql_query($sql, $con))
{
die('Error: ' . mysql_error());
}

echo "  : Escalation request successfully entered.";

mysql_close($con)

?>

<html>
<head>
<meta content="en-us" http-equiv="Content-Language">
</head>
<body bgcolor="#C0C0C0">
<p>&nbsp;</p>
<p><a href="new.php">Return to the 
  escalation request form</a>.</p>

  </html>