form validation and redirect problem

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

laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

form validation and redirect problem

Post by laticca8 »

i have truly exhausted all resources (to the best of my knowledge) in solving this problem. i read the posting guidelines and didn't understand the tag references, so pls forgive me with this first post.

i have a form that i want to be completed, validated, then directed to another file to process the database insert command. i had already created this form with a validation script that worked perfectly and was hoping to make "minor" adjustments to it. here's the script in part:

Code: Select all

<HTML>
<head>
<script language="JavaScript" type="text/javascript"> 

function Validatemercy() 
{
var method = document.mercy.ContactMethod;
var email = document.mercy.EmailAddress;
var address = document.mercy.PostalAddress;

if (method.selectedIndex < 1)
{
alert("Please provide your preferred method of contact.");
method.focus();
return false;
}
if ((method.selectedIndex==1) && (email.value == ""))
{
window.alert("Please enter your email address.");
email.focus();
return false;
}
[color=darkred]return true;[/color]
}
// -->
</script>

</head>
<form name="mercy2" method="post" action='.$GLOBALS["PHP_SELF"].'onSubmit="return Validatemercy();">
<input type=submit name="sent" value="Submit Form" language="JavaScript" onclick="return validate(this)">
<INPUT TYPE=RESET VALUE="Reset Form">
</FORM>
i have no problem with the validation, but after the form is completed correctly and resubmitted, an error message (page cannot be displayed; HTTP 405 - Resource not allowed IIS) appears.

i tried to insert header("Location: help2.php"); in the return true of my if statement, but i get "page cannot be displayed" as soon as the submit button is clicked.

can anyone help me?

Sami | Please use BBCode when posting source code. See: Posting Code in the Forums
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

try

Code: Select all

header("Location: help2.php"); 
exit;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm going to need to see more code I think.
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post by laticca8 »

Here's the complete code. and i have already tried to use

Code: Select all

header("Location: help2.php"); 
exit;
but, that didn't work.

Code: Select all

<HTML>
<head>
<script language="JavaScript" type="text/javascript"> 
 
 function Validatemercy2() 
  {
  var method = document.mercy2.ContactMethod;
  var email = document.mercy2.EmailAddress;
  var address = document.mercy2.PostalAddress;
  
  if (method.selectedIndex < 1)
  {
  alert("Please provide your preferred method of contact.");
  method.focus();
  return false;
  }
  if ((method.selectedIndex==1) && (email.value == ""))
    {
        window.alert("Please enter your email address.");
        email.focus();
  return false;
  }
  if ((method.selectedIndex==2) && (address.value == ""))
    {
        window.alert("Please enter your mailing address.");
        address.focus();
  return false;
  }
  return true;
  }
  // -->
</script>

</head>
<form name="mercy2" method="post" action='.$GLOBALS["PHP_SELF"].'onSubmit="return Validatemercy2();">
<input type="hidden" name="id" value="NULL">
<div align="left">
	<p class="title01" style="margin-bottom: 8pt"><span style="font-size: 16pt">
	Request for More Information</span></p>
	<table border="0" id="table1">
		<tr>
			<td align="right"><b><font face="Tahoma" style="font-size: 9pt">Name</font></b></td>
			<td><font face="Tahoma"><span style="font-size: 9pt">
<INPUT NAME="FullName" SIZE=50></span></font></td>
		</tr>
		<tr>
			<td align="right"><b><font face="Tahoma" style="font-size: 9pt">
			Preferred Contact Method</font></b></td>
			<td><font face="Tahoma"><span style="font-size: 9pt">
			&nbsp;<SELECT NAME="ContactMethod" size="1">
<option selected>Please select one</option>
<OPTION>Email
<OPTION>Postal Delivery
</SELECT></span></font></td>
		</tr>
		<tr>
			<td align="right"><b><font face="Tahoma" style="font-size: 9pt">
			Email Address* </font></b></td>
			<td><font face="Tahoma"><span style="font-size: 9pt">
<INPUT NAME="EmailAddress" SIZE=25></span></font></td>
		</tr>
		<tr>
			<td align="right" valign="top"><b>
			<font face="Tahoma" style="font-size: 9pt">Postal Address* </font>
			</b></td>
			<td><font face="Tahoma"><span style="font-size: 9pt">
<TEXTAREA NAME="PostalAddress" ROWS=5 COLS=35></TEXTAREA></span></font></td>
		</tr>
	</table>
</div>
<p style="margin-top: 0"><font face="Tahoma" size="1"><b>* These fields are 
required only if chosen as the preferred method of contact.</b></font></p>
<BLOCKQUOTE>
</BLOCKQUOTE>
<P>
<font face="Tahoma" style="font-size: 9pt">How did you hear about us?&nbsp;&nbsp;&nbsp;
</font><font face="Tahoma"><span style="font-size: 9pt">
<SELECT NAME="ReferralMethod" size="1">
<option selected>Please select one</option>
<OPTION>I am a returning customer.
<OPTION>Referral from a friend/relative/associate
<OPTION>Google.com
<OPTION>Other search engine
<OPTION>I saw/clicked on your link on another site.
<OPTION>Other
</SELECT></span></font></P>
<P>
&nbsp;</P>
<P>
<font style="font-size: 10pt; font-weight: 700" face="Tahoma">I 
would like to receive additional information about the following services:</font></P>
<P>
<font face="Tahoma" style="font-size: 9pt">Please select any of the following <u>Business Services</u> that apply:</font></P>
<BLOCKQUOTE>
<P>
<font face="Tahoma"><span style="font-size: 9pt">
<INPUT TYPE=CHECKBOX NAME="Business_Svcs_Startup_Services" value="0">
Startup Services<BR>
<INPUT TYPE=CHECKBOX NAME="Business_Svcs_Credit_Services" value="0">
Credit Services<BR>
<INPUT TYPE=CHECKBOX NAME="Business_Svcs_Tax_Recovery" value="0">
Tax Recovery<BR>
<INPUT TYPE=CHECKBOX NAME="Business_Svcs_Strategic_Plan_Consulting" value="0">
Strategic Plan Consulting<BR>
<INPUT TYPE=CHECKBOX NAME="Business_Svcs_Process_Improvement_Consulting" value="0">
Process Improvement Consulting<BR>
<INPUT TYPE=CHECKBOX NAME="Business_Svcs_Business_Audit" value="0">
Business Audit<BR>
</span></font>
</P>
</BLOCKQUOTE>
<P>
<font face="Tahoma" style="font-size: 9pt">Please select any of the following <u>Church Services</u> that apply:</font></P>
<BLOCKQUOTE>
<P>
<font face="Tahoma"><span style="font-size: 9pt">
<INPUT TYPE=CHECKBOX NAME="Church_Svcs_Charitable_Gifting" value="0">
Charitable Gifting<BR>
<INPUT TYPE=CHECKBOX NAME="Church_Svcs_501c3_Setup" value="0">
501c3 Setup<BR>
</span></font>
</P>
</BLOCKQUOTE>
<P>
<font face="Tahoma" style="font-size: 9pt">Please select any of the following <u>Personal Services</u> that apply:</font></P>
<BLOCKQUOTE>
<P>
<font face="Tahoma"><span style="font-size: 9pt">
<INPUT TYPE=CHECKBOX NAME="Personal_Svcs_Life_Insurance" value="0">
Burial & Life Insurance<BR>
<INPUT TYPE=CHECKBOX NAME="Personal_Svcs_Health_Insurance" value="0">
Health Insurance<BR>
<INPUT TYPE=CHECKBOX NAME="Personal_Svcs_Education_Funding" value="0">
Education Funding<BR>
<INPUT TYPE=CHECKBOX NAME="Personal_Svcs_Retirement_Planning" value="0">
Retirement Planning<BR>
<INPUT TYPE=CHECKBOX NAME="Personal_Svcs_Disability_Insurance" value="0">
Disability Insurance<BR>
</span></font>
</P>
</BLOCKQUOTE>
<P>
<font face="Tahoma" style="font-size: 9pt">Please select any of the following <u>Other Services</u> that apply:</font></P>
<BLOCKQUOTE>
<P>
<font face="Tahoma"><span style="font-size: 9pt">
<INPUT TYPE=CHECKBOX NAME="Other_Svcs_Website_Design" value="0">
Website Design<BR>
<INPUT TYPE=CHECKBOX NAME="Other_Svcs_Logo_Design" value="0">
Logo Design<BR>
<INPUT TYPE=CHECKBOX NAME="Other_Svcs_HR_Consulting" value="0">
HR Consulting<BR>
<INPUT TYPE=CHECKBOX NAME="Other_Svcs_Government_Contractor_Registration" value="0">
Government Contractor Registration<BR>
<INPUT TYPE=CHECKBOX NAME="Other_Svcs_501c3_Setup" value="0">
501c3 Setup<BR>
<INPUT TYPE=CHECKBOX NAME="Other_Svcs_Event_Services" value="0">
Event Services</span></font><BR>
<INPUT TYPE=CHECKBOX NAME="Other_Svcs_Printing_Services" value="0">
<font face="Tahoma"><span style="font-size: 9pt">Printing Services</span></font></span></font><BR>
</P>
</BLOCKQUOTE>
<input type=submit name="sent" value="Submit Form" language="JavaScript" onclick="return validate(this)">
<INPUT TYPE=RESET VALUE="Reset Form">
</FORM>
</body>
</html>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

would any of the moderators put this guy's code in appropriate tags... :)
hi, do use php tags for php code
code tags for html code
if you want quote something others have said use quote
use img for images
use url tags to wrap urls
i hope these would get u started to post properly :)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Include this on the top of the same page where your form is...hope this solves :)

Code: Select all

if ($_POST["sent"] == "Submit Form"){
	header(Location: help2.php);
}

or

Code: Select all

if ($_POST["sent"] == "Submit Form"){
	echo "<script>window.location=\"help2.php\";</script>";
}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

then regarding your javascript code,
concatenate all the error messages with a newline character to separate them and display at the end when valid is false.
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post by laticca8 »

raghavan20 wrote:then regarding your javascript code,
concatenate all the error messages with a newline character to separate them and display at the end when valid is false.
thank u so much for reaching out to me. i do apologize again about the posting. ummm, i don't quite understand what u mean by this advice. i'm relatively new to PHP and script writing, so . . . . :oops:
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

this is how it should be!!!

Code: Select all

<script language="JavaScript" type="text/javascript"> 

function Validatemercy() 
{ 
var valid = true;//store the validity of fields
var errorMessage = "";//to store error messages
var method = document.mercy.ContactMethod; 
var email = document.mercy.EmailAddress; 
var address = document.mercy.PostalAddress; 

if (method.selectedIndex < 1) 
{ 
errorMessage = "Please provide your preferred method of contact." + "\n" ;
method.focus(); 
valid = false;
} 

if ((method.selectedIndex==1) && (email.value == "")) 
{ 
errorMessage += "Please enter your email address." + "\n" ; 
email.focus(); 
valid = false;
} 

if (valid){
	return true;	
}else{
	return false;
	alert (errorMessage);

}

} 
// --> 
</script>
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post by laticca8 »

raghavan20 wrote:this is how it should be!!!

Code: Select all

<script language="JavaScript" type="text/javascript"> 

function Validatemercy() 
{ 
var valid = true;//store the validity of fields
var errorMessage = "";//to store error messages
var method = document.mercy.ContactMethod; 
var email = document.mercy.EmailAddress; 
var address = document.mercy.PostalAddress; 

if (method.selectedIndex < 1) 
{ 
errorMessage = "Please provide your preferred method of contact." + "\n" ;
method.focus(); 
valid = false;
} 

if ((method.selectedIndex==1) && (email.value == "")) 
{ 
errorMessage += "Please enter your email address." + "\n" ; 
email.focus(); 
valid = false;
} 

if (valid){
	return true;	
}else{
	return false;
	alert (errorMessage);

}

} 
// --> 
</script>
i'm so sorry i've frustrated you! i do thank you so much, though! my form is working properly now, though i have yet to incorporate these changes you've just suggested! the header redirect is working just as it should!

again, please forgive me for my ignorance! :cry:
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post by laticca8 »

got another question:

why doesn't this work when i try to hold the values to be stored in the thank you page that sends the info to the database?

Code: Select all

if ($_POST["sent"] == "Submit Form"){
   $name = $_POST['name'];  
   $contactmethod = $_POST['contactmethod'];  
   $email = $_POST['email']; 
   $address = $_POST['address']; 
   $referral = $_POST['referral'];  
   $bizstartup = $_POST['bizstartup'];  
   $bizcredit = $_POST['bizcredit'];  
   $_501c3setup = $_POST['_501c3setup'];  
   $_529plan = $_POST['_529plan'];  
   $_401k = $_POST['_401k'];  
   $disabilityins = $_POST['disabilityins']; 
   header("Location: help2.php");
   exit;
}
i read that i could use multiple statements for a condition. the header redirect works, but the values don't store. my help2.php has this script that works fine when help2.php is the action of another form.

Code: Select all

if ($_POST["sent"] == "Submit Form")
{
$DBhost = "localhost";
$DBuser = "*******";
$DBpass = "********";
$DBName = "info requests";
$table = "test";
$id = $_POST['id']; 
$name = $_POST['name'];  
$contactmethod = $_POST['contactmethod'];  
$email = $_POST['email']; 
$address = $_POST['address']; 
$referral = $_POST['referral'];  
$bizstartup = $_POST['bizstartup'];  
$bizcredit = $_POST['bizcredit'];  
$_501c3setup = $_POST['_501c3setup'];  
$_529plan = $_POST['_529plan'];  
$_401k = $_POST['_401k'];  
$disabilityins = $_POST['disabilityins'];
$string1 = "Thank you for your interest!";
$string2 = "We will promptly send the requested information to";
$string3 = "We look forward to working with you!";


mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select database $DBName");

$sqlquery = "INSERT INTO $table VALUES('$id','$name','$contactmethod','$email','$address','$referral','$bizstartup','$bizcredit','$_501c3setup','$_529plan','$_401k','$disabilityins',NOW())";

$results = mysql_query($sqlquery);

mysql_close();

if ($contactmethod == "Email")
{
echo "$string1 $string2 $email.<br><br>";
}
else
{ 
echo "$string1 $string2 $address.<br><br>";
}
echo "$string3";
}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

PHP:
if ($_POST["sent"] == "Submit Form"){
$name = $_POST['name'];
$contactmethod = $_POST['contactmethod'];
$email = $_POST['email'];
$address = $_POST['address'];
$referral = $_POST['referral'];
$bizstartup = $_POST['bizstartup'];
$bizcredit = $_POST['bizcredit'];
......
......
I dont actually understand wot you are saying??? You want these values to be accessed in the redirected page.
as you shd be knowing, its not possible to access global and local variables that are defined in one page in another. you have to either store them as session or cookie variables or pass all of them through GET or POST using a form.
laticca8
Forum Newbie
Posts: 13
Joined: Sat Aug 20, 2005 2:58 am

Post by laticca8 »

okay, raghavan20. thanks for the advice. i'll figure it out eventually.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

$name = $_POST['name'];
$contactmethod = $_POST['contactmethod'];
$email = $_POST['email'];
$address = $_POST['address'];
$referral = $_POST['referral'];
$bizstartup = $_POST['bizstartup'];
$bizcredit = $_POST['bizcredit'];
......
store all these POST variables into an array like
$tempArray = $_POST;

register the array with the session
session_register($tempArray);
and you can access all these variables in all pages of the application. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

raghavan20 wrote:register the array with the session
session_register($tempArray);
note: remember the following caution
session_register() wrote:If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.
Post Reply