Jump to content

[SOLVED] Passing Variable through the Query String


hanlonj

Recommended Posts

Hi, I am just trying to get to grips with passing a variable through a query string from one script to another. So I want to declare a variable e.g. $myString on the first page. I then want to append it to a link e.g.

 

<?php
  
   $myString = "This is my String!";

?>

// back to the html part of the page
<a href = "anotherPage.php?<?php $myString ?>">Another Page</a>

 

I then want the "anotherPage.php" to print the contents of $myString to the screen.

 

 

My attempt is this but it's not working:

 

<?php

$_GET[$myString];

echo "$myString";

?>

 

Can anyone help here please?

 

hj

Link to comment
Share on other sites

<?php
  
   $myString = "This is my String!";

?>

// back to the html part of the page
<a href = "anotherPage.php?var=<?php echo urlencode($myString) ?>">Another Page</a>

 

Then...

 

<?php

  if (isset($_GET['var'])) {
    echo urldecode($_GET['var']);
  }

?>

 

You really only need urlencode because there are spaces in your string.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.