robins robins - 1 month ago 5
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.

Controller

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

echo $table;


script

function SpDetails(val,name)
{
alert(name)

}


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

Anyone please help me

Answer
  • 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;
Comments