I want to display success message after form submission without changing URL

I will be very glad if I can get a tip on how I can display success message after form submission with changing URL in PHP…just the idea to start off

With or without changing URL? I am confused…

I mean without changing URL…I want the success message to display at the top of the form

am a newbie…help pls…

You need to learn first to submit a form using AJAX.

And then on success you write your message.
You can use alert() to display your message.

oh, am new to web development…is there any other way I can do that in PHP? I want user without JavaScript enable to see the success message on the same page the form is submitted

IF php processing is above output (as it should be), it should be easy to set a variable if for example DB was updated etc.

Certainly. You can place the form processing code on the contact page itself, and instead of redirecting to a thanks page on submission, you can just output the thanks message on the contact page itself.

You can use Ajax to do that - http://www.w3schools.com/ajax/, click change content in exemple to see what you need :slight_smile:

Why use a debugging call to display the message?

Alerts look like this in Opera:

and like this in Firefox and Chrome

Note the checkbox for turning off JavaScript for debugging infinite loops or for turning off subsequent alerts for debugging loops with a high number of passes.

@felgall ;
There is nothing I can say regarding JS, you are far better than me. lol
Anyway I use it personally.

Thanks everyone…if I want to place my form processing code on the contact page itself…what will be the value of my action attribute within the form element? and the success message is already output without submitting the form yet…pls help

<form action="" method="post">

The success message should only be defined on successful processing of POST data. Then above the form you could have something like

<?php
if (isset($success_message)){ echo $success_message;}
?>

My PHP code


$message = " ";
$fullName = $_POST["fullName"] ;
$email = $_POST["email"];
$formMessage = $_POST["message"];


$message .= "Full Name: " . $fullName . "\
"; 
$message .= "Email Address: " . $email . "\
"; 
$message .= "Comment: " . $formMessage . "\
";

$subject = "Contact Us";
$myEmail =" name@example.com";

$success = mail($myEmail, $subject, $message);


My HTML markup

<div>if (isset($success)) {

echo "Message successfully sent";
}
else {

echo  "Message Sending Failed, try again";
}   </div>

<form method="post" action="" id="contactForm">
<div>
<label for="fullName">Name: </label>
<input type="text" name="fullName" id="fullName" required="required" />
</div>
				
<div>
<label for="Email">Email address:</label>
<input type="email" name="email" id="email" required="required" placeholder="name@example.com" />
</div>
			
<div>
<label for="message">Idea / Question / Comments:</label>
<textarea name="message" id="message" rows="5" cols="20"></textarea>
</div>
			
<div class="submit">
<input type="submit" name="contactSubmit" id="contactSubmit" value="Send Message" />
</div>
</form>

With this code above I have the success message display already on the page without even submit the form yet…

Please help me out with any error within the HTML and PHP code…I just want the success message to display on the same page without changing URL

<?php
if(isset($_POST['contactSubmit'])){
	$message = " ";
	$fullName = $_POST["fullName"] ;
	$email = $_POST["email"];
	$formMessage = $_POST["message"];
	
	
	$message .= "Full Name: " . $fullName . "\
";
	$message .= "Email Address: " . $email . "\
";
	$message .= "Comment: " . $formMessage . "\
";
	
	$subject = "Contact Us";
	$myEmail =" name@example.com";
	
	if (mail($myEmail, $subject, $message)){
		$success = "Message successfully sent";
	}else{
		$success = "Message Sending Failed, try again";
	}
}
?>
<html>
<body>
<?php
if (isset($success)){ echo "<div>" . $success . "</div>";}
?>
	<form method="post" action="" id="contactForm">
		<div>
			<label for="fullName">Name: </label>
			<input type="text" name="fullName" id="fullName" required="required" />
		</div>
						
		<div>
			<label for="Email">Email address:</label>
			<input type="email" name="email" id="email" required="required" placeholder="name@example.com" />
		</div>
					
		<div>
			<label for="message">Idea / Question / Comments:</label>
			<textarea name="message" id="message" rows="5" cols="20"></textarea>
		</div>
					
		<div class="submit">
			<input type="submit" name="contactSubmit" id="contactSubmit" value="Send Message" />
		</div>
	</form>
</body>
</html>

Here is a simple one page script. http://www.websitecodetutorials.com/code/php/one-webpage-php-contact-form.php

Here is a more difficult multipage script with full validation (php and js) and captcha http://www.websitecodetutorials.com/code/php/how-to-html-form-with-php-js-captcha-validation.php

Probably should add some headers.

<?php
if(isset($_POST['contactSubmit'])){
	$message = " ";
	$fullName = $_POST["fullName"] ;
	$email = $_POST["email"];
	$formMessage = $_POST["message"];
	
	
	$message .= "Full Name: " . $fullName . "\
";
	$message .= "Email Address: " . $email . "\
";
	$message .= "Comment: " . $formMessage . "\
";
	
	$subject = "Contact Us";
	$myEmail =" name@example.com";
	
	$headers = "From: \\"$fullName\\" <$email>\\r\
";
	$headers .= "Reply-To: $email\\r\
";
	$headers .= "X-Sender: $email \\r\
";
	$headers .= "X-Priority: 3 \\r\
";
	$headers .= "X-Mailer: php\\r\
";
	
	if (mail($myEmail, $subject, $message, $headers)){
		$success = "Message successfully sent";
	}else{
		$success = "Message Sending Failed, try again";
	}
}
?>
<html>
<body>
<?php
if (isset($success)){ echo "<div>" . $success . "</div>";}
?>
	<form method="post" action="" id="contactForm">
		<div>
			<label for="fullName">Name: </label>
			<input type="text" name="fullName" id="fullName" required="required" />
		</div>
						
		<div>
			<label for="Email">Email address:</label>
			<input type="email" name="email" id="email" required="required" placeholder="name@example.com" />
		</div>
					
		<div>
			<label for="message">Idea / Question / Comments:</label>
			<textarea name="message" id="message" rows="5" cols="20"></textarea>
		</div>
					
		<div class="submit">
			<input type="submit" name="contactSubmit" id="contactSubmit" value="Send Message" />
		</div>
	</form>
</body>
</html>

@Drummin thanks so much for your time and thinking…everything is working fine now, I can now display the success message on the same page without changing the url…thanks so much…I hope to see you again in future for help cause am new to web development and I believe that there is more you can show me… thanks once again… and everyone too…thanks all.