简体   繁体   English

如何从php调用javascript函数

[英]how to call a javascript function from php

I am trying to read selected option (month) from mysql database via tabela.php (php then selects only users and their hours for he month selected). 我正在尝试通过tabela.php从mysql数据库中读取选定的选项(月份)(然后php仅选择用户及其所选月份的时间)。 At this moment when i select a option i get results but it is shown in new site which is tabela.php. 目前,当我选择一个选项时,我会得到结果,但结果显示在tabela.php新站点中。 How can i make ajax or script to show me results on my HTML site. 如何制作Ajax或脚本以在HTML网站上显示结果。

For example: table on html is first empty, user then selects Januar and below that selection table should autofill with correct results. 例如:html上的表首先为空,然后用户选择Januar,然后在该选择表下应自动填充正确的结果。 HTML: 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: 的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 ();
}

EDITED: 编辑:

<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>

Consider you have div in your current page with id=result . 考虑您当前页面中的divid=result Now, in your script file you can use onChange() on the select tag to get the data using ajax. 现在,在脚本文件中,可以在select标记上使用onChange()来使用ajax获取数据。 JS script: JS脚本:

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

Add id to your form, in above script I have used id=test_form . 将id添加到您的表单中,在上面的脚本中,我使用过id=test_form

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

Update your form tag: 更新您的表单标签:

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

Also, remove onChange from your select tag. 另外,从您的选择标记中删除onChange

<select name="meseci">

With the help of @jaysingkar we got it to work. 在@jaysingkar的帮助下,我们开始工作了。 Below is the answer: 以下是答案:

<form method="post" id="test_form">
Mesec: <select id="meseci" name="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','#meseci',function(){ 
$.post('tabela.php',$('#test_form').serialize()).done(function(result){ 
$('#result').html(result); 
});
}); 
</script>
<div id="result"</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM