Skip to content
Advertisement

How do I make this Javascript function run?

I’m trying to make an image captcha from scratch for a small project, but in the code below, I can’t seem to manage to run the upRAns() function, I can’t find the proper syntax anywhere. Any help?

for($i = 0; $i < 9; $i++){
  $j = rand(1, 4);
  echo '<img src="images/' . $j . '.png" height = 150 width = 150 class = captcha onclick = "captchaClick(this)" data-target=' . $i . '/>';
  echo '<input type=checkbox class=hidden name=' . $i . '/>';
  if(($i+1)%3 == 0){
    echo '<br>';
  }
  if($j == $ans){
    echo '<script src = "Captcha.js">upRAns(' . $i . ')</script>'; //this line right here
  }
}

Advertisement

Answer

A <script tag cannot have both a src attribute (telling it to load the script content from a separate file) and some inline content within the block itself.

You can simply define two separate <script tags instead:

if($j == $ans)
{
  echo '<script src="Captcha.js"></script>';
  echo '<script>upRAns(' . $i . ')</script>';
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement