mxr7350
mxr7350

Reputation: 1458

Dynamically display selected option

I've seen related questions but they don't help me a lot. I want to display the selected option in the table.

I use function admin_items() to fill out the options from the table, and I need to pass selected value to the show_editable() function

function admin_items() {
  $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  if ($mysqli->connect_error) {
    echo("Connection failed: " . mysqli_connect_error());
    exit();
  }
  $query = "SELECT * FROM Items";
  $result_set = mysqli_query($mysqli,$query);
  $num_rows = $result_set->num_rows;
  $array = array();
  $i = 1;
  echo "<h2>Select item to change:</h2>\n<select name='movie_list'>";
  while($row = mysqli_fetch_array($result_set)) {
    echo "<option value =\"" . $row['ID']. "\">" . $row['ProductName']. "</option>";
  }
  $mysqli->close();
  echo "</select><hr />";
}



function show_editable(){
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_error) {
  echo("Connection failed: " . mysqli_connect_error());
  exit();
}
$query = "SELECT * FROM Items WHERE ID = 1";//I would like to pass selected value here
$result_set = mysqli_query($mysqli,$query);
$num_rows = $result_set->num_rows;
echo "<br /><h3>Selected Item:</h3>\n
  <form action=\"admin.php\" method=\"post\">
  <table class='admin'>
    <tr class='header'>  
      <td>Product Name</td>
      <td>Description</td>
      <td>Quantity</td>
      <td>Price</td>
    </tr> ";
while($row = mysqli_fetch_array($result_set)) {
  echo "<tr><td><textarea style=\"resize:none\" type=\"text\" name=\"ProductName" . $row['ID'] . "\" cols=22 rows=3>" . $row['ProductName'] . "</textarea></td>
  <td><textarea type=\"text\" name=\"Description" . $row['ID'] . "\" cols=80 rows=10>" . $row['Description'] . "</textarea></td>
  <td><input type=\"text\" maxlength=\"4\" size=\"4\" name=\"Quantity" . $row['phoneID'] . "\" value=\"" . $row['Quantity'] . "\" /></td>
  <td><input type=\"text\" maxlength=\"4\" size=\"4\" name=\"Price" . $row['phoneID'] . "\"      value=\"" . $row['Price'] . "\" /></td>
  </tr>";
}
echo "</table>
<input type=\"submit\" name=\"goedit\" value=\" Edit Field(s) \" /><br />";

echo "</form><br /><hr />";

$mysqli->close();
}

Upvotes: 1

Views: 891

Answers (1)

Chen Sturmweizen
Chen Sturmweizen

Reputation: 639

I think u will need to split the two functions into different files

The admin_items() should be run during loading the page so that the options can be populated

Then when ever the user choose from the options, post an ajax call to run the show_editable().

$('#your_dropdown_id').on('change', function(){
              $.ajax({
                'type' : 'post',
                'data':{'id': $(this).val()},
                'url' : '../show_editable',//point to the show_editable()
                'success' : function(data)
                {
                    $('place_that_should_contain_the_html_codes').html(data);
                }
            });
});

Then in your show_editable() function, just add one line to get the posted ID:

$id = $_POST['id'];//the key for the param is up to u to define

and echo the HTML codes.

Then the JavaScript will do the onSuccess and finish the rest.

Hope this helps

Upvotes: 3

Related Questions