0

I have built an ajax contact form using this tutorial.

How to Create an AJAX Contact Form

My AJAX Contact Form works well - perfectly inline with the tutorial.

But rather than just the plain text

echo "Thank You! Your message has been sent.";

I would like to include a hyperlink in the Thank you message.

echo "<p><a href="#">Thank You! Your message has been sent.</a>";

But the ajax outputs my HTML as plain text.

Pete D
  • 311
  • 3
  • 15
  • 1
    Change : echo "

    Thank You! Your message has been sent."; To echo "

    Thank You! Your message has been sent."; And Can you show Screenshot for your PHP output that send to JS?

    – mohammed alshobaki Mar 29 '21 at 14:40
  • 3
    Try changing `href="#"` to `href='#'`. You need to either use single quotes `'` inside a double quoted string, or escape the the quotes: `href=\"#\"`. If not, PHP will close the string as soon as it finds the same quote you used to open it. – M. Eriksson Mar 29 '21 at 14:41
  • Does this answer your question? [Double quotes within php script echo](https://stackoverflow.com/questions/11036420/double-quotes-within-php-script-echo) – M. Eriksson Mar 29 '21 at 14:44
  • Please **do not** show us screenshots! We're programmers, we can read. Please read about [why you shouldn’t upload images of code or errors when asking a question](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). Better just copy the output as text and update the question to show it to us. – biesior Mar 29 '21 at 14:44
  • 1
    Also, show the code where you are processing and displaying the HTML once the JS receives it, please. If you're writing `$(formMessages).text(response);` as per the tutorial then, it likely needs to be changed to `$(formMessages).html(response);`. If you look in the jQuery documentation for these two functions it will explain the difference between them (hint: one writes data as plain text, the other as HTML! Self-explanatory, really.) – ADyson Mar 29 '21 at 14:46
  • 1
    it could be also `echo "

    Thank You....

    ";` Also, close your paragraph finally with ``
    – biesior Mar 29 '21 at 14:46
  • All helpful, particularly @ADyson. And to remind oneself to look for all instances of $(formMessages).text(response); , always clear your cache and to check paths to staging files if copying from a live site. – Pete D Mar 30 '21 at 09:15

2 Answers2

0

You have to escape the quotes in the string by adding a backslash \ before "

echo "<p><a href=\"#\">Thank You! Your message has been sent.</a></p>";

or using single quotes inside double quotes

echo "<p><a href='#'>Thank You! Your message has been sent.</a></p>";

or even using Heredoc

echo <<<EOL
<p><a href="#">Thank You! Your message has been sent.</a></p>
EOL;
XMehdi01
  • 5,538
  • 2
  • 10
  • 34
0

This has already been stated by Steverst, but I'm going to say it anyway. Just use single quotes. The reason that this is happening is because # is the symbol for a comment in PHP, so anything past it on that same line is going to be commented out if it isn't formatted as text. What you did was you ended the quote right before the # by using a second double quote, and it was interpreted as closing your quote because your double quotes paired up. What you should've done is enclosed the whole thing in single quotes instead of double quotes or you could've enclosed the # in single quotes. Example:

echo "<p><a href='#'>Thank You! Your message has been sent.</a>";