Jump to content

Recommended Posts

Here is my situation

 

1.  User fills out form and clicks the submit button

2.  On next page, get the POST variables

3.  If POST variables empty, redirect back to original page and show form

4.  If POST variables complete, do other code

 

I did have a theory that before i send to the other page, i send it to iteself to do some form validation and if that passes, then go to the other page.

 

What i don't want to do is have the user having to type in onfo all over again.

 

What would be best? Could i have the form validation done of the other page and if it fails send it back and if so, how i would fill in the form with validated data so the user doesn't have to type it again

<?php
if(issset($_POST['submit']) {
    //validate the post variables to make sure nothing is left out

    //if all passed okay go to other page
    header('Location: mypage.php');
}
?>
//my form code goes here<form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post">

 

Link to comment
https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/
Share on other sites

you can set the user info to sessions and set the input values to the sessions...so if the user has not filled out any info..the value will be empty...however if they have filled out info, the input value will be set to that info...that way if they are redirected back the the form, their info will remain intact..however make sure that you unset the sessions upon form completion

For every input type="text", in the value="" field you write inline PHP code, checking if there was a $_POST. If there was for that item, you echo out its value otherwise leave it blank:

 

<input type="text" name="username" value="<?=(isset($_POST['username']) ? $_POST['username'] : '')?>" />

I've done this in a project before. I had a very neat solution: you create an error variable and set it to 0.

 

For each field that you validate and it fails validation, you increment $error_count by 1.

 

Then after validation BUT BEFORE any output, if $error_count > 0, then show same page (still checking for posted variables), otherwise redirect the user to the completion page.

The only real solution is sessions are AyKay47 suggests.

 

@silkfire, I don't see how your suggests can solve this problem because once the user is redirected back to the form there will be no $_POST data, redirects don't transport the previous post data.

The simplest solution is to put the form and the form processing code on one page. Any existing post data is available to put back into the fields and you can validate and display any errors next to the field they apply to.

The only real solution is sessions are AyKay47 suggests.

 

@silkfire, I don't see how your suggests can solve this problem because once the user is redirected back to the form there will be no $_POST data, redirects don't transport the previous post data.

I never said redirect to same page I said show the same page thus retaining the POST variables. You won't leave the page until all fields are correctly entered.

you can use $_POST values...however that method is not full-proof and will rely on the user not refreshing the page by accident or something along those lines..

If you refresh a page with post variables most modern browsers will ask if you want to repost the page.

you can use $_POST values...however that method is not full-proof and will rely on the user not refreshing the page by accident or something along those lines..

If you refresh a page with post variables most modern browsers will ask if you want to repost the page.

and the average user does not know what that means and will cancel

Here's an additional suggestion for any form submission - After you have successfully processed a form submission, you actually need to redirect to the same page as the form's action="" attribute to change the browser's history for that page from a form submission to a GET request so that the browser won't attempt to resubmit the data should you browse back to that page.

 

If you have single page or separate pages for the form and the form processing code, anytime you browse back to the page that matches the action="" attribute of the form, the browser will attempt to perform the last operation it has recored in the browser's history for that page.

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.