Jump to content

Form not validating


daveyclark

Recommended Posts

Hi

 

I have a form that I want to validate the List box so if it is set to the default value (blank) when "send" is clicked, I don't want the form to go to my asp page which sends the email, I want it to show the error message then stop.

 

So far, my form does show the error message, but then carries on and calls the asp page. My php code is below:

 

<HTML>

<HEAD>

<TITLE>New Street Request</TITLE>

    <link rel="stylesheet" type="text/css" href="stylesheet.css" />

<script LANGUAGE="JavaScript">
    <!-- 
    function ValidateForm(form){ 
    ErrorText= ""; 
    if ( form.RequestedBy.selectedIndex == 0 ) { alert ( "Please select your Name from the list." ); return false; } 
    if (ErrorText= "") { form.submit() } 
    } 
    -->
    </script>
    
</HEAD>



<BODY>

<?php

$moacode = $_POST['moacode'];

$con = mssql_connect("server","username","password");
if (!$con)
  {
  die('Could not connect: ' . mssql_error());
  }

mssql_select_db("database", $con);

$result = mssql_query("SELECT moa,name,name2,addr1,addr2,addr3,addr4 FROM MOAS where MOA=$moacode");

echo "<form action='cdontsmailreplymoanew.asp' method='POST'>";

if (!mssql_num_rows($result)) 
{
  echo "Requested By: <SELECT NAME='RequestedBy'> <br/>";
  echo "<OPTION SELECTED VALUE=''>";
  echo "<OPTION VALUE='person1@email.com'>person1";
  echo "<OPTION VALUE='person2@email.com'>person2";
  	  echo "<OPTION VALUE='person3@email.com'>person3";
  echo "</SELECT><br/>";

  echo "Name Line 1: <input type='text' size='50' maxlength='30' name='name' value=''> <br/>";
  echo "Name Line 2: <input type='text' size='50' maxlength='30' name='name2' value=''> <br/>";
  echo "Address Line 1: <input type='text' size='50' maxlength='30' name='addr1' value=''> <br/>";
  echo "Address Line 2: <input type='text' size='50' maxlength='30' name='addr2' value=''> <br/>";
  echo "Address Line 3: <input type='text' size='50' maxlength='30' name='addr3' value=''> <br/>";
  echo "Post Code: <input type='text' size='50' maxlength='7' name='postcode' value=''>";
  echo "<input type='hidden' size='50' name='header' value='new'>";	  	
  echo "<input type='hidden' size='50' name='moa' value='$moacode' Readonly> <br/>";
  
  echo "<input type='submit' value='Send' onSubmit='CheckSubmit()'>";
} else {

while($row = mssql_fetch_array($result))
  {
  echo "Requested By:   <SELECT NAME='RequestedBy'> <br/>";
  echo "<OPTION SELECTED VALUE=''>";	  
  echo "<OPTION VALUE='person1@email.com'>person1";
  echo "<OPTION VALUE='person2@email.com'>person2";
  	  echo "<OPTION VALUE='person3@email.com'>person3";
  echo "</SELECT><br/>";

  echo "MOA Code:      <input type='text' size='50' name='moa' value='".$row['moa']."' ReadOnly> <br/>";
  echo "Name Line 1:    <input type='text' size='50' maxlength='30' name='name' value='".$row['name']."'> <br/>";
  echo "Name Line 2:    <input type='text' size='50' maxlength='30' name='name2' value='".$row['name2']."'> <br/>";
  echo "Address Line 1: <input type='text' size='50' maxlength='30' name='addr1' value='".$row['addr1']."'> <br/>";
  echo "Address Line 2: <input type='text' size='50' maxlength='30' name='addr2' value='".$row['addr2']."'> <br/>";
  echo "Address Line 3: <input type='text' size='50' maxlength='30' name='addr3' value='".$row['addr3']."'> <br/>";
  echo "Post Code:        <input type='text' size='50' maxlength='7' name='postcode' value='".$row['addr4']."'>";
  echo "<input type='hidden' size='50' name='header' value='old'>";	  	

  echo "<input type='submit' value='Send' onClick='ValidateForm(this.form)'>";
  	  	  	  
  }
}
echo "</form>";

mssql_close($con);
?> 
</BODY>
</HTML>

 

If i change the type of the "Send" button to type="button", the message shows and the form does not submit, but the form never submits at all then even if the form gets past the validation, whereas the way it is just now, shows the message, but carries on and calls the asp page.

 

Obviously there is somthing wrong with my validation or the type of the send button, but not sure what the solution is.

 

Any ideas anyone?

 

Cheers in advance.

Link to comment
Share on other sites

Finally solved this!

 

Kept my original code and changed it slightly.

 

removed the second part of the javascript and added an else return true onto first bit so new JS looks like:

 

<script language='JavaScript' type='text/JavaScript'>
<!--
function validate() {
if( document.mailform.RequestedBy.value=='')
	{
	alert('Please choose your name from the dropdown!');
	return false;
	}

else	{
	return true;
		}
}
//-->
</script>

 

 

 

Left the type of send button as submit and form as method=post but took out the onclick of the form and called the JS onsubmit of the form!

 

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.