2

I am using html, PHP and MySQL to create a webpage that has a dropdown. The contents of the dropdown will be pulled from a MySQL database table. I am able to get the dropdown populated and I am able to get a search button created using PHP. My question is after selecting an entry from the dropdown how do I get the button to pull that info so I can do something else with it. In this case I want to do two things.

  1. Show the entry selected in the dropdown along with its associated information in the SQL table.
  2. have a second button that then sends you to a link based on the information collected from SQL.

In essence you select an 800 number from a dropdown. it shows you the 800 number and the name associated with the 800 number and then when you click a "go" button it sends you to their website.

All of the 800 number name associations exist in a SQL DB because it is dynamically changed.

Here's what I have so far:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
  <?php echo '<p>Hello World</p>'; ?>
  <?php
    mysql_connect("localhost", "root", "root") or die("Connection Failed");
    mysql_select_db("voice")or die("Connection Failed");
    $query = "SELECT * FROM 800db";
    $result = mysql_query($query);
  ?>
  <form mthod="post">
    <select id="8xx" name="dropdown1">
      <?php
        while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
      ?>
      <option value="<?php echo $line['8xxNumber'];?>">
       <?php echo $line['8xxNumber']; ?>
      </option>
      <?php } ?>
    </select>
  </form>

  <?php
    $selected_8xx = $_POST['8xx'];
    echo
      "<form action='' method='post'>
        <input type='submit' name='use_button' value='search' />
       </form>";

    if(isset($_POST['use_button'])) {
      echo "it should say $selected_8xx";
    }
  ?>
</body>
</html>

I do not get the desired results. In fact when I click the button it reverts the dropdown back to the default value and returns nothing. Anything you can do to help would be appreciated.

2
  • you have a typo <form mthod="post"> Commented Jun 30, 2015 at 17:27
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Also add or die(mysql_error()) to mysql_query(). Commented Jun 30, 2015 at 17:27

1 Answer 1

2

You're creating two forms. The one with the submit button doesn't have any values to send to the server, so it sends no values. (Other than the button itself.)

Remove your first form's closing tag and your second form's opening tag so the select and the input can be in the same form.

Aside from that, there are a number of other issues here, including but not necessarily limited to...

  • Typos, for example mthod instead of method in the form tag.
  • You're looking for $_POST['8xx'] when the element's name is dropdown1, you should be looking for $_POST['dropdown1']
  • Mixing your code which displays the form with your code which handles the form post is undoubtedly going to cause confusion regarding the ordering of page loads and mixups in server-side vs. client-side code.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. After those cleanups It seems to work so far. onto the next section. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.