What would be the best way for me to pass data from a form on one page to another? For example an email form field is input on one page and that takes the user to a new page with that field already completed. If someone entered the page without using the email form submit link then the field would be blank. I know this can be done through javaScript or PHP and I am open to either possibility. Are there any examples of this being done that I can be directed to?
You don't need any JavaScript to do this.
You can simply invoke either the $_GET or $_POST arrays in PHP and populate your form fields (on the second page) with any values that have been filed out.
For example:
<input type="text" name="email" value="<? echo $_POST['email']; ?>" />
If there is an email that is being sent from the previous page via a form, it will display in the input field. If not, nothing will display.
Check out: $_POST, $_GET, and $_REQUEST in the PHP manual. These pages offer some quick examples to show you how these arrays allow you to work with forms easily in PHP.
Note: You should also take time to make sure that you sanitize your data before presenting it on the page.
- You can use header, to send a variable value from one page to another
$msg = "blablabla"; header('location:index.php?msg=$msg');
- Or you can use a session variable $_SESSION['msg'] = "the message you want to send";
In the next page you can use this same session variable and retrieve the value. $msg = $_SESSION['msg']
For this method make sure the the session is start before you assign the variable.
for more info check $_SESSION