0

我正在尝试从第一个投递箱中过滤出第二个投递箱。第一个$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;

?> 
4

1 回答 1

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>
于 2013-02-05T18:33:26.330 回答