Home > database >  How to get html data-attribute string from a php variable
How to get html data-attribute string from a php variable

Time:02-23

I am constructing an html form in php with javascript and css, the following line of code works fine and displays a textarea with the required prefix:

$form .= '<textarea data-prefix="Message from: " ></textarea>';

However I want to include a php variable i.e

$form .= '<textarea data-prefix="Message from: $foo" ></textarea>';

This show the textarea with the prefix 'Message from $foo'. How do I show it with the value of $foo combined into the data-prefix i.e 'Message from Foo'?

Any advice would be appreciated, thanks.

CodePudding user response:

Either

$form .= "<textarea class='text' data-prefix='Message from: " . $foo. "' ></textarea>";

or

$form .= "<textarea class='text' data-prefix='Message from: {$foo}' ></textarea>";

CodePudding user response:

Try to concat PHP variable with your string

$form .= '<textarea  data-prefix="Message from: '.$foo.'" ></textarea>';
  • Related