PHP submitting a form and validation

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
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

PHP submitting a form and validation

Post by someone2088 »

Hi,

I currently have four pages: index.php, home.php, shoppingBasket.php and checkout.php
I also have a file called validateCheckout.asp which contains a load of javascript which I am trying to use to validate the data entered into the little form in checkout.php

The first page the user sees when the browse to the site is the index.php page. Here, the user is asked to log in, by entering their name into a text box, and clicking submit.

Once they have clicked submit, they are taken to home.php where a table displays a list of games, and the info related to those games, that has been retrieved from a PostgreSQL database table. The table displays the game titles, prices and descriptions, along with a check box to allow the user to select any individual game.

When the user has ticked one or more of the check boxes, they can click an 'Add to basket' button, which adds the selected games to the shopping basket. This button also automatically takes the user to the shoppingBasket.php page, where they view the contents of their shopping basket, again, displayed in a table.

On the shoppingBasket.php page, there is also a button titled 'Proceed to checkout'. When the user clicks this, they are taken to the checkout.php page, where the total price for all of the items in the shopping basket is displayed, along with a small form for the user to enter their email address and credit card number.

The problem I am having is, on the checkout.php page, when the user enters their email address and credit card number, and clicks the submit button my form is calling the validateCheckout.asp page, which should check whether or not the text that has been entered into the two fields passes the validation rules. If it does, the user should be taken to a page which says that their order has been placed, and if not, it should tell them that what they have entered into the text boxes is not valid.

This is the code for my checkout.php page:

Code: Select all

<?php 
session_start(); 
	
?>


<html>
<head>
<title>Checkout</title><a href="logout.php">Log Out</a>
</head>
<body>
<h1>Checkout</h1>

<?php
	echo "Welcome ".$_SESSION['userName']."!";
	echo "Total Price = £". $_SESSION['total'];
?>
<form name="checkout" action="validateCheckout.asp" onsubmit="return validateCheckout();" method="post">
	Email address: <input type="text" name="emailAddress" /><br />
	Credit card number: <input type="text" name="creditCardNumber" /><br />
	<input type="submit" value="Submit">
</form>

</body>
</html>
and this is the code for my validateCheckout.asp file:

Code: Select all

function validateEmailIsEmpty(){
	var email=document.forms["checkout"][emailAddress].value;
	if(email==null || email==""){
		alert ("You must enter an email address");
	}
}	
	
function validateEmailFormatIsCorrect{	
		var atpos=email.indexOf("@");
	var dotpos=email.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
	{
		alert("The email address entered is not valid");
		return false;
	}
}
	

function validateCreditCardIsEmpty(creditCardNumber, helperMsg){
	var creditCard=document.forms["checkout"][creditCardNumber].value;
	if(creditCard==null || creditCard==""){
		alert ("Your must enter a credit card number");
	}
}
	
function validateCreditCardIsNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(creditCardNumber.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		creditCardNumber.focus();
		return false;
	}
}
Currently however, when the user clicks the submit button on the checkout.php page, they are taken to an 'Error 403' page, which says "sorry, the page you requested is not accessible."

Provided the information that they entered into the text boxes in the checkout.php page is valid, they should be taken to an "orderPlaced.php" page.

Could someone point out to me what I'm doing wrong here?

Many thanks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP submitting a form and validation

Post by Celauran »

someone2088 wrote:Provided the information that they entered into the text boxes in the checkout.php page is valid, they should be taken to an "orderPlaced.php" page.
I see no code supporting this. The form sends them to validateCheckout.asp
someone2088 wrote:Could someone point out to me what I'm doing wrong here?
Your form's action is a page full of JavaScript functions mislabeled as an ASP page. You're calling validateCheckout() onsubmit, which is the right idea, but I don't see that function defined anywhere.
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP submitting a form and validation

Post by someone2088 »

Thanks for your reply.

So should I change the form action to "orderPlaced.php", so that it takes them to that page? If so, how do I then get the Javascript that's in my "validateCheckout.asp" to check that the text entered in the form is valid?

Should I even have a .asp file for my Javascript, or should that be somewhere else? Sorry, I'm really new to all this.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP submitting a form and validation

Post by Celauran »

Javascript files are generally .js

Calling the JS function in onsubmit is fine, but currently you're calling a function that doesn't exist. Create a function that calls each of the validation functions in turn and returns true or false based on that. Bear in mind that even with client-side validation, you'll still want server-side validation.
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP submitting a form and validation

Post by someone2088 »

Ok, so having changed my validateCheckout.asp file to validateCheckout.js, would I then create a new function in validateCheckout.js along with the validation rules that I have already written, that just calls the validation rules?

i.e.

Code: Select all

function validateCheckout(){
     validateEmailIsEmpty();
     validateEmailFormatIsCorrect();
etc...
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP submitting a form and validation

Post by Celauran »

Basically, yes. Just make sure you're passing in the appropriate parameters.
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP submitting a form and validation

Post by someone2088 »

Ok, I've added function validateCheckout(){} with calls to all of the validation rules like I mentioned above, but when I enter an email address and credit card number, and click 'submit' I'm still taken to the "Error 403" page which I mentioned in my first post. Any ideas why this is?
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP submitting a form and validation

Post by someone2088 »

I've changed the code on my "checkout.php page", so that it is now taking me to the "orderPlaced.php" page when I click "submit".

However, the form is no longer being validated by the Javascript in validateCheckout.js, despite the fact that the 'onsubmit' attribute in my form on the "checkout.php" page is still set to "return validateCheckout();"

Do I need to specify where it should look for this function? How would I do that?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP submitting a form and validation

Post by Celauran »

Yes, you need to include the JS file in your document head.
Post Reply