homepage Welcome to WebmasterWorld Guest from 23.23.22.200
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
form post
tr8er8




msg:3962695
 7:24 pm on Jul 30, 2009 (gmt 0)

i have a form i want to validate that the fields arent empty on the page of the form. i can do that no problem, but if i want to redirect to another page when they are filled out on submit and add an action to the:

<form action="nextpage.php" method="POST">

it just goes to the nextpage.php before it even validates it. is there a way to validate the fields before going to the next page?

 

Tommybs




msg:3962707
 8:10 pm on Jul 30, 2009 (gmt 0)

perform the validation on nextpage.php or after you have validated the form data on the first page redirect them to nextpage.php using a header

e.g
[code]
if(isset($_POST['submit'])){
//validate form
if($valid == true){
header("Location: nextpage.php");
}else{
echo "<p>Learn how to use my form!</p>";
}
}

tr8er8




msg:3962845
 12:45 am on Jul 31, 2009 (gmt 0)

well i can validate on next page, except i want to do it on the page of the form. and it says headers already sent, because i already sent headers at the top of the page :/ like a redirect if a person doesnt meet requirements to view the page. so im assuming i cant use headers again, unless there is another way.

Tommybs




msg:3962985
 6:35 am on Jul 31, 2009 (gmt 0)

you can, just begin the page with ob_start(); before you send anything else to the browser

then headers can be sent anywhere on the page

dreamcatcher




msg:3962987
 7:00 am on Jul 31, 2009 (gmt 0)

An alternative would be Ajax validation.

dc

andrewsmd




msg:3963109
 1:51 pm on Jul 31, 2009 (gmt 0)

Another alternative would be to use templates. If you use templates then you can do all sorts of validation and still write header information. As long as you set templates and never actually "echo" anything. The last alternative which may be quick and dirty but fitting for you would be to use JS. Something like this.
if(your form is valid){
echo("<SCRIPT language="JavaScript">
<!--
window.location="http://someplace.com";
//-->
</SCRIPT>");
?>

tr8er8




msg:3963522
 3:22 am on Aug 1, 2009 (gmt 0)

Ok I got it to work I used ob_start() but anways, is there a way to still retrieve $_POST variables from the page since the form action is just
action="#" ?
andrewsmd




msg:3964637
 1:33 pm on Aug 3, 2009 (gmt 0)

As long as your code is in that page you can just request them via
$var = $_POST['yourName']; if it's going to another page before you validate then you need to either use get on your form method or use session variables.
session_start();
$_SESSION['yourName'] = $_POST['yourName'];
You would need to do that on the page where they click the submit button. Now $_SESSION['yourName'] would be whatever value that post variable was, the entire time the users browser was open.

tr8er8




msg:3964716
 3:30 pm on Aug 3, 2009 (gmt 0)

yeh i know i can use sessions but i dont want to use a bunch of sessions for something that people might not even follow through with. ill just post them on the same page :)

thanks

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved