javascript - 如何从 php 调用 javascript 函数

标签 javascript php jquery mysql ajax

我正在尝试通过 tabela.php 从 mysql 数据库读取选定的选项(月份)(然后 php 仅选择用户及其所选月份的时间)。此时,当我选择一个选项时,我会得到结果,但它显示在新站点 tabela.php 中。我如何制作 ajax 或脚本来在我的 HTML 网站上显示结果。

例如:html 上的表首先是空的,然后用户选择 Januar,并且在该选择表下方应自动填充正确的结果。 HTML:

<form action="tabela.php" method="post">
Mesec: <select name="meseci", onchange="this.form.submit()">
<option value="o"> </option>
<option value="1">Januar</option>
<option value="2">Februar</option>
</select><br><BR>
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false });
setInterval(function() {
$('#results').load('tabela.php');
}, 3000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
});

php:

$conn = new mysqli($servername, $username, $password, $dbname);
$x = (isset($_POST['meseci']) ? $_POST['meseci'] : null);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
$result = mysqli_query($conn, "SELECT ime, stevilo_ur FROM smart_ure WHERE mesec = '$x ' ");
echo "<table border='1'>
<tr>
<th>ime</th> 
<th>stevilo_ur</th>
</tr>";
while ( $db_v = mysqli_fetch_assoc($result) ) {

echo "<tr>";
echo "<td>" . $db_v['ime'] ."</td>";
echo "<td>" . $db_v['stevilo_ur'] ."</td>";
echo"</tr>";
}
echo "</table>";

mysqli_close($conn);

if (isset ($_GET['update']))
{
echo $tbl;
die ();
}

编辑:

<form method="post" id="test_form">
Mesec: <select id="meseci">
<option value="o"> </option>
<option value="1">Januar</option>
<option value="2">Februar</option>
</select><br><BR>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajaxSetup({ cache: false }); 
$(document).on('change','select[#meseci]',function(){
$post('tabela.php',$('#test_form').serialize()).done(function(results){
$('#results').html(results);
});
});
></script>
<div id="results"</div>

最佳答案

假设您当前页面中有 div 且带有 id=result。现在,在脚本文件中,您可以在 select 标记上使用 onChange() 来使用 ajax 获取数据。 JS脚本:

$(document).on('change','select[name="meseci"]',function(){
    $.post('tabela.php',$('#test_form').serialize()).done(function(result){
        $('#result').html(result);
    });
});

将 id 添加到您的表单中,在上面的脚本中我使用了 id=test_form

<form action="tabela.php" method="post" id="test_form">

更新您的表单标记:

<form method="post" id="test_form">

此外,从您的选择标记中删除 onChange

<select name="meseci">

关于javascript - 如何从 php 调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38939968/

相关文章:

javascript - 在 contenteditable div 中获取子 div 的 id

php - 我可以使用特定参数定位 CSS 样式吗?

javascript - 如何在没有 AJAX 的情况下将 JSON 数组发送到服务器

javascript - 如何在不使用 eval 的情况下调用不存在的(非成员、非全局)方法?

javascript - 修复了 iFrame 内的 NavBar

javascript - jquery 添加/删除可排序的 div 和更新计数

php - 在 WooCommerce 存档页面中的产品标题下方添加自定义字段值

PHP:测试多维数组中是否存在单元格

javascript - 使用 javascript/jQuery 查找已点击的图像

jquery - 将 jQuery 每个函数转换为 Prototype.js