Hi. I want to have a query as below.

$sql = "SELECT amount FROM payment WHERE id = '$a'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$amount = $row['amount'];

The value for $a variable is getting from the selection on drop down list. For example, the user select id=11, then the amount for this id is 10 which select from database. So how am I going to assign the value to $a based on the drop down selection of the user? Thanks in advance for any help.

Recommended Answers

All 3 Replies

You would have the drop down box as a form with a "submit" button which would redirect to the page with this query on. You would then use $_POST to get the value of that dropdown box.

Form page:

<!--Replace results.php with your results page-->
<form action="results.php" method="post"> 
  <select name="dropdown">
    <option value="5">6</option>
    <option value="6">7</option>
    <option value="7">8</option>
    <option value="8">9</option>
    <option value="9">10</option>
    <option value="10">11</option>
  </select><br />
  <input type="submit" value="submit" />
</form>

Results page:

<?php
  $a = $_POST['dropdown'};

  $sql = "SELECT amount FROM payment WHERE id = '$a'";
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result);
  $amount = $row['amount'];
?>

Thanks for the reply. But I want to do this without submit the form. So just after selection, then directly assign it. Any idea?

You would have the drop down box as a form with a "submit" button which would redirect to the page with this query on. You would then use $_POST to get the value of that dropdown box.

Form page:

<!--Replace results.php with your results page-->
<form action="results.php" method="post"> 
  <select name="dropdown">
    <option value="5">6</option>
    <option value="6">7</option>
    <option value="7">8</option>
    <option value="8">9</option>
    <option value="9">10</option>
    <option value="10">11</option>
  </select><br />
  <input type="submit" value="submit" />
</form>

Results page:

<?php
  $a = $_POST['dropdown'};

  $sql = "SELECT amount FROM payment WHERE id = '$a'";
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result);
  $amount = $row['amount'];
?>

In that case, you would need to load everything from the database to begin with but not display it anywhere then use javascript to check the currently selected value from the drop down list and only display the relevant entries.

w3schools.com has a few good tutorials which could probably help you do this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.