Jump to content

passing errors from form processing script


jacko_162

Recommended Posts

I have a html for that posts to a form processing script which is fully functional atm,

 

in the processing script i have if statements, if the condition is met it updates tables in database, if there not met i echo a "not met" statment.

 

problem is my form processing script just goes straight back to the index.php page using the following code;

 

header('Location: index.php');

 

 

here is the form processing script:

<?php
session_start();

header('Location: index.php');

include "connect.php";

$id = $_SESSION['id'];
$user = $_SESSION['user'];
$ticketNumber = $_POST[ticketNumber];
//echo $_POST[ticketNumber];
$today = date('Y-m-d H:i:s', time() - 3600);

// Query "Ticket" Table to check if user has purchased a "regular" ticket within the last 24 hours
$query24hour = mysql_query("SELECT * FROM tickets WHERE username = '$user' AND HOUR(TIMEDIFF(NOW() , purchaseDate)) < 24;") or die(mysql_error());

// Query "promoTickets" table to grab information of previous tickets bought to the "promotional" auction and LIMIT tickets to 1 per user for "promotional" auctions. 
$querySold = mysql_query("SELECT * FROM promoTickets WHERE promoID='$_POST[promoID]' AND (ticketNumber='$ticketNumber' OR username = '$user');") or die(mysql_error());
//echo $querySold;
$sold = mysql_fetch_assoc($querySold);
//print_r($sold);
//echo $sold;
//echo query24hour;

$querycount24hour = mysql_num_rows($query24hour);
//echo $querycount24hour;

//check if ticket is sold and if user has purchased a "regular" ticket within 24 hours

if(empty($sold)!=FALSE and $querycount24hour >= 1){

//Checks users balance to see if they have enough for the ticket
$queryBal = mysql_query("SELECT user_iskbalance FROM users WHERE username = '$user';") or die(mysql_error());
//echo $querySold;
//echo $user;
//echo $queryBal;
$balArray = mysql_fetch_assoc($queryBal);
$bal = $balArray[user_iskbalance];
$newBal = $bal-$_POST[ticketPrice];



//check if he has the money to buy the ticket
if($bal>=$_POST[ticketPrice]){
	//remove the money
	$queryBalRemoveal = mysql_query("UPDATE `users` SET `user_iskbalance`='$newBal' WHERE `username`='$user';") or die(mysql_error());

	//buy ticket & insert data into "promoTickets" table
	$query = mysql_query("INSERT INTO promoTickets(promoID, username, charID, ticketNumber, ticketPrice, purchaseDate) VALUES ('$_POST[promoID]', '$user', '$id', '$_POST[ticketNumber]', '$_POST[ticketPrice]', '$today');") or die(mysql_error());
}
else{
die("Insufficent balance. Please add more ISK")	;
}


}
else{
die("Ticket has already been Sold or you have already bought a ticket to this promotion..!");	
}
?>

 

any way i can get the errors to be passed onto the index.php (with the form) and echo them there?

 

as it stands errors arnt shown to the users and its confusing people  :'(

Link to comment
Share on other sites

A really good/simple way to do it is instead of calling die() each error is to just add an error to an $errors array, and then if it's empty call your header redirect to index, else, redirect it with header to error page.

 

Example:

//define empty errors array
$errors = array();

// check if a field is empty
if (empty($somefield))
{
// add an error message to the array
$errors[] = "Somefield is empty.";
}


// check if errors exist at the end
if (empty($errors)) // no errors
{
header("location: index.php"); // redirect to index.php
} else {
header("location: errors.php"); // redirect to errors.php
}

 

Though instead of redirecting to errors I would use a foreach loop to echo each one of the errors out individually, but that's just me.

Link to comment
Share on other sites

thank you for the help,

 

will have a go now how do i get the errors.php page to echo these result? or cant they be passed on?

 

problem i have is the above coding for my page is a processing script, i guess the only way to echo the erros individually is to have the for post to itself then echo errors?

Link to comment
Share on other sites

There are two (straight forward) ways you can display the error messages in your form -

 

1) Pass them using a $_SESSION variable (the $errors array that Zurev has suggested can either simply be copied to a $_SESSION variable, something like $_SESSION['errors'] = $errors; or you could use $_SESSION['errors'][] directly in the code instead of $errors[] )

 

2) Put your form and your form processing code on a single page. This will also have the advantage of being able to directly redisplay the form data that was already entered so the visitor does not get pissed off about having to re-enter all the data.

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.