0

我是新手php并且ajax想要显示一个充满数据库内容的表格。我成功了,但现在我正在尝试使用select. 我知道有很多网站解释如何做到这一点,但不知何故我不明白。对我来说最好的解决方案是在不重新加载页面的情况下更改表格或使用单独的按钮重新加载它。

我读过关于这样做的信息,ajax / javascript但正如我所提到的,我不熟悉这些事情。

下面是我已经工作的代码。

PHP:

<?php

$mysqlhost="localhost"; // 
$mysqluser="root"; //
$mysqlpwd=""; //
$mysqldb="wordpress"; //


$connection=mysql_connect($mysqlhost, $mysqluser, $mysqlpwd); 
mysql_select_db($mysqldb, $connection);


$sql = "SELECT id, user_email FROM wp_users";  
$db_query = mysql_query($sql);


?>
<table cellpadding="1" cellspacing="3" border="1">
    <tr>
        <td>ID</td>
        <td>Mail</td>
    </tr>
<?php

  while ($adr = mysql_fetch_array($db_query)){
?>

    <tr>
        <td><?=$adr['id']?></td>
        <td><?=$adr['user_email']?></td>
    </tr>
<?php
  }
?>
</table>

我的选择:

<select name="Choose" title="chose">
<option value="one" id="One">One</option>
<option value="two" id="Two">Two</option>
<option value="three" id="Three">Three</option>
</select>

我真的很感激一些代码或提示如何做到这一点。

4

2 回答 2

0
<script>
function reloadWithOptionValue(){
document.FilterFrom.submit();
}
</script>
<?php

$mysqlhost="localhost"; // 
$mysqluser="root"; //
$mysqlpwd=""; //
$mysqldb="wordpress"; //


$connection=mysql_connect($mysqlhost, $mysqluser, $mysqlpwd);

mysql_select_db($mysqldb, $connection);


$sql = "SELECT id, user_email FROM wp_users";  
if(isset($Choose) && !empty($Choose)){
      $sql.=" where id like '%$Choose%' or user_email like  '%$Choose%'";
}
$db_query = mysql_query($sql);


?>

<form method="post" name="FilterFrom">
<table cellpadding="1" cellspacing="3" border="1">

    <tr>

        <td>ID</td>

        <td>Mail</td>

    </tr>
<?php

  while ($adr = mysql_fetch_array($db_query)){

?>

    <tr>

        <td><?=$adr['id']?></td>

        <td><?=$adr['user_email']?></td>

    </tr>

<?php

  }

?>

</table>
<select name="Choose" title="chose" onchange="reloadWithOptionValue()">
<option value="one" id="One">One</option>
<option value="two" id="Two">Two</option>
<option value="three" id="Three">Three</option>
</select>
</form>

试试这个

于 2013-10-29T10:44:34.630 回答
0

这是代码:

html:

<table id="tableid"> //mention id for a table
 ......
 ......
 </table>

// create an event for select
<select name="Choose" title="chose" onchange="getajax(this.value)">
<option value="one" id="One">One</option>
<option value="two" id="Two">Two</option>
<option value="three" id="Three">Three</option>
</select>

javascript

function getajax(value){
$.ajax({
type: "GET",
url: "Ajaxpage.php",
data: {text:value},
success: function(data) {
  $("#tableid").html(data);
}
});
}

Ajaxpage.php:

<?php
$mysqlhost="localhost"; // 
$mysqluser="root"; //
$mysqlpwd=""; //
$mysqldb="wordpress"; //


$connection=mysqli_connect($mysqlhost, $mysqluser, $mysqlpwd); //use mysqli instead of mysql

mysqli_select_db($mysqldb, $connection);


$sql = "SELECT id, user_email FROM wp_users where someid='".$_GET['text']."'";  

$query = mysql_query($sql);
 while($row= mysql_fetch_array(query)){
 echo "<tr><td>".$row['id']."</td><td>".$row['user_email']."</td></tr>";

 }


?>
于 2013-10-29T10:27:54.453 回答