chitoiu daniel
chitoiu daniel

Reputation: 107

Insert function name with php

I'm trying to do a simple task, insert a second function name under "onmouseover" but probably something is wrong with the syntax.

echo '<div onmouseover="changeText(); <?php if($title==""){echo changeViz();}; ?> ">';

I probably need to escape some quotes and add some, but i can't figure out the correct solution. Thanks

Nothing it's working. Let me give you the complete code...:

echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); <?php if($title[$i]==""){echo changeViz();}; ?>">';

Upvotes: 0

Views: 114

Answers (3)

Bogdan Burym
Bogdan Burym

Reputation: 5512

Try this:

echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); '. ($title[$i]=="")?'changeViz();':'').'">';

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270609

You are nesting <?php ?> inside existing php code, which is a syntax error. Instead, concatenate in the javascript function changeViz() as a quoted string.

This version uses a ternary operator to duplicate the if() statement you had originally.

echo '<div onmouseover="changeText(); ' . ($title == '' ? 'changeViz();' : '') .  '">';

The ternary operation here will concatenate changeViz(); onto the echo string if $title == "", or otherwise just concatenate on an empty string.

Update after seeing full code:

You have the quote escaping correct in the first part.

echo '<div class="frame" onmouseover="changeText(\'' . $text[$i] . '\'); ' . ($title == '' ? 'changeViz();' : '') .  '">';

Upvotes: 4

devsnd
devsnd

Reputation: 7722

You can make your code much more readable if you do not try to do everything in one line:

$onmouseover_action = "changeText(); ";
if($title==""){
    $onmouseover_action .= "changeViz(); ";
}
echo '<div onmouseover="'.$onmouseover_action.'">';

It makes your code easier to maintain, and you have less need to comment it, because it describes itself quite much better.

Upvotes: 1

Related Questions