0

I would like to call a JavaScript function from my php page, which looks like this:

xyz.php

for ($i = 0; $i < count($temp2a); $i++) {
 $URL = "xyz.php?eingabe=".$temp2a[$i]."&eingabe2=".$datei;
 echo '<p><a href="#" onClick="test("'.$URL.'")">link</a></p>';
}
<script src="java.js"></script>

java.js

function test(para){
 alert(para);
 alert("Para"); 
 }

Calling the function without a variable works. test() outputs two alerts:

Undefined
Para

Whenever I try to pass a variable, the function does not get called at all. No alert or anything else.

asked Mar 2, 2015 at 11:37
2
  • because you are creating an attribute like onclick="test("somevalue")" as you can see the attribute value is not properly enclosed Commented Mar 2, 2015 at 11:39
  • Then how would it be properly enclosed? Commented Mar 2, 2015 at 11:40

2 Answers 2

1

You can do something like

echo '<p><a href="'.$temp2a[$i].'" onClick="test(\''.$URL.'\')">link</a></p>';
answered Mar 2, 2015 at 11:41
Sign up to request clarification or add additional context in comments.

Comments

0

Blockquote Replace your code, It seems mistake of single quotes balance.

for ($i = 0; $i < count($temp2a); $i++) {
 $URL = "xyz.php?eingabe=".$temp2a[$i]."&eingabe2=".$datei;
 echo '<p><a href="'.$temp2a[$i].'" onClick="test(\"'.$URL.'\")">link</a> 
 </p>';
}
answered Mar 2, 2015 at 11:40

1 Comment

Using that code I get an error: Parse error: syntax error, unexpected '\' (T_NS_SEPARATOR), expecting ',' or ';' in C:\xampp\htdocs\xyz.php on line 3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.