-1

I know it`s possible to call javascript functions in php and it has worked but when i try to call checkSecondValue, nothing happens. 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.

<!--<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>"; */
}

?>
9
  • Where is checkSecondValue() defined? Commented Mar 4, 2011 at 18:34
  • 2
    It's not possible to call JS functions in PHP. Learn what you are actually trying to do before learning how to do it. Commented Mar 4, 2011 at 18:35
  • in a seperate js file called validation.js Commented Mar 4, 2011 at 18:35
  • 3
    Correction, it is possible to tell php to write a javascript function call, but it is impossible to call a javascript function while php is executing. However, since we don't see the checkSecondValue signature, we cannot tell you what is wrong. Commented Mar 4, 2011 at 18:35
  • can you show us the definition of checkSecondValue()? If you have it working for other functions I'm tempted to think there's something wrong with that function rather than the way you're calling it. Commented Mar 4, 2011 at 18:36

2 Answers 2

3

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.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

My bad, the onChange event works, its the bottom part that doesnt work
What bottom part? If you mean the stuff you've commented out at the end of the code, like I said, you cannot call client-side javascript functions from PHP. And like Mellanmokb said in the other answer, don't do actual calls to functions until the entire page has loaded.
0

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:

<body onload="checkSecondValue();">

The function call is going to execute on the client-side, after the entire page has been processed by PHP. You can't communicate back-and-forth between JavaScript and PHP during the process of the page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.