PHP submitting a form and validation
Posted: Thu Dec 01, 2011 1:23 pm
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:
and this is the code for my validateCheckout.asp file:
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
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>
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;
}
}
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