jacko_162 Posted December 29, 2010 Share Posted December 29, 2010 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 :'( Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 29, 2010 Share Posted December 29, 2010 Why do you have the header() redirect as the first thing in the script, and allow it to trigger unconditionally? Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted December 29, 2010 Author Share Posted December 29, 2010 Why do you have the header() redirect as the first thing in the script, and allow it to trigger unconditionally? ooh how shoud it be? im a php n00blet.. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 29, 2010 Share Posted December 29, 2010 it should be wherever you want the user to be redirected. right now it's at the top, so the user is redirected right away regardless of anything that happens afterward. Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted December 29, 2010 Author Share Posted December 29, 2010 so how can i code it in a way to say redirect if fine but if any of the errors occur then forward to failure.php or something? Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted December 30, 2010 Author Share Posted December 30, 2010 is there anyway i can make the forward into php with an if statment? say if success the index.php else error.php? Quote Link to comment Share on other sites More sharing options...
Zurev Posted December 30, 2010 Share Posted December 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted December 30, 2010 Author Share Posted December 30, 2010 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? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2010 Share Posted December 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted December 30, 2010 Author Share Posted December 30, 2010 tbh the form is all hidden fields except a submit button so the second option would mean re-writing alot of code across the site, where i like the idea of the sessions. how long will the sessions be stored for? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2010 Share Posted December 30, 2010 You could always just put the form processing code ON the page where your form is already at? Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted December 30, 2010 Author Share Posted December 30, 2010 i managed to send the sessions variables and echo them, with a unset($_SESSION[]); afterwards so it deletes the session after displaying. seems to work for now =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.