Jump to content

[SOLVED] Help with passing fields


gorex

Recommended Posts

Hi. I got a problem with passing parameters, and hope for some help. The problem is as below:

 

I got a website, and would like to pass a field from one page to another. So forexample, if this is how the url will look like when someone comes into the site:

 

www.mysite.com?id=xxx

 

Then, there is a coding on the site:

$id = $_GET["id"];

 

and this allows me to get the id parameter and store it for use, so that on links in that page, I can just put:

 

www.mysite.com/linkpage2.php?id=<?echo $id ?>

 

and this will allow me to pass the id=xxx onto another page.

 

 

Now the problem is this. If suppose someone came and for some reason, the id=xxx field is missing. So their url will look like www.mysite.com. Now the $id = $_GET["id"] will give an empty field, and when someone hovers over the link, it will just show www.mysite.com/linkpage2.php?id= . Is there anyway to code it so that if $id is empty, as in this case, then it will automatically fill $id=noid or something similar, so that if someone hovers over the link, it will show www.mysite.com/linkpage2.php?id=noid . I tried putting this line in:

 

if ($id == "") {id == "noid";}

 

but it doesn't work. When hovering over the link, it still shows www.mysite.com/linkpage2.php?id= . Any suggestion/help greatly appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/58912-solved-help-with-passing-fields/
Share on other sites

Hi AndyB, thanks for the help. Below is the code I got at the moment from my index.php file. So:

 

/////////////////////////////////////////////////////

<?php

$id = $_GET["id"];

if ($id == "") {$id == "noid";}

?>

 

<head>..........

.....

.....

<a href="list.php?id=<?echo $id?>">-Read more-</a>

....

....

////////////////////////////////////////

 

So what till happen is when someone comes to my site, www.mysite.com?id=example , $id will be equal to example, and when people hover on the link for list.php, it will read www.mysite.com/list.php?id=example

 

For the second line if ($id == "") {$id == "noid";}, I was hoping that when people come to my site without any ?id, ie. www.mysite.com, then the $id for the list.php link will be replaced with www.mysite.com/list.php?id=noid intead of www.mysite.com/list.php?id= . Hoever, the code doesn't work, and when people hover over the list.php link, it still shows www.mysite.com/list.php?id=.

 

As suggested, I tried putting that code in the list.php page, so on list.php, it will have:

 

/////////////////////////////

<?php

$id = $_GET["id"];

if ($id == "") {$id == "noid";}

?>

 

<head>..........

...

...

/////////////////////////////

 

Unfortunately it doesn't work as well. $id still shows up as nothing.  :(

 

Try using:

 

if ($id == "") {$id = "noid";}

 

rather than:

 

if ($id == "") {$id == "noid";}

 

 

== is the comparison operator - so you use it to compare two values for equality

= is the assignment operator - you use this to give a variable a value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.