Welcome to WebmasterWorld Guest from 54.146.191.2

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

form post

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

Junior Member

5+ Year Member

joined:Feb 6, 2008
posts: 94
votes: 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?

8:10 pm on July 30, 2009 (gmt 0)

Full Member

5+ Year Member

joined:June 21, 2007
posts: 316
votes: 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>";
}
}

12:45 am on July 31, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 6, 2008
posts:94
votes: 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.
6:35 am on July 31, 2009 (gmt 0)

Full Member

5+ Year Member

joined:June 21, 2007
posts: 316
votes: 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

7:00 am on July 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


An alternative would be Ajax validation.

dc

1:51 pm on July 31, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 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>");
?>
3:22 am on Aug 1, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 6, 2008
posts:94
votes: 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="#" 
?
1:33 pm on Aug 3, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 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.
3:30 pm on Aug 3, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 6, 2008
posts:94
votes: 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