我是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>
我非常感谢一些代码或提示如何做到这一点。
答案 0 :(得分: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>";
}
?>
答案 1 :(得分: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>
尝试这个