Help with Contact Form message echo placement

My Contact Form works successfully and echos the “Sent Successfully” message after submitting.
How can I put the echoed message in a particular spot on the page, instead of it appearing at the very top of the page? For example, I’d like to have it appear just above the Form table. I Googled around, but couldn’t find the answer.

Here’s the php code:

			mail($your_email, $subject, $message, $from);

		    $result = "Your Message Has Been Sent. Thank You";

			echo "$result";
			}
			}

Any help will be appreciated.

How do you call that php code?

I can’t say from looking at that small amount of code.

But output is where the echo is.

So you could put the echo into the HTML wherever you want it.

What I prefer to do is instead of using echo in a function is to have the function return the value(s) and then echo the functions return value(s) in the HTML. I find it less trouble that way to debug and maintain.

1 Like

Here more code:

if($_POST['submited'] == "1"){
			$your_email = "support@....com";  //This is your email address -
			$from = "From: contact-form@....com". "\r\n"; //This will show as the email sender. -
		    $user_email = $_POST['email'];
			$user_name = $_POST['name'];
			$subject = $_POST['subject'];
			$email_body = $_POST['comments'];

			 if($user_email == "" or $user_name == "" or $subject == "" or $email_body == ""){
			 $error = "Please Complete All Required* Fields";
			}else{

			$message = "ContactUs : \n \nUsers Email : $user_email \nUsers Name : $user_name  \n\n\n Users Message : \n\n$email_body";


		 //SEND THE EMAIL -

			mail($your_email, $subject, $message, $from);

		    $result = "Your Message Has Been Sent. Thank You";

			echo "$result";
			}
			}

And

			form action="../index.php" method="post" name="contact_us" onSubmit="return capCheck(this);">
					<tr>
					<td>
					<input id="name" name="name" value="NAME" onfocus="if (this.value=='NAME') {this.value=''; this.style.color='#000000';}" onclick="clickclear(this, 'Enter Name')" onblur="clickrecall(this,'')" required/>
					</td>
					<td>
					<input id="name" name="email" value="EMAIL" onfocus="if (this.value=='EMAIL') {this.value=''; this.style.color='#000000';}" onclick="clickclear(this, 'Enter Email')" onblur="clickrecall(this,'')" required/>
					</td>
					<td>
					<input id="name" name="subject" value="SUBJECT" onfocus="if (this.value=='SUBJECT') {this.value=''; this.style.color='#000000';}" onclick="clickclear(this, '')" onblur="clickrecall(this,'')" required/>
					</td>
					</tr>
					<tr>
					<td colspan="3"><input id="textbox1" name="comments" value="COMMENTS" onfocus="if (this.value=='COMMENTS') {this.value=''; this.style.color='#000000';}" onclick="clickclear(this, 'Enter Comments')" onblur="clickrecall(this,'Enter Comments1')" required/>
					</td>
					</tr>
					<tr>
					<td>
			      	<input id="captext" type="text" name="captext" style="maxlength="6" value="" /></td>
					<td><img src="../includes/captcha.php" border="0" id="verificiation_image" /></a><center><a onclick="refresh_security_image(); return false;" style="cursor:pointer; <u>Refresh Image</u></a><center>
			      	</td>
					<td><input type="hidden" name="submited" value="1" /><input class="my-input" type="submit" value="SEND MESSAGE">
					</td>
					</tr>
		</form>

You can do it by this way:-

Firstly Set the $result is equal to empty or null

$result=“”;

Then Use any html tag to print the message. Like I am using here the paragraph tag

<?php echo $result; ?>

// Your Html Template Code goes here

By this way your paragraph will be empty by default, but when mail sended, it will show the desired message

1 Like

Thanks for your reply.

If I set result to empty, doesn’t that mean there’s no message to display?

Presuming you have the php in with the html, as the start, that’s why the message is echoed at the top.
Just move the echo to where you want it in the html.
To me it would make sense to change the $error variable to $result, that way the echoed result would be the result of the form either way.

$result = "Please Complete All Required* Fields";

The result get echoed wherever you want it in the html.
In this example, the end.

</tr>
</table> <!--Closing table tag added-->
</form>
<p><?php echo $result ; ?></p>

You should be testing whether or not the mail() was successful and setting result to Message sent or Message not sent depending on the result. See https://secure.php.net/manual/en/function.mail.php

1 Like

Thanks for the replies, but still no ‘message’ success.

Regarding “put the echo into the HTML wherever you want it”, I added code like so:

        <table class="table10">
        	**<p><?php echo $result ; ?></p>**
        <form action="../index.php" method="post" name="contact_us" onSubmit="return capCheck(this);">
		<tr>

but, the message does not appear after ‘submitting’.
Any ideas on a remedy will be appreciated.

This may not be the reason why the message is not displaying, but that html above is totally invalid.
Put the <p> before the <table> and if you must format the form with a table, wrap the <form> around the whole <table>.

Do you have error reporting on? What does it say?

My guess is

  • page loads with $message as an empty string
  • form is submitted, $message may be assigned a non-empty string
  • page loads again from “scratch” so $message is again an empty string.

Thanks for all the replies.

As suggested, I’ve now tried this (table within Form) without ‘message’ success:

        <p><?php echo $result ; ?></p>
        <form action="../index.php" method="post" name="contact_us" onSubmit="return capCheck(this);">
        <table class="table10">
		<tr>
		<td>
etc....

I have error reporting on - I see no errors anywhere.

I’m not sure what to do with this information:

“page loads with $message as an empty string
form is submitted, $message may be assigned a non-empty string
page loads again from “scratch” so $message is again an empty string”

Any additional help will be appreciated.

:bulb:
So the form processing is not in the form.
Where is the html for the form result?

The “html for the Form result” may be what I’m missing.
Can you give me an example, pertaining to my posted code, please?

The html for the form result is basically whatever HTML you output from the mail-sending code (in your case I think it’s "../index.php" because that’s your form destination. When your user presses the submit button, the form data is sent to that PHP code, and what appears on the page from that point on is whatever that php script outputs. So are you saying that when the mail is sent, you just get a blank screen with your success message on it? Or do you get something else? If the latter, then that html code is where your message should be added.

Obviously another way is to use Ajax to process the form, so that the display remains in place. That would allow you to do something like replace the div containing the form with a “Please wait, sending…” message, and then replace it again on success with your “Message sent” message.

I earlier assumed that the php processing code and the html for the form, were both within the same document.
But seeing your form action:-

action="../index.php"

This s evidently not the case, so the result should not be echoed in the form page. I should be in whatever html document you are displaying after from submission, which you have yet to show us.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.