sirin_ibin
(Sirinibin2006)
June 10, 2012, 12:02pm
1
How to pass php variable value to a javascript onclick function as an argument?
here is my code:
echo ‘<li>’.$mainGroup->name.’<input type=“button” value="-remove" onclick=“removeMainGroup(’.$g->id.’);” ></li>’;
its not working.
anyone have a soluton for this?
-Sirin
What does it means that "its not working"? What happen? Where is javascript. What tell you firebug?
binkabir
(Binkabir)
June 10, 2012, 1:22pm
3
can you try this
echo '<li>.$mainGroup->name.<'input type="button" value="remove" id=".$g->id." ></li>
//in you jquery script
$(document).ready(function(){
$('li button').click(function(){
var remove = $(this).attr('id');
//pass to the ajax
$.ajax({
url:'xxx',
});
})}):
note that i have not tested it
cbull80
(Cbull80)
July 4, 2012, 1:08pm
4
How to pass php variable value to a javascript onclick function as an argument?
here is my code:
echo ‘<li>’.$mainGroup->name.’<input type=“button” value="-remove" onclick=“removeMainGroup(’.$g->id.’);” ></li>’;
its not working.
anyone have a soluton for this?
-Sirin
I suspect your problem might be missing “” and as a result removeMainGroup isn’t sent the contents of $g->id but rather an undefined javascript variable with that name
try:
echo '<li>' . $mainGroup->name . '<input type="button" value="-remove" onclick="removeMainGroup("' . $g->id . '");" ></li>';
Suralc
(Wegvonhier)
July 16, 2012, 9:11pm
5
I’d suggest using HEREDOC or simliar styles to do this, as it won’t break any " or ‘’(no need to escape static content) in HTML/Javascript(but requires PHP 5.3++)
echo <<<JAVASCRIPT
<li>{$mainGroup->name}<input type="button" value="-remove" onclick="removeMainGroup("{$g->id}")></li>
JAVASCRIPT;
Drini
(Idrini)
July 18, 2012, 6:35pm
6
never ever use the onClick feature… to start with…
and btw, the solution to your problem is only syntax stuff.
jacmoe
(Jacob Moena)
July 18, 2012, 7:19pm
7
[color="#006400 "]/* Moved to Yii 1.1 General Discussion as it’s not a tip, nor a snippet or even a tutorial… */ [/color]