robins robins - 1 year ago 64
PHP Question

How to pass string parameter through onclick

I'm generating a table through ajax function.But i need to add onclick function.Inside this function passing two parameters one is number and other is name.


$table.=' <td><input type="checkbox" value="" id="sp_'.$app_id.'" onclick="SpDetails('.$app_id.','.$app_details->app_name.');" /></td>'

echo $table;


function SpDetails(val,name)


But i didn't get the name when i alert.

Anyone please help me

Answer Source
  • You have extra " here id="sp_'.$app_id.'" ".
  • Pass the second parameter as string so you miss double quotes.
  • Internal double quotes should be escaped like this "foo \" foo".
  • You can pass variable in strings with double quotes "$variable".

so your code should be like this

   $table.="  <td><input type='checkbox'  value='' id='sp_$a' onclick='SpDetails($a,\"$b\");' /></td>";

     echo $table;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download