Get support from Xavier Media
It is currently Sun Dec 08, 2013 6:46 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: $_POST question.
PostPosted: Tue Dec 26, 2006 5:30 pm 

Points:
I have something to ask regarding $_POST

Let's say i have 3 page.

1st page will require 2 user to enter their name and there will be a submit button.

Code:
<form action="InputPage.php" method="post" enctype="application/x-www-form-urlencoded" name="form1">
                 Player 1
                 <label>
                 <input name="player1" type="text" id="player1" size="20">
                 </label>
                 <p>Player 2
                   <label>
                   <input name="player2" type="text" id="player2" size="20">
                   </label>
                 </p>
                 <p>
                   <label>
                   <input type="submit" name="Submit" value="Enter">
                   </label>
                 </p>
                 </form>


After they input their names and submit, it will go the next page. Which will echo the first user name.

Code:
Welcome <?php echo $_POST['player1'] ?>


After which, 1st user will key in something and then press submit button another and will go to another page. Page 3.

I want page 3 to show the 2nd user name. however i found out that the name couldn't be show.

Code:
Welcome <?php echo $_POST['player2'] ?>


I guess that it's because it's not directly from the 1st submit button. Thus on Page 3, 2nd user name is not able to get.

I tried to get the user 1 and 2 name on Page 2. I was able to get it.

Now i want to ask, what can i do to show the 2nd user name on Page 3. Any ways ?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: Tue Dec 26, 2006 9:31 pm 

Points:
Hi,

You can do it 1 of 2 ways.

The first way is to assign the values to a $_SESSION variable like so:

Code:

session_start();
$_SESSION['player1'] = '$_POST['player1']';   



Session variables are stored on the server and can be accessed from any page. However, your first line of code on every page (before the <!DOCTYPE ...> must be:

Code:

<?php
session_start();
?>



or you will get an error parsing the page.

The second way you can pass any variable from page to page is using the "hidden" form field like so:

Code:

<input type="hidden" name="player1" value="<?php echo $_POST['player1']; ?>">



HIDDEN fields will not be visible but will be passed as a standard $_POST variables.

Hope that helps.


Report this post
Top
  
Reply with quote  
 Post subject: Sessions, Cookies, and HTTP
PostPosted: Wed Dec 27, 2006 3:35 am 

Points:
Right on the money! :D

When passing values to web pages remember that "in the world of HTTP" there is no past. Now before I weird you out, what I mean is that each new page that is requested over the internet is treated like it is the first page ever to be requested.

There is no past or future as far as the computers are concerned. They don't remember what went on 50 seconds ago when you clicked on the "contact" page. Or that you are in the middle of a purchase. That is just how the HTTP spec. goes. Think of it like REALLY bad memory loss.

So just because you sent the "player1" data to page2 30 seconds ago doesn't mean that the computer will remember the "player1" variable on page3.

That is why COOKIE's and SESSION's were made - so that the server would have some way of remembering people.
1) By making a note to itself that they had already been there and putting it on the server. (sessions)
or
2) By giving the user's PC a note to give back to the server the next time they requested a page. (cookies)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 27, 2006 6:49 am 

Points:
Thanks for the help.

However, i've yet to learn session command. So i not sure where to place the code into.

Let's say i use the session command.

before <!DOCTYPE...>

i put in:

Code:
<?php
session_start();
?>


then at which line should i put in

Code:
session_start();
$_SESSION['player1'] = '$_POST['player1']';


So if i just want it to remember player2, i change the code to:

Code:
session_start();
$_SESSION['player2'] = '$_POST['player2']';


Is that correct ?

So, when i want it to show player2 in the 3rd page, what code do i use to get the player2 name shown ?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 27, 2006 6:58 am 

Points:
And also for the hidden form.

Do i replace my current form to become hidden form ?

Let's say my current form is

Code:
<input name="player1" type="text" id="player1" size="20" value="<?php if (!empty($_POST['player1'])) echo $_POST['player1'] ?>">


So i change the current one to hidden ?

Code:
<input type="hidden" name="player1" value="<?php echo $_POST['player1']; ?>">


Also for the player2, i change to hidden form too ?

Code:
<input type="hidden" name="player2" value="<?php echo $_POST['player2']; ?>">


The above form will be on Page1 where they input their name.

So on the 2nd page, when i want to show player1 name.

I will type:

Code:
<?php echo $_POST['player1] ?>


I'm not showing player2 name on 2nd page, because i want player1 to enter some data into the 2nd page first. After player1 keyed in the data, and press the submit button to proceed to 3rd page.

That's only when i want it to show player2 name.

And so if the above i said is correct,

On 3rd page, i'll just have to type in this code to show player2 name ?

Code:
<?php echo $_POST['player2'] ?>


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 27, 2006 7:36 am 

Points:
One more question i would like to ask.

I would like to check whether both the player had enter their name in the field on submit button.

What i am thinking now is that, i have to create one more page.

This new page is that if they did not enter any name or missing name, they will be directed to the new page.

So if they had enter both their names.

then they will continue to the next page.

[php]

If (isset($_POST['submit']) )
{
if (strlen($_POST['player1']) > 0 ) && (strlen($_POST['player2']) > 0)
the page will be directed to the next.

else

the page will be directed to the new page which is asking them to enter their name before pressing the submit button.
}
[/php]

i not sure whether there is another way to check.

Can't think of it right now ? so i help to get help from you guys.

Actually i wanted it to pop out a small window using javascript to tell their that their name is not entered instead. However, it might be too complicated so directing them to a new page would be easier right now.

When i have completed everything then i will do more add ons.

Thanks anyway.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 27, 2006 8:05 am 

Points:
Okay, i managed to get out the player2 name on the 3rd page.

what i did was add in a hidden form on 2nd page.

[php]<form action="GamePage.php" method="post">
<input type="hidden" name="player2" value="<?php echo $_POST['player2'] ?>" />[/php]

And then from there, i go to the GamePage.php (3rd Page) and add in.

[php]<?php echo "Welcome " .$_POST['player2'];?>[/php]

and everything works fine.

I am able to get the name to show for player2.

However, i would like to ask that.

Is it that the hidden form in page 2 that i have added in. It takes the value from Page 1 . Then pass it to Page 3 with the form action . That's why i am able to see the player2 name on Page 3.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 27, 2006 9:15 am 

Points:
Okay, i have also found out how to do validate the form.

Using the Validation Form option in dreamweaver. Ah..


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: Thu Dec 28, 2006 1:45 am 

Points:
Jr0x wrote:

Is it that the hidden form in page 2 that i have added in. It takes the value from Page 1 . Then pass it to Page 3 with the form action . That's why i am able to see the player2 name on Page 3.


Yeah, that is correct. The "hidden" field acts as a suitcase, if you will, between pages.

This is fine if you only have 1 or 2 values you need to keep track of. However, if you start to get more, I recommend using the $_SESSIONS variable or a standard cookie.

Much easier to keep track of that way.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]