Jump to content

Creating a error message


reggie

Recommended Posts

I want to create a error message box that appears onces the user has clicked submit on a registration form. If the user doesnt fill in a filed to pop up an error message box and then once clicked ok to go back to the form.....I have used the javascript alert code, but i dont know how to open an error box in php and continue with the same form.Can anyone help.....

Link to comment
Share on other sites

Alright...I don't quite understand what your saying. You shouldn't even need php to popup an error box. you could do something like this:

<HTML> <HEAD>  <TITLE>Register</TITLE>  <script>   function doCheck() {	int username = document.forms[0].elements[0].value;	int password = document.forms[0].elements[1].value;	int email = document.forms[0].elements[2].value;	if(username == '' || password == '' || email == '') {	 alert("All fields are required!");	}	else {	 document.forms[0].submit();	}   }  </SCRIPT> </HEAD> <BODY>  <TABLE ALIGN="center" WIDTH="50%">   <TR>	<TD>	 <FORM ACTION="doRegister.php" METHOD="Post">	  Username:	</TD>	<TD>	  <INPUT TYPE="text" name="username">	</TD>   </TR>   <TR>	<TD>	  Password:	</TD>	<TD>	  <INPUT TYPE="text" name="password">	</TD>   </TR>   <TR>	<TD>	  Email:	</TD>	<TD>	  <INPUT TYPE="text" name="email">	</TD>   </TR>   <TR>	<TD colspan=2 align="center">	  <INPUT TYPE="button" VALUE="Register" onClick="doCheck()">	</TD>   </TR>  </TABLE> </BODY></HTML>

I wouldn't recommend using this though. It's not always secure and can be edited and changed and used from a person's desktop. A good thing to do with this is check your referrer on the doRegister.php page.

Link to comment
Share on other sites

The referer doesn't matter if they have javascript disabled to begin with. And you can also forge the referer header. Javascript validation is fine for user convenience, but you need to validate using PHP as well.I like having the form page process itself. So, you have form.php that displays the form, and the action of the form is also set to form.php, so when you hit submit the page reloads. You need to include a way to know if the form has been submitted, and if it has, then validate the information. If the information has errors, then since you are on the same page that has the form, you just go ahead and display the same form again, but include an error box on the top.

<?php$errors = false;if ($_POST['submitted'] == "true"){  //the form was submitted, validate the information  if ($_POST['name'] == "")	$errors = true;  ...}?><html>  <head>  ...  </head>  <body>  ...  <?php   if ($errors)  {	echo "There were errors";  }  ?>  <form action="form.php" method="post">  <input type="hidden" name="submitted" value="true">  ...  </form>  </body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...