Disha Goyal
Disha Goyal

Reputation: 624

How to write this string in a proper way

I want to store a javascript onclick function code into a string first and then i need to pass it in a radio button.

My sting variable is:

$jsFunc = "onclick=\"javascript:showHide(this.value, \'$arrFlipEntities[$field_id]\');\"";

I want to use this variable like dis:

$strCode .= "<input {$jsFunc} type=\"radio\" name=\"{$field_name}\" id=\"{$option}\" value=\"{$option}\" checked=\"checked\"/><label for=\"{$option}\">{$option}</label>";

But it is giving this output, when i viewed the source code:

<input onclick="javascript:showHide(this.value, \'bas_link\');" type="radio" name="bas_type" id="Yes" value="Yes" checked="checked">

Upvotes: 1

Views: 65

Answers (4)

Jeff Wooden
Jeff Wooden

Reputation: 5479

I prefer the single quote method like so:

$jsFunc = 'onClick="javascript:showHide(this.value, '.$arrFlipEntities[$field_id].');"';

$strCode .= '<input '.$jsFunc.' type="radio" name="'.$field_name.'" id="'.$option.'" value="'.$option.'" checked="checked"/><label for="'.$option.'">'.$option.'</label>';

Upvotes: 1

Wenhuang Lin
Wenhuang Lin

Reputation: 214

when ' is in two ",it doesn't need \

$jsFunc = "onclick=\"javascript:showHide(this.value, '$arrFlipEntities[$field_id]')\"";
$strCode .= "<input {$jsFunc} type=\"radio\" name=\"{$field_name}\" id=\"{$option}\" value=\"{$option}\" checked=\"checked\"/><label for=\"{$option}\">{$option}</label>";

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try:

$jsFunc = "onclick=\"javascript:showHide(this.value, '".$arrFlipEntities[$field_id]."');\"";

$strCode .= "<input {$jsFunc} type=\"radio\" name=\"{$field_name}\" id=\"{$option}\" value=\"{$option}\" checked=\"checked\"/><label for=\"{$option}\">{$option}</label>";

Upvotes: 1

siva.k
siva.k

Reputation: 1344

$jsFunc = "onclick=\"javascript:showHide(this.value, \'$arrFlipEntities[$field_id]\');\""; 

Should be:

$jsFunc = "onclick=\"javascript:showHide(this.value, '".$arrFlipEntities[$field_id]."');\""; 

Single and double quotes don't get escaped when they are wrapped by the other. Also you can't access an array like that inside double quotes, only works for variables like $var = "$myVar";

Upvotes: 0

Related Questions