在列表框中动态注入一些内容,然后用PHP取回它


Inject dynamically some content on a listbox and get it back with PHP

我创建了一个网页,它使用JQuery将表单的内容重定向到另一个网页,使用PHP连接到数据库以找到一些内容并将其放回第一页。

一切都很好(感谢stack overflow的追随者的帮助:-)),但现在我想要以下内容:我正在询问一个城市的邮政编码,如果我很幸运,这个邮政编码是唯一的(只有一个城市有它),但它也发生了,邮政编码是相同的几个城市,所以我想在这种情况下显示一个列表框供用户选择他/她的城市。有人知道怎么做吗?

my code:

home。

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <form action="/" id="myform">
   <input type="text" name="postal_code" id="postal_code" placeholder="Search..." />
   <input type="submit" value="Search" />
  </form>
  <!-- the result of the search will be rendered inside this div -->
  <div id="result"></div>
<script>
$('#myform').submit(function() {
   var url = 'target.php';
   var postal_code = $('#postal_code').val();
   $.post( url, { postal_code: postal_code },
      function( data ) {          
          $( "#result" ).empty().append( data );
      }
    );
   return false;
});

target.php

<?php
try
{
   $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
   $bdd = new PDO('mysql:host=localhost;dbname=mydatabase', 'root', '', $pdo_options);
   $response = $bdd->prepare('SELECT city FROM city_list where postal_code = ?');
   $response->execute(array($_POST['postal_code']));
   echo '<ul>';
       while ($data = $response->fetch())
       {
   ?>
          <br/>The city you entered the postal code is : <?php echo $data['city'];  
       }
       $response->closeCursor();
}
catch (Exception $e)
{
        die('Error : ' . $e->getMessage());
}
?>
编辑:

这是我需要的代码。我只需要从Jules的代码中做一些非常小的改变,使它ok(因为一个未知的原因,他的答案对他来说是完美的,但不适合我:-))

<?php
try {
//Get the postal code:
$postcode = $_POST['code_postal'];
//Make MySQL connection
mysql_connect("localhost", "root", "") or die (mysql_error());
//Select the database
mysql_select_db("site_artisans_amélioré");
//Do your query based on the postcode...
$query = "SELECT ville FROM liste_communes_code_postaux where code_postal = '" . mysql_real_escape_string($postcode) . "'";
//Return the response in a variable
$data = mysql_query($query) or die (mysql_error());
//echo "Num rows: " . mysql_num_rows($data);
//Check how many rows the query returned. If more than 1 that means several cities
//exist for one postcode, so you should show a listbox.
//If not, just return the ville name
if (mysql_num_rows($data) > 1) { ?>
    <select name="cities">
<?php  while ($row = mysql_fetch_assoc($data)) {  ?>
        <option value="<?php echo $row['ville']?>"><?php echo $row['ville']?></option>
<?php  } ?>
    </select>    
<?php }
else {
    $row = mysql_fetch_assoc($data);
    echo $row['ville'];
    }
}
catch (Exception $e) {
   die("Error : " . $e->getMessage());
}
?>

我不确定你使用的数据库查询库,所以我会在伪代码和mysql_query..

target.php

<?php
try {
    //Get the postal code:
    $postcode = $_POST['postal_code'];
    //Make MySQL connection
    mysql_connect("localhost", "username", "password") or die (mysql_error());
    //Select the database
    mysql_select_db("mydatabase");
    //Do your query based on the postcode...
    $query = "SELECT city FROM city_list where postal_code = '" . mysql_real_escape_string($postcode) . "'";
    //Return the response in a variable
    $data = mysql_query($query);
    //Check how many rows the query returned. If more than 1 that means several cities
    //exist for one postcode, so you should show a listbox.
    //If not, just return the city name
    if (mysql_num_rows($data) > 1) { ?>
        <select name="cities" multiple="multiple">
    <?  while ($row = mysql_fetch_assoc($data)) {  ?>
            <option value="<?=$row['city']?>"><?=$row['city']?></option>
    <?  } ?>
        </select>    
 <? }
    else {
        $row = mysql_fetch_assoc($data);
        echo $row['city'];
    }
}
catch (Exception $e) {
    die("Error : " . $e->getMessage());
}
?>

我希望你明白我的意思,你可以自己完成。