Welcome to WebmasterWorld Guest from 54.196.175.173

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

form post

     

tr8er8

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

5+ Year Member



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

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

5+ Year Member



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

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

5+ Year Member



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

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

5+ Year Member



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

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

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



An alternative would be Ajax validation.

dc

andrewsmd

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

WebmasterWorld Senior Member 5+ Year Member



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

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

5+ Year Member



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

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

WebmasterWorld Senior Member 5+ Year Member



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

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

5+ Year Member



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month