【发布时间】:2017-04-13 04:13:50
【问题描述】:
当用户单击提交按钮时,我能够将 5 个表单中的数据输入 mysql。
现在,我想在用户单击其他按钮时在表格中显示数据。我知道我需要使用 Ajax 和 Php,但是我找不到一个我清楚的例子。
我的部分代码如下。我没有包括用于将表单数据发布到数据库的 javascript 和 php。如果这能帮助你理解,我可以添加它。
input.html
<div class="container">
<form action="vocab_input.php" method="post" id="input_form">
<label>Word:</label>
<input type="text" name='word'>
<label>POS:</label>
<input type="text" name='pos'>
<label>Translation:</label>
<input type="text" name='trans'>
<label>Definition:</label>
<input type="text" name='definition'>
<label>Sentence:</label>
<input type="text" name='sen'>
<input type="submit">
</form>
</div>
<div class="btn-group btn-group-justified" role="group" aria-label="..." style="margin-top: 50px;">
<div class="btn-group" role="group">
<button type="button" class="btn btn-default" id="list">Vocab List</button>
</div>
</div>
<-- I want the table displayed here when user clicks the list button -->
get_input.js
$(function() {
$('#list').on('click', function(e) {
var data = $("#list :input").serialize();
$.ajax({
type: "GET",
url: "get_input.php",
data: data,
});
e.preventDefault();
});
});
get_input.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM user_input";
$result = $conn->query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
foreach($row as $field) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
}
$conn->close();
?>
【问题讨论】:
-
查看一些文档stackoverflow.com/documentation/ajax/1082/…注意有一个
.success函数用来处理返回的数据 -
你在混合 APIS ,注意
mysql_fetch_array,这只是一个错字吗? -
@RiggsFolly 好的。我会查看 .success 函数。 var数据线怎么样。我是从以前发布到数据库的代码中提取的,所以我认为它需要有所不同。
-
@hassan 应该是 mysqli 吗??