Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:47:55+00:00 2026-05-24T09:47:55+00:00

I’ve created a webpage which uses JQuery to redirect the content of a form

  • 0

I’ve created a webpage which uses JQuery to redirect the content of a form to another webpage using PHP to connect to a database to find some content and put it back on the first page.

Eveything works great (thanks to the help of followers of stack overflow 🙂 ) but now I’d like the following : I’m asking for the postal code of a city, if I’m lucky this postal code is unique (only one city has it) but it also happens that a postal code is the same for several cities so I’d like in that case to display a listbox for the user to choose his/her city.
Does someone has an idea of how to do this ?

my code :

home.html

<!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());
}
?>

EDIT:

This is the code ok for my needs. I’d only to make some very minor changes from Jules’ code to make it ok (for an unknow reason his answer worked perfectly for him but not for me 🙂 )

<?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());
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:47:56+00:00Added an answer on May 24, 2026 at 9:47 am

    I am not sure which library you are using for your Database queries, so I’ll do it in Pseudo-code and 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());
    }
    ?>
    

    I hope you catch my drift and you can complete it yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.