Format PHP form $result = [msg]

heres one that might be easy – but I cant find an answer to.How can I add html elements to this:$return[‘msg’] = ‘Thank you: ‘ . $_POST[‘name’] . ‘ email address: ‘ . $_POST[‘email’] . ‘Your Message: ‘ . $_POST[‘message’] ;to make it look nice. I’ve tried just putting like a in that broke it, then I tried putting a …

via PHP Website Development » Search Results » ajax:

Format PHP form $result = [msg]

heres one that might be easy – but I cant find an answer to.
How can I add html elements to this:
$return[‘msg’] = ‘Thank you: ‘ . $_POST[‘name’] . ‘ email address: ‘ . $_POST[‘email’] . ‘Your Message: ‘ . $_POST[‘message’] ;to make it look nice. I’ve tried just putting like a
in that broke it, then I tried putting a ‘
‘ 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’] . ‘
email address: ‘ . $_POST[‘email’] . ‘
Your Message: ‘ . $_POST[‘message’] ;Or you could put them into paragraphs:
$return[‘msg’] = ‘

Thank you: ‘ . $_POST[‘name’] . ‘

‘;
$return[‘msg’] .= ‘

email address: ‘ . $_POST[‘email’] . ‘

‘;
$return[‘msg’] .= ‘

Your Message: ‘ . $_POST[‘message’] . ‘

‘;
…………………………………..

$return[‘msg’] = “Thank you: $_POST[‘name’]
email address: $_POST[‘email’]
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’] . ‘
email address: ‘ . $_POST[‘email’] . ‘
Your Message:
‘ . $_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 = ‘

‘;
$message .= ‘

Thank you: ‘ . $_POST[‘name’] . ‘

‘;
$message .= ‘Email address: ‘ . $_POST[‘email’] . “
“; //this br is the same
$message .= ‘Your Message: ‘ . $_POST[‘message’] . ‘
‘; //as this br.
$message .= ‘

‘;

$return[‘msg’] = $message;Note : .= concatenates the new string to old one. Quotes surounded by the same type of quotes have to be escaped ”

” with a backslash.
Hope it helps.

For more info: Format PHP form $result = [msg]

PHP Website Development » Search Results » ajax

Format PHP form $result = [msg]

Share this post:

Related Posts

Leave a Comment