简体   繁体   English

在PHP中调用JS函数

[英]Calling a JS function in PHP

I know it`s possible to call javascript functions in php and it has worked but when i try to call checkSecondValue, nothing happens. 我知道有可能在php中调用javascript函数,它已经起作用了,但是当我尝试调用checkSecondValue时,什么也没有发生。 I have included my variations on calling the function. 我已经包含了有关调用该函数的各种信息。

Edit: If i try to call it at the bottom without activating the onChange event in the select, it won`t work. 编辑:如果我尝试在底部调用它而不激活选择中的onChange事件,它将无法正常工作。

<!--<html>
<head>
    <script type="text/javascript" src="validation.js"></script>
    </head>
</html>

<script src="validation.js" type="text/javascript"></script>
-->
<?php
    //retrieve all the bris for the drop down
include '../../inc/database.php';
$res = BbqcDatabase::getInstance()->doQuery('SELECT * FROM T_TOURNOI_BRIS');
$str = "<select name='ddlBrisSelected' id='ddlBrisSelected' onChange='checkSecondValue()'>";

$bris = ($_GET['bris']);

if($bris == 4)
{
    $bris2 = "autre";
}

if($bris == null)
{
    $str .= "<option value='' selected></option>";
}
else
{
    $str .= "<option value=''></option>";
}

$i = 0;
while($data = mysql_fetch_assoc($res))
{
    if($data['F_BRISID'] == $bris)
    {
        $str .= "<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>";
    }
    else 
    {
        $str .= "<option value='" . $data['F_BRISID'] . "'>" . $data['F_BRISTITLE'] . "</option>";
    }
}

if($bris2 == "autre")
{
    $str .= "<option value='autre' selected>Autre</option>";
}
else
{
    $str .= "<option value='autre'>Autre</option>";
}
$str .= "</select>";

echo $str;

if(is_numeric($bris))
{
    /* echo "<SCRIPT language=\"JavaScript\" SRC=\"validation.js\">checkSecondValue();</SCRIPT>"; */
    /* echo "<script> checkSecondValue();</script>"; */
    /* echo "<script type=\"text/javascript\">checkSecondValue();</script>"; */
    /* echo '<script type="text/javascript">', 'checkSecondValue();', '</script>'; */
    /* echo "<SCRIPT LANGUAGE='javascript'>checkSecondValue();</SCRIPT>"; */
}

?>

You can't call client-side javascript functions from PHP, but you can echo code that makes the client call the function on a specific (client-side) event. 您不能从PHP调用客户端javascript函数,但是可以回显使客户端在特定(客户端)事件上调用该函数的代码。

Just look at the generated HTML you get from your PHP, and from there, try to see why checkSecondValue() isn't executed on an onChange event. 只需查看您从PHP中获得的生成的HTML,然后从那里开始,看看为什么在onChange事件上不执行checkSecondValue()。

At the very least, you want your function call to happen only after the page is loaded. 至少,您希望仅在页面加载后才发生函数调用。 This can be done by embedding the function in the body tag during onload : 这可以通过在onload期间将函数嵌入body标记中来完成:

<body onload="checkSecondValue();">

The function call is going to execute on the client-side, after the entire page has been processed by PHP. 在PHP处理了整个页面之后,该函数调用将在客户端执行。 You can't communicate back-and-forth between JavaScript and PHP during the process of the page. 在页面处理期间,您无法在JavaScript和PHP之间来回通信。

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

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