want to show a message on the same (form) page.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nadeem14375
Forum Newbie
Posts: 23
Joined: Sat Oct 30, 2010 2:11 am

want to show a message on the same (form) page.

Post by nadeem14375 »

Dear all,

I have signup form, on clicking the submit button, the control goes to "process.php". where i check the form's required fields are filled or not, if not filled it come back to signup.php and show the message. "Please fill all the required fields".

1. Is this right way to do it?
2. I want to show the message bellow the submit button, but couldn't succeed.

Regards:
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: want to show a message on the same (form) page.

Post by social_experiment »

The form can be called on the same page and the code you want to run on failure (or success) below that

Code: Select all

<?php
 // leave the action attribute blank
 <form action="" method="post" >
 </form>
 // below the form goes the code that sends the email
 // pseudo code
 if ($sentMail)
 {
   echo 'Sent';
 }
 else 
 {
  echo 'Not sent';
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: want to show a message on the same (form) page.

Post by califdon »

nadeem14375 wrote:I have signup form, on clicking the submit button, the control goes to "process.php". where i check the form's required fields are filled or not, if not filled it come back to signup.php and show the message. "Please fill all the required fields".

1. Is this right way to do it?
2. I want to show the message bellow the submit button, but couldn't succeed.
social_experiment explained how to do it in the same script, but I just wanted to point out that unless you need to validate data with a database on the server, you can (and generally should) use Javascript to perform simple validation in the browser. If all you want to do is check that required fields are not blank, this can be done in Javascript faster and without extra processing at the server. It is possible that some browsers will have Javascript disabled, but that's probably only a tiny percent of browsers, since an overwhelming number of websites depend on Javascript and very few people disable it in their browsers, I believe.
Idri
Forum Newbie
Posts: 19
Joined: Sun May 29, 2011 9:21 am

Re: want to show a message on the same (form) page.

Post by Idri »

califdon wrote: social_experiment explained how to do it in the same script, but I just wanted to point out that unless you need to validate data with a database on the server, you can (and generally should) use Javascript to perform simple validation in the browser. If all you want to do is check that required fields are not blank, this can be done in Javascript faster and without extra processing at the server. It is possible that some browsers will have Javascript disabled, but that's probably only a tiny percent of browsers, since an overwhelming number of websites depend on Javascript and very few people disable it in their browsers, I believe.

I'll just drop another 2 cents in here because I felt they needed to be there.

Though javascript can be used for simple validation of input fields and whatnots, you should not neglect to check these values in your processing logic. Addons/Tools such as Firebug allow users to alter HTMl on pages, though this is just clientside.

Take for example this simple code.

Code: Select all

<html>

	<head>
		<script>
			function keypress(){
				alert('Test');
			}
		</script>
	</head>

	<body>
		<input type="text" onkeypress="keypress();"/>
	</body>
</html>
Using firebug, one can alter the onkeypress event to not call any javascript functions. Now consider the keypress() function being your validation logic, if I´d just remove the function call I wouldn´t have to be worried about what I put into the field seeing as there´s no longer a check. This is why you should not rely on client-side checks alone. Always make sure that you check submitted values, regardless of whether or not you´ve checked them upon input.
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: want to show a message on the same (form) page.

Post by phazorRise »

nadeem14375 wrote:Dear all,

I have signup form, on clicking the submit button, the control goes to "process.php". where i check the form's required fields are filled or not, if not filled it come back to signup.php and show the message. "Pleasefill all the required fields".

1. Is this right way to do it?
2. I want to show the message bellow the submit button, but couldn't succeed.

Regards:
You can validate form either on client with javascript , rich form validation plugins are available in jQuery .

Or it's better to do it on server.

You can use php-form-validator-class .

You can customize it to suit your needs. Or you can go through above class and make your own.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: want to show a message on the same (form) page.

Post by social_experiment »

phazorRise wrote:Or it's better to do it on server.
It is probably better to do it on the server aswell because javascript can be disabled on the user's browser rendering all validations using javascript useless.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply