0

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();}; ?>">';
0

3 Answers 3

4

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();' : '') .  '">';
Sign up to request clarification or add additional context in comments.

5 Comments

@RyanB I think the quotes are all correct, since the final " closes the onmouseover. In any case, I changed them to single quotes on the PHP side to be clear.
wouldn't the first " after title == end the pair for the mouseover?
@RyanB It's a different string, in a different context. Inside the single quotes, the opening " doesnt need escaping and isn't parsed as an open string as far as PHP is concerned. The pair of double quotes after $title (which I thought I changed to single) are a separate string as far as PHP is concerned as well, and won't close any opened in the previous string.
It might have been while you were updating
@chitoiudaniel Sorry, looks like the ternary statement needs to be enclosed in (). Edited.
1

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.

Comments

0

Try this:

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

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.