Solved

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

Posted on 2004-08-13
19
250 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
  • 10
  • 9
19 Comments
 
LVL 4

Accepted Solution

by:
aratani earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 4

Expert Comment

by:aratani
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry - forgot to mention that there are heaps of pages in content.php, i have just snipped it.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

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

Author Comment

by:theprankstanator
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No problem, Christian.

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

Take care

AJ
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now