Link to home
Start Free TrialLog in
Avatar of theprankstanator
theprankstanator

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of aratani
aratani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of theprankstanator
theprankstanator

ASKER

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
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
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
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
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
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
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
Sorry - forgot to mention that there are heaps of pages in content.php, i have just snipped it.
so, the username/password script is in home.php right?
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;
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
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

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!!)
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
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.
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
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
No problem, Christian.

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

Take care

AJ