3 Replies - 14326 Views - Last Post: 08 April 2008 - 04:18 AM Rate Topic: -----

#1 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 412
  • Joined: 19-December 07

How can I pass Variable between the Page using URL

Posted 04 April 2008 - 01:48 AM

How can I pass Variable between the Page using URL?

I a bit confuse with the function GET and POST...

When should use GET and when we should use POST...

Got any simplest coding such as passing helloWorld to another page using php...

Thanx
Is This A Good Question/Topic? 1

Replies To: How can I pass Variable between the Page using URL

#2 deepamanohar  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 14-March 08

Re: How can I pass Variable between the Page using URL

Posted 04 April 2008 - 03:56 AM

Hi
There are two different ways to send a information from the browser to the server one through a URL and other within the body of the HTTP request,

you can pass variable either by a get or post. It depends upon the situation where it exactly needed.

In POST method data entered inside the form is passed to action page by using HTTP header. For example if you are doing a registration or a sign up process we should use the post method by placing method="post" in the form tag. To be more clear for some secure purpose such as to access the data in the database we can use the post method.

GET method transfer the data to the processing page in name value pairs through URL, so it is exposed and can be easily traced by visiting history pages of the browser.

Get can be used for search purpose. For example if you have some products search you can send the parameters as a query string in the URL, the usage behind this is lot one i can tell is that they can bookmark the pages for his reference and use it when the user comes later to find the same product.

If you need to send the variable as a get from a form then you have to mention method="get" . But this method works only if there are few parameters as the length of the URL is limited. There are more chances that the URL will be truncated if the form uses a large number of parameters, or if the parameters contain large amounts of data.


I had placed a small sample code to use the get and a post, if you want to send the value "helloworld" in the next page which is in a text field, just place the "method=post" in the form tag and have a submit button to next page.

<code>
<form method="post" action="test.php">
<input type=input value="helloworld" name="world">
<input type="submit" value="submit" name="submit">
</form>
</code>

You can access the value in the next page as $_POST["world"]. If you have placed "method=get" access a get value as $_GET["world"].

In the get you can see the variables are displayed in the address bar but this is not the condition with post

With out using a form also you can send the value of a variable in the next page .

example : "<a href='test.php?name=joe' target='_parent'>Click here</a>

In the next page you can access the variable has $_GET because we are sending the name in the URL.


Apart from these you can even pass the variable between the pages using the Sessions and Cookies. But these can be used only on certain cases. Such as login authentications or while using a shopping cart etc. As these will help in access the value of the variables in all the pages.

I hope this will to some extent clear your doubt :)

Thanks
Was This Post Helpful? 0
  • +
  • -

#3 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: How can I pass Variable between the Page using URL

Posted 04 April 2008 - 10:21 AM

There are different times when you need to use GET and when you need to use POST, and then times when you need to use SESSION.

GET is the right thing to do if the content is STATIC (or at least not volatile). Also, keep in mind anybody can simply type in the URL - so a system like controlpanel.php?user=spearfish without any other authentication systems wouldn't be the best - everybody else could access my control panel!

Also, by definition, GET pages can be browser cached. So you need to either mark the content as volatile with a pragma, or just use POST in those areas.

POST be default tells the browser NOT to cache the results. Additionally, they cannot go directly to the page they want. For example, if you have a system that lets a user select a profile, GET would be better because the content wouldn't change all that much, and a visitor can bookmark the link.

SESSIONs work a lot like POST, but are a type of cookie. The problem you are describing is called "maintaining state". Although GET works well, there's some information you don't want in the URL, so we used POST. But using POST for everything means that every link must be a disguised submit button. To solve this problem there are SESSIONs. To register a SESSION variable, do this:
$_SESSION['cookie'] = sugar;



All SESSION variables are stored in the $_SESSION associative array. To access this, you need to put <?php session_start(); ?> at the very top of the HTML, even before the DOCTYPE. Then you can directly pull the variables. So,
<?php session_start();
$_SESSION['car'] = "ford";
echo $car;
?>


would out put "ford". To end a SESSION, use session_destroy and all SESSION variables will be forgotten.

~~~

To mark volatile content:
<META HTTP-EQUIV="Pragma" CONTENT="no-cache" />
<META HTTP-EQUIV="Expires" CONTENT="-1" />


in the HEAD of the document.
Was This Post Helpful? 1

#4 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 25
  • View blog
  • Posts: 412
  • Joined: 19-December 07

Re: How can I pass Variable between the Page using URL

Posted 08 April 2008 - 04:18 AM

Thank a lot It really useful information

Thank spearfish and deepamanohar!!!!

But... I not successfully master the session part.... :(

Anyway, I felt happy that I master the GET and POST function....


Luv you all...

This post has been edited by fsloke: 08 April 2008 - 04:20 AM

Was This Post Helpful? 1

Page 1 of 1