2

Its a bit hard to express what I want to do but let me give it a shot.

First of all I have a database with a table. One of the columns from that table I will be showing in a select dropdown menu in HTML, to be more precise: this outputs the column 'name' in the dropbown menu:

<?php
$query = mysql_query("SELECT name from table");

echo "<select name='select1'>";

while ($row = mysql_fetch_array($query))
{
echo "<option value'" . $row['name'] ."'>" . $row['name] . "</option>;
}
echo "</select>";
?>

Then when a value is selected in that dropdown, that value needs to be saved in a variable.

if (isset($_POST['button']))
{
$select = $_POST['select1'];
}

As last bit, the value in variable $select needs to be used to make a SELECT query. I want to use that variable in the SELECT statement to look for the corresponding row (in the same table) that is related to the value in the dropdown menu and pick a column and output that value to a textbox or checkbox, depending on as what the value needs to be outputted.

Example:

TABLE

      id - name - street - number - ...
row 1: 0 - test1 - teststreet1 - 1 - ...
row 2: 1 - test2 - teststreet2 - 2 - ...
row 3: 2 - test3 - tesstreett3 - 3 - ...

1) I select test2 from the dropdown menu (dropdown menu is filled with the column name from database)

2) test2 is saved as a variable in $name

3) select query searches for value of street column where $name = test2

SELECT street from table where ?
row 2: 1 - test2 - tesstreett2 - 2 - ...

So I want teststreet2 from street to be outputted in a textbox

<input type='text' value='?'>

I need help with the select query or how to call this.

Thanks in advance and taking time to read this!

Kind Regards

user3360972
  • 107
  • 2
  • 13
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 04 '15 at 12:47

2 Answers2

0

You can not change a state of any loaded dropdown or text as it is.

You must use jQuery/Ajax here.

When you select on Dropdown value it will than trigger Ajax/jQuery and call another page where your select query will run and return value, which you can display in textbox using jQuery.

<select name = 'select1' onchange='return get_street()'>
<option name='test1'>test1</option>
<input type="text" name ="street" id="street" value="" />

<script> 
    function get_street()
    {

       var selected_val = $('option:selected', this).val();

       $.ajax({  //ajax call
        type: "POST",      //method == POST 
        url: "street.php", //url to be called
        data: "name="+selected_val, //data to be send 
        success: function(data){
               $('#street').val(data); // here we will set a value of text box
           }
        });
    } 

 </script>

And your street.php will look like

<?php
  $name = $_POST['name'];
  $res = mysql_query("select street from table where name = '".$name."'");
  $row = mysql_fetch_assoc($res);
  return $row['street']; // this will return a name of street

?>

Few things you need to make sure is

  1. give appropriate path of your file in ajax

  2. give id to your text input and same should be in ajax success

  3. you must connect database in your street.php

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
php-coder
  • 955
  • 1
  • 12
  • 23
0

Found it!:

if(isset($_POST['select1']))
{
$select = $_POST['select1'];

$street_select = mysql_query("SELECT street FROM table WHERE name = '" .&select. "'");
$street = mysql_fetch_array($street_select);
$resul_street = $street['street'];
}

Now I can do this for every value in each column that is asked via the dropdown menu!

user3360972
  • 107
  • 2
  • 13