动态显示所选选项

时间:2014-10-03 07:54:25

标签: php jquery mysql

我见过相关问题,但对我帮助不大。 我想在表格中显示所选的选项。

我使用函数 admin_items()来填写表格中的选项,我需要将所选值传递给 show_editable()函数

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

1 个答案:

答案 0 :(得分:3)

我想你需要将这两个功能分成不同的文件

在加载页面时应运行admin_items(),以便填充选项

然后,当用户从选项中选择时,发布ajax调用以运行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);
                }
            });
});

然后在show_editable()函数中,只需添加一行即可获取发布的ID:

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

并回显HTML代码。

然后JavaScript将执行onSuccess并完成剩下的工作。

希望这有帮助