0

I'm trying to implement a JavaScript script into a Drupal 7 module and for some reason I keep getting this error here: "Parse error: syntax error, unexpected T_ECHO, expecting ')'"

I've been attempting this for a few hours now, but I'm just at a loss. Any help would be so greatly appreciated.

My code block indented to the far right. The other code is part of a module in Drupal 7 for your reference.

$node->content['actions'] = array(
      '#theme' => 'links',
      '#prefix' => '<div id="match-actions">',
      '#suffix' => '</div>',
      '#links' => _match_actions($node),
                    echo '<script type="text/javascript">'
                        , 'croll();'
                        , 'troll();'
                        , '</script>';
      '#attributes' => array(
        'class' => array('links', 'match-actions'),
      ),
      '#heading' => t('Match actions'),
      '#weight' => -10,
    );

The JavaScript I'm trying to insert (as you can see my attempt in the echo above) is

function class_roll() {
    // Car Classes
    var classes = ["B", "A", "S", "R3", "R2", "R1"],
        classToUse = classes[Math.floor(Math.random() * classes.length)];
    document.getElementById("croll").innerHTML = classToUse ;
}

function track_roll() {

    var tracks = ["Clear Springs Circuit", "Festival North Circuit", "Beaumont Circuit", "Finley Dam Circuit", "Gladstone Circuit", "Clifton Valley Trail", "Gladstone Trail", "Red Rock Descent", "Red Rock Hill Climb"],
        trackToUse = tracks[Math.floor(Math.random() * tracks.length)];
    document.getElementById("troll").innerHTML = trackToUse ;
}

What exactly am I doing wrong? I've been searching around Stack and all over the net which has enabled me to try different syntaxes, but I just can't get it to work. I'm not an expert in JS and PHP, but I'm trying to learn :). Again, any help is REALLY appreciated.

P.S. - In HTML terms, what I'm trying to do is this:

<p id="croll">Some text here</p>
<p id="troll">Some text here</p>
    <button onclick="class_roll(); track_roll();">Class Roll</button>

but it would be great if instead of doing an onclick type of PHP action, it would be an onload type of event, but it would only onload for the first time and stick there and remain static.

3 Answers 3

2

You can't put an echo inside of an array.

You should just be able to do:

$links = _match_actions($node);
$links[] = '<script type="text/javascript"> croll(); troll(); </script>'

$node->content['actions'] = array(
      '#theme' => 'links',
      '#prefix' => '<div id="match-actions">',
      '#suffix' => '</div>',
      '#links' => $links,
      '#attributes' => array(
        'class' => array('links', 'match-actions'),
      ),
      '#heading' => t('Match actions'),
      '#weight' => -10,
    );
Sign up to request clarification or add additional context in comments.

1 Comment

The new $links syntax allowed my site to work again with the parse error mentioned in my OP. Thank you so much! Is there also a way to <p id="croll">Some text here</p> <p id="troll">Some text here</p> <button onclick="class_roll(); track_roll();">Class Roll</button> but in the PHP I listed? Thank you again for your help!
0

As HorusKol said you cannot directly call a JavaScript function inside your module. The reason being modules are written in PHP, and one cannot call functions of other programming language into one.

If you wish to insert JavaScript code, you should use the function drupal_add_js() to do so.

So, you could replace your echo with the following:

echo drupal_add_js('croll();troll();','inline');

1 Comment

For some reason this is breaking the site and is giving me the same parse error mentioned in my OP: Parse error: syntax error, unexpected T_ECHO, expecting ')' Should I maybe add it as a #body (if there is such a thing) or a #heading if it makes it easier? Is that possible with drupal_add_js? Thank you SO much for your help thus far!
0

The better method, that won't break with things like Drupal AJAX, is to use Drupal behaviors.

(function ($) {
  Drupal.behaviors.myModuleName = {
    attach : function (context, settings) {
       $(".match-actions", context).once('match-actions', function(){
         croll();
         troll();
       })
    }

  }
})(jQuery);

Place that in a js file and load it with drupal_add_js(drupal_get_path('module', '{my module name}') . '{js file name}');

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.