开发者

Format PHP form $result = [msg]

How can I add html elements to this:

$return['msg'] = 'Thank you: ' . $_POST['name'] . ' email address: ' . $_POS开发者_开发知识库T['email']  . 'Your Message: ' . $_POST['message'] ;

to make it look nice. I've tried just putting like a <br/> in that broke it, then I tried putting a '<br/>' but that doesnt work either - as the formatting is so that it can display nicely on successful form submission - I'm getting the results just not the formatting.

The form is being submitted by AJAX and the results displayed by json_encode.


Use the br element when you want a line break:

$return['msg'] = 'Thank you: ' . $_POST['name'] . '<br />email address: ' . $_POST['email'] . '<br />Your Message: ' . $_POST['message'] ;

Or you could put them into paragraphs:

$return['msg'] = '<p>Thank you: ' . $_POST['name'] . '</p>';
$return['msg'] .= '<p>email address: ' . $_POST['email'] . '</p>';
$return['msg'] .= '<p>Your Message: ' . $_POST['message'] . '</p>';


$return['msg'] = "Thank you: {$_POST['name']}<br />email address: {$_POST['email']}<br />Your Message: {$_POST['message']}";

Note the use of {} around the variables. They let you embed "complex" variables within a double-quoted string (though they're not strictly necessary in this case), and save you having to do the repeated string concatenation.


Keep the html formatting inside the single quotes. For example...

$return['msg'] = 'Thank you: ' . $_POST['name'] .   '<br />email address: ' . $_POST['email']  . '<br />Your Message:<br />' . $_POST['message'] ;


In php :

Constant values (static words, html...) have to have single ' or double " quotes around them. Variables on the other hand , while in a string of other variables or constants, have to have a . to seperate them.

So you could have something like this :

$message = '<div id="message">'; 
$message .= '<h2>Thank you: ' . $_POST['name'] . '</h2>'; 
$message .= 'Email address: ' . $_POST['email'] . "<br />"; //this br is the same
$message .= 'Your Message: ' . $_POST['message'] . '<br />'; //as this br.
$message .= '</div>'; 

$return['msg'] = $message;

Note : .= concatenates the new string to old one. Quotes surounded by the same type of quotes have to be escaped " <div id=\"escaped\"> " with a backslash.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜