PHP String returns with HTML
I want to return HTML string as follows from my controller.
$returnValue = "<a onclick='demosuccess(".
chunk_split( base64_encode( $details['clientid'] ) ).
",".chunk_split( base64_encode( $details['email'] ) ).
",1)' >$this->lang->line('link_sendactivation')</a>";
But,it getting error.I tried it by different combin开发者_StackOverflow社区ations of single and double quotes .Please help me to call demosuccess function using above html string....Thanks
$client_id = chunk_split(base64_encode($details['clientid']));
$email = chunk_split(base64_encode($details['email']));
$lang_line = $this->lang->line('link_sendactivation');
$returnValue = "<a onclick='demosuccess($client_id, \"$email\", 1)'>$lang_line</a>";
$returnValue = "<a onclick=\"javascript:demosuccess(".chunk_split(base64_encode($details['clientid'])).",".chunk_split(base64_encode($details['email'])).",1);\">".$this->lang->line('link_sendactivation')."</a>";
$returnValue = "<a onclick='demosuccess(".chunk_split(base64_encode($details['clientid'])).",".chunk_split(base64_encode($details['email'])).",1)' >".$this->lang->line('link_sendactivation')."</a>";
Try breaking things up a bit,
$id = chunk_split(base64_encode($details['clientid']));
$email = chunk_split(base64_encode($details['email']));
$thirdthing = $this->lang->line('link_sendactivation');
$returnValue = '<a onclick="demosuccess(\''.$id.'\',\''.$email.'\',\'1\')" >'.$thirdthing.'</a>';
The problem here is that you cannot use a a method call like your $this->lang->line('link_sendactivation')
inside a string declaration.
You either need to get the return value of that method in advance and store it in a temporary variable:
$tmp = $this->lang->line('link_sendactivation');
$returnValue = "<a onclick='demosuccess(".chunk_split(base64_encode($details['clientid'])).",".chunk_split(base64_encode($details['email'])).",1)' >$tmp</a>";
Or you concatenate that return value with the other parts (just like you did it with chunk_split
):
$returnValue = "<a onclick='demosuccess(".chunk_split(base64_encode($details['clientid'])).",".chunk_split(base64_encode($details['email'])).",1)' >".$this->lang->line('link_sendactivation')."</a>";
Or you use sprintf
like Gordon suggested:
$returnValue = sprintf("<a onclick='demosuccess(%s,%s,1)' >%s</a>",
chunk_split(base64_encode($details['clientid'])),
chunk_split(base64_encode($details['email'])),
$this->lang->line('link_sendactivation'));
Futhermore you probably forgot to quote the chunked Base-64 values in JavaScript. You can use json_encode
to do that:
$returnValue = sprintf("<a onclick='demosuccess(%s,%s,1)' >%s</a>",
json_encode(chunk_split(base64_encode($details['clientid']))),
json_encode(chunk_split(base64_encode($details['email']))),
$this->lang->line('link_sendactivation'));
精彩评论