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.
2 Answers 2
You can do something like
echo '<p><a href="'.$temp2a[$i].'" onClick="test(\''.$URL.'\')">link</a></p>';
answered Mar 2, 2015 at 11:41
Arun P Johny
389k68 gold badges532 silver badges532 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Mihir Bhatt
3,1652 gold badges39 silver badges43 bronze badges
1 Comment
Fynn
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
Explore related questions
See similar questions with these tags.
default
onclick="test("somevalue")"as you can see the attribute value is not properly enclosed