Solved

Carrying info from a form to a new page? (hard to explain)

Posted on 2004-08-13
19
253 Views
Last Modified: 2006-11-17
Hi Guys,

I am writing a registration form for a client. I need the user to input name, email and vehicle into a form. That would insert the data into my db (thats all easy). But, what I want is once the user has entered this info, it redirects them to another page where their information is available to view (and maybe even mail() it to them) ... I am guessing I will need to use a session for this, is that right?

Any help would be muchly appreciated.

Thanks,

Christian
0
Comment
Question by:theprankstanator
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
19 Comments
 
LVL 4

Accepted Solution

by:
aratani earned 500 total points
ID: 11798720
One of the easiest ways to do a form is to post it the same page. For example if you have a user name to enter in a form you can format the form in such a way,

<form action = "<?php echo $_SERVER ['SCRIPT_NAME']; ?>">

  <input name = "username" type = "text">

</form>

In this way when the form is submitted it is submitted to the same page. Thne you check whether the value has been posted. This code goes on the top of the page generally.

if ($_POST ['username']) {

  //do what you need to do to check if the form has been submitted

}

else {


}

Now, to go to a next page you need to use the header function in php. To switch to a page called process.php, you would use,

header ("Location: process.php");

That is one of the ways to redirect a page, but it will only work if you haven't sent any output out. To store the value between pages the best way is use a session. So, to do that, you need to declare,

session_start (); //This function loads all the sesison variables in memory and starts the session

at the start of the page.

To store the session value, you need to assign it to the $_SESSION array (after you declare the session_start).

So, something like this,

$_SESSION ['username'] = $username;

In the next page to access the variable again you need the following,

$username = $_SESSION ['username']; //make sure you declare session_start before it.

Also, sessions are secure since it is on th server-side.

To send mail from a page, I would suggest to read this post on the php manual, it is very easy if you follow the examples given,

http://us2.php.net/function.mail

You use the mail () function in php to send the e-mail.

I hope it helps, if you would like to write complete script for you please message me back with you exact requirements and I'll do that. But, I hope you choose to use my information to try it on your own and you will become a much better coder.

Hope this helps

Regards

AJ



0
 

Author Comment

by:theprankstanator
ID: 11798834
Hey aratani,

By far the most informative answer I have had on here yet, thank you.

I want to do it all myself to learn, so don't worry about writing it for me. :)

I will have a go and post back shortly - thanks again
0
 

Author Comment

by:theprankstanator
ID: 11798856
Hey aratani,

I am having a few problems, I tried both options, set session_start() etc... But, I am actually now preferring the idea of having it all on one page...

So, I did this;
******************************************
if ($_POST['name']) {
?>
<form method="post" name="form1" action="<?php echo $_SERVER ['SCRIPT_NAME']; ?>">
Name:<br />
<input class="formfield" name="name" type="text" /><br />
<snip> - there are many more form fields
</form>
<?
}
else {
?>
testing
<?
}
******************************************
And I continue to get the form whether I have submitted it or not.

Thanks again!

Christian
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 4

Expert Comment

by:aratani
ID: 11798862
Christian,

One slight error. You have do the testing if the $_POST condition is true (i.e. something was posted). Otherwise, show the form (i.e. the user didn't fill out the data or the form hasn't been submitted yet). The correct version is below.

if ($_POST['name']) {
?>

testing

<?
}
else {
?>

<form method="post" name="form1" action="<?php echo $_SERVER ['SCRIPT_NAME']; ?>">
Name:<br />
<input class="formfield" name="name" type="text" /><br />
<snip> - there are many more form fields
</form>

<?
}

This should work now. Ask me more question if you have any.

Take care

AJ
0
 

Author Comment

by:theprankstanator
ID: 11798889
Hi AJ,

No go :(

It redirects me to my index (I have a switch setup for navigation, ie index.php?page=blah) ... not sure why, any ideas?

Christian
0
 
LVL 4

Expert Comment

by:aratani
ID: 11798903
Okay Christian, I don't understand.

You have a page called index.php that has this form or which page would have this form. This form would redirect back to the page from where the form came from that is itself. If you could give me the code for the index.php, I would be able to solve your problem.

Thanks,

AJ
0
 

Author Comment

by:theprankstanator
ID: 11798921
Hi AJ,

my index consists of this;

include('header.php');
include('content.php');
include('footer.php');

and then in content.php;
switch( $_GET['page'] ) {
      case null:
            include ("home.php");
            break;
      case 'home':
            include ("home.php");
            break;
      default:
            include ("home.php");
            break;
}

So, when I submit the form, it goes to default instead of to the same page.

Does that make sense?

Thanks again!

Christian
0
 
LVL 4

Expert Comment

by:aratani
ID: 11798922
Also, give if you can, please give me the name of the script and the code for the script that executes the username/password form.

Take care

AJ
0
 

Author Comment

by:theprankstanator
ID: 11798944
Sorry - forgot to mention that there are heaps of pages in content.php, i have just snipped it.
0
 
LVL 4

Expert Comment

by:aratani
ID: 11798952
so, the username/password script is in home.php right?
0
 

Author Comment

by:theprankstanator
ID: 11798959
Sorry, the script asks for user, email and vehicle. That is in form1.php which in content.php i have;
case 'rego_form':
      include ('form1.php');
      break;
0
 
LVL 4

Expert Comment

by:aratani
ID: 11798968
okay what you have is your index.php looking like this,

include('header.php');
include('content.php');
include('footer.php');

so, your index.php page includes all the code required in index.php (i.e. index.php includes content.php which includes home.php). But everything is running from index.php. So, $_SERVER ['SCRIPT_NAME'] is the script where everything is running from (i.e. index.php). So, thats why it comes back to index.php.

I'll give you two ways of doing it. I would do it the second way.

What you could do is the following? Let us assume since index.php is the form everything is running in. What you could do is post it to index.php. So, your index.php could look like this (assuming you wanted the header and footer everytime),

include('header.php');

if ($_POST ['username']) {

   do the testing with the user name and password since its been posted
}

include('footer.php');

Your user name and password form page could just be the following,

<form method="post" name="form1" action="<?php echo $_SERVER ['SCRIPT_NAME']; ?>">
Name:<br />
<input class="formfield" name="name" type="text" /><br />
<snip> - there are many more form fields
</form>

BETTER SOLUTION
============

But, you have to understand that the way you have developed it comes back to index.php so you have to do all the $_POST processing in index.php. If you had page called login.php just for the login of the user then you could have all your checking on that page and then redirect the user back to index.php, which I think is a better solution.

i.e. if the user is not logged on he is directed to this page (you can set a session variable to tell you if the user logged on or not).

So, you would have index.php as,

if (!$_SESSION ['username']) {

   header ("Location: login.php");
}

include('header.php');
include('content.php');
include('footer.php');

Now, login.php would look like what we had before except for the following,

if ($_POST['name']) {
?>

//check if the username and password are correct
if (password is correct) {

  //set session variable
  $_SESSION ['username'] = $_POST ['name']; //will be used in index.php and all other pages to make sure user has logged in. You could also use this variable to figure out who the user is. So, it serves double purpose. To make sure you have logged in the user, and to find the identity of the user

  header ("Location: index.php"); //take him back to the page

}
<?
}
else {

//if you needed a header and footer on your login page
include ('header.php');

?>



<form method="post" name="form1" action="<?php echo $_SERVER ['SCRIPT_NAME']; ?>">
Name:<br />
<input class="formfield" name="name" type="text" /><br />
<snip> - there are many more form fields
</form>

<?

include ('footer.php');
}

?>

I hope this will solve your problem now, this way you are separating username/password onto a different page which is the right way to do it. And you are using session variables to store the fact that the user has logged in and know who the usre is. So, you have a better and more powerful system.

Take care .. I have to go to sleep now ..  

Hope you are satisfied,

AJ
0
 
LVL 4

Expert Comment

by:aratani
ID: 11798975
Oh, also actually after reading your last comment about the 'rego_form', this is the way then for index.php (if you are using my solution 2 .. the better one).

Your code:

case 'rego_form':
      include ('form1.php'); //This include is causing the problem since the main page that is loaded is still index.php. So, do the following for this
      break;

Corrected code: (hopefully)

case 'rego_form':
      header ('location: form1.php'); //This will take you to the login page
      break;

In form1.php, just do it as I described in the earlier post. You probably don't need to set the session variables since it doesn't look like you are logging in a person just storing some information through a form. But if you are transferring pages and you want the information to be there inthe next page, just est session variables (don't forget the session_start (); !!!!).

Also, remember header () function won't work if you have already sent out some output to the screen (Whcih you might in header.php). So, maybe you could first check for redirection to the form1.php and then send output.

Anyway, if yo uneed more help I'll do it tommorow,

Take care

AJ

0
 

Author Comment

by:theprankstanator
ID: 11798976
I am not asking for a login, purely gathering information from the user for registration for an event, not registering on the site. (I'm so sorry, I should have mentioned this aggggesss ago!!)
0
 
LVL 4

Expert Comment

by:aratani
ID: 11800817
Yeah, I figured that out when  you told me the information you were trying to access was just name, e-mail and vehicle registration.

So, did you try out what I said in my last post? It would reidrect to the login page if the case was 'rego_form' and then in form1.php you would take the information (as in two posts ago) and process it and then redirect back to the main page. form1.php would like the following,

if ($_POST['name']) {
?>

  //testing here
  header ("Location: index.php"); //take him back to the page

}
<?
}
else {

//if you needed a header and footer on your login page
include ('header.php');

?>

<form method="post" name="form1" action="<?php echo $_SERVER ['SCRIPT_NAME']; ?>">
Name:<br />
<input class="formfield" name="name" type="text" /><br />
<snip> - there are many more form fields
</form>

<?

include ('footer.php');
}

?>

You could still set the session variables so that you can make the user feel like you know who he is on all the other pages he navigates. Like you could read the session variable on another page and say, "Welcome user".

Tell me what is happening.

Take care

AJ
0
 

Author Comment

by:theprankstanator
ID: 11818159
Ok, I am having a hardtime understanding what you mean. On the page after the form, I just want it to give the user an ID number for the event (just the auto_increment ID in the database that corresponds with the info he has inserted) and maybe their name.

Sorry if this is a hassle, if I could increase the points, I would.
0
 
LVL 4

Expert Comment

by:aratani
ID: 11837588
This is what you should do,

In your index.php change your case for the 'rego_form' to the following,

case 'rego_form':
      header ('Location: form1.php'); //This will take you to the login page. Remmeber you cannot have any output to the screen prior to this. If you do and it gives you a warning of headers already sent, send me another post, and I'll show you a workaround for that.
      break;

And in your form1.php

//if you needed a header and footer on your form page
include ('header.php');

<?php

if ($_POST['name']) {

  //This means that you have finished submitting the form
  //Do all the processing you need to do over here
  //Also show the user what he wants and enter all the information in teh database like the id number and all that
?>

<!-- This is where you will show the user all the information about the id number and thank him for entering the information -->

<?php

}
else {


?>

<form method="post" name="form1" action="<?php echo $_SERVER ['SCRIPT_NAME']; ?>">
Name:<br />
<input class="formfield" name="name" type="text" /><br />
<snip> - there are many more form fields
</form>

<?php

}

include ('footer.php');

?>

And don't worry about the points, if you are satisfied you could give me more points by opening up another question just for the points.

Thanks

Tell me how it goes ..

AJ
0
 

Author Comment

by:theprankstanator
ID: 11837605
Hi AJ,

Only 2 hours ago I got it working with Sessions, it works a charm - thank you! (I will give you full points still as it was your help that pushed me in the right direction).

Thanks very much!

Christian
0
 
LVL 4

Expert Comment

by:aratani
ID: 11837763
No problem, Christian.

Hope to help you out in the future too. :)

Take care

AJ
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Displaying a google org chart from php/mysql code 27 47
Set a time limit on Wordpress Cookie function. 3 45
JSON decode 5 42
Load string Array from file 23 37
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question