ok, I have a form that submits data to a database. data that is colected from things like dropdown boxes, textboxes, checkboxes etc.

I want to put some conditions that prevent the user from submitting something that is missing important data.

I found a little tutorial that dose that, but it dosen't quite work because I neet to transfer a value from a thextbox to a function and make a check that it's not empty.

here's an example of what I would like to do ( the code obviously dose not work as is):

<HTML>
<HEAD>
<script language="JavaScript">

function ConfirmForm(N)
{
var cmp = N;

IF ( cmp != "" )
{
 return confirm("are you sure you want to submit this?");
}
ELSE 
{
 return alert("You have not fild all of the necesary fields");
}
}

</script>
</HEAD>
<BODY>

ECHO "<FORM NAME=zzz METHOD=get ACTION=something.php TARGET='lll' OnSubmit='return ConfirmForm(N);'>";

ECHO "Name: <INPUT TYPE='text' NAME=N ID=N/>";

ECHO "<INPUT TYPE = submit NAME = button_submit  VALUE = 'Submit'>";

</FORM>

</BODY>
</HTML>

I don't know how to pass value of the text box from php to the java script.

Normally I would imagine there would need to be a $GET[N] statement. but I don't know where to post it. don't think it works inside the script itself.

Can anybody tell me how to do this?

it much easier just to use php

<?php 
//this is just an example
if(empty($_POST['N'])){
$message = 'Please fill in your Name';
}
if($message){
echo $message;
}
else {
//insert into database
}
?>

when the form is submitted you can send to a php page that checks the form or you can submit the page to itself to check all the fields and check boxes. and then either echo the the error message or process the page and insert all the data to the database

edit: I forgot to add that some people might have javascript turned off in their browser, and if thats the case then the javascript used to check the form won't work.

I can't resubmit the page because I'm already using the submit function to send the data to another page.

the intermediar check.php page seemsi nteresting. butr I ahve no idea how to make the page automaticly redirect the user to either the submit page or back to the data entry page.

any ideas how to automaticly redirect users without them touching anything?

I know there's something like this used for a back to previous page link:

onClick='history.go(-1);return true;'

and that would be perfect for sending the user back to the previous page after stating what he needs to do to continue. but that code requires that the viewer push a button. wich I don't have ( too bad I can't link the ok button from the alert message that I would use to point out the errores.

and then there's the problem with redirecting him to the submit.php page in case all the info is corect.

how do you automaticly redirect to certain pages?

Your code is a complete mess. You're trying to echo static html and it doesn't have any <?php delimiters. The delimiters tell the server which parts of the code to parse. Without them, all of your code gets delivered to the browser without being parsed in plain text.
You are also mixing html and xhtml elements, no doctype...
Here's a cleaner version of your page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">

function ConfirmForm()
{
var cmp = document.zzz.N.value;

	if (cmp != "")
	{
	 return confirm("are you sure you want to submit this?");
	}
	else
	{
	 return alert("You have not filled all of the necesary fields");
	}
}

</script>
<title>Form</title>
</head>
<body>
<form name="zzz" method="get" action="something.php"  onsubmit='return ConfirmForm();'>

Name: <input type='text' name="N" id="N" />
<input type="submit" name="submit" value="Submit" />

</form>

</body>
</html>

Your javascript validation will work fine to save the user time. However, it's not going to be secure. You need to do server side validation. At the top of something.php, do some basic checks and redirect back to the referring page if any fields aren't complete:

<?php
if(empty($_GET['N'])){
header('Location:form.php');
exit;
}
?>
<html>
<head>
<html>
 <body>
 
 <?php
 
 $display_form = true;
 
  if($_POST['submit']=="Submit")
  {
	  if(trim($_POST['my_text'])=="")
	  {
		  $error = "Please fill text box";
	  }
	  else
	  {
		   echo ' <form name="my_form" id="my_form" method="POST" action="submit.php">  
		  
				 <input type="hidden" name="my_text" value="'.$_POST['my_text'].'">
		  
		               </form>
		  
				<script language="JavaScript">
					 function form_submit()
					 {
						var my_frm = document.getElementById("my_form");						
						my_frm.submit();
					 }
					window.onload=form_submit;
					 
				</script>';
					 
				 $display_form = false;
	  }
		  
  }//EO   if($_POST['submit']=="Submit")
  
  echo "<br><font color='#FF0000'>".$error."</font><br>";
  
  if($display_form == true)
  {
  ?>
  
  
  <form name="same_page" method="POST">
     <input type="text" name="my_text"  value="<?php echo $_POST['my_text']; ?>">
     <input type="submit" name="submit" value="Submit">
  </form>

<?php
  }
  ?>
  
 </body>
</html>

Note : This an example of SERVER side validation, you can add CLIENT side validation also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.