/    Sign up×
Community /Pin to ProfileBookmark

pass data to PHP file using URL string

Hi

I just got started with PHP, and have gotten as far as interactively populating and then reading and displaying data from a mySQL DB.

Now, what I want to do is pass parameters to an online PHP file via the URL, to tell the PHP file what data to display. I’ve looked at some reference materials, but they’re either to basic or too advanced for me. Can someone point me toward any sample scripts, both for building the URL string, and for parsing it for processing?

thx!

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 11.2005 — Here's the most basic use:[code=php]<?php

if(isset($_GET['x')){ // url = http://domain.com/file.php?x
print 'this is x';
}else{ // default
print 'this is default';
}

?>[/code]
Copy linkTweet thisAlerts:
@strBeanauthorSep 11.2005 — Thanks. Right after I posted my question, I found a little example that I was able to duplicate.

I'll probably be back with more questions on this!

thx
Copy linkTweet thisAlerts:
@NogDogSep 11.2005 — Here's a basic example to look at:
[code=php]
<?php
$foo = "This is a test";
$bar = 123;
$url = htmlentities("foo=" . urlencode($foo) . "&bar=" . urlencode($bar));
$page = $_SERVER['SCRIPT_NAME'];
$link = "$page?$url";
echo "<p>This is a test: <a href='$link'>Click Me</a></p>n";
if(isset($_GET))
{
foreach($_GET as $key => $val)
{
echo "<p>$key = $val</p>n";
}
}
?>
[/code]

See these links for more info:

http://www.php.net/manual/en/reserved.variables.php#reserved.variables.get

http://www.php.net/urlencode
Copy linkTweet thisAlerts:
@strBeanauthorSep 11.2005 — Hey, thanks! Just about my speed, maybe! I can look up those function names and learn from this.

Another quick question:

If I pass values via URL to a PHP page, can javascript on that page also get those values?
Copy linkTweet thisAlerts:
@strBeanauthorSep 11.2005 — Oh! Another question!

What's the lifetime of the URL string variable? Let's say I've used the URL to pass a query string to the PHP page, which has used it for a parameter against my DB. There is a form on this page for the user to respond. The form's ACTION attribute calls the same PHP page. Now the PHP page is opening again, this time finding form data to process. Is the original URL string variable now toast? Easy to solve by passing the value again via the form, but just curious.
Copy linkTweet thisAlerts:
@NogDogSep 11.2005 — Not sure about accessing the URL via Javascript, but you can use PHP to set values in your page's JS:
<i>
</i>&lt;script type="text/javascript"&gt;
var myvar = "&lt;?php echo $_GET['var1']; ?&gt;";
&lt;/script&gt;

(Or something like that, my JS is pretty rusty these days.)

You could do a similar thing to populate hidden fields in a form to pass the GET values on via the form.
Copy linkTweet thisAlerts:
@strBeanauthorSep 11.2005 — Wow! I can?? That's pow'ful stuff!

thx!
×

Success!

Help @strBean spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...