Use get() in link

I have a couple of links in a unordered list. When the user click a link, some text should be displayed on the same page as the link.

Now, this works:

<a href=my_page.php?var1=Hello!>Hello again!</a>

But I want the link to display text contained in a variable. Something like this:

<?php
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse et tempus risus. Mauris adipiscing consequat odio, eleifend lobortis velit hendrerit sit amet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eu lectus in elit tempor porttitor at eu mi. In ullamcorper semper rhoncus. Quisque id varius dolor. Morbi congue luctus rhoncus. Sed scelerisque posuere vulputate. Nullam at libero turpis, non bibendum odio. Nulla facilisi. Morbi hendrerit nulla eget metus varius in aliquam tortor mattis. Nullam faucibus massa a ligula dictum id commodo metus convallis. Phasellus orci est, malesuada a varius sed, congue at dolor. ';
$text = $_GET[“$text”];
echo “$text”;
?>

<a href=my_page.php?$text>Link number 1</a>

Of course this does not work. What am I doing wrong? All help appreciated!

It’s a pretty bad idea to allow user supplied data to be presented by your application without filtering it.

Try something more along these lines… :wink:


<?php
$messages = array(
    'hello'     => 'Hello there!',
    'goodbye'   => 'Bye bye, come again!'
);

$message = (false === empty($_GET['message']) && true === array_key_exists('message', $_GET)) ? $messages[$_GET['message']] : $messages['hello'] ;
?>

<h6>
    <?php echo $message; ?>
</h6>

<ul>
    <?php foreach(array_keys($messages) as $message): ?>
        <li>
            <a href="?message=<?php echo $message; ?>">
                Click
            </a>
        </li>
    <?php endforeach; ?>
</ul>

The preferred method is to store all the actual data in a database and then retrieve it by parsing your get() value as the id of each row of data.

I am afraid I don’t understand this:

$message = (false === empty($_GET[‘message’]) && true === array_key_exists(‘message’, $_GET)) ? $messages[$_GET[‘message’]] : $messages[‘hello’] ;

Same as:

	$message = $messages['hello'];
	
	if (false === empty($_GET['message']) && true === array_key_exists('message', $_GET))
	{
		$message = $messages[$_GET['message']];
	}

Thank you very much! That was claryfying. One thing: I have seen = and ==, but never ===. What does === mean?

Means identical, http://www.php.net/manual/en/language.operators.comparison.php

Thanks!
This script gives you a list like this:

click
click
etc

It works, but I want my list to look more like:

javascript
php
xhtml

How do I change the script so it reflects that change? And one more, maye stupid question. In the script somtimes there’s a questionmark:

a href="?message

What does that mean?

bump

? indicates the start of the query string

Anything after a ? in a URL string is passed as variables (In PHP, those variables are stored in the $_GET superglobal array).

== is not technically the same as === ; Because of typing, 0 == false is a true comparison, but 0 === false is not, because the types are different (Int vs Boolean)

As for “This script gives you a list like click click click”… compare it with this:

<?php
$messages = array(
    'hello'     => 'Hello there!',
    'goodbye'   => 'Bye bye, come again!'
);

$links = array(
   'hello' => 'Say Hello',
   'goodbye' => 'Say Goodbye'
);

$message = (false === empty($_GET['message']) && true === array_key_exists('message', $_GET)) ? $messages[$_GET['message']] : $messages['hello'] ;
?>

<h6>
    <?php echo $message; ?>
</h6>

<ul>
    <?php foreach(array_keys($messages) as $message): ?>
        <li>
            <a href="?message=<?php echo $message; ?>">
                <?php echo $links[$message]; ?>
            </a>
        </li>
    <?php endforeach; ?>
</ul> 

Thank you very much, both of you!

I have been doing some experimenting and have stumbeled on this:

<h6> <?php echo $message; ?></h6>

I would like to print the $message variable under the list, not over it. How do I accomplish that? CSS?

‘Under’ the list… as in after it?

Move the h6 block (Everything from <h6> to </h6> ) down below the </ul>.

I tried that, but then it will only echo the last item in the array.

Yeah… we did a bad thing and reused a variable name.


<?php
$messages = array(
    'hello'     => 'Hello there!',
    'goodbye'   => 'Bye bye, come again!'
);

$links = array(
   'hello' => 'Say Hello',
   'goodbye' => 'Say Goodbye'
);

$message = (false === empty($_GET['message']) && true === array_key_exists('message', $_GET)) ? $messages[$_GET['message']] : $messages['hello'] ;
?>
<ul>
    <?php foreach(array_keys($messages) as $message2): ?>
        <li>
            <a href="?message=<?php echo $message2; ?>">
                <?php echo $links[$message2]; ?>
            </a>
        </li>
    <?php endforeach; ?>
</ul>

<h6>
    <?php echo $message; ?>
</h6>

Because we used $message as our foreach variable, it was getting overwritten.

Thanks StarLion, i made the changes and now it works, though I am not sure I understand why… :slight_smile: This is complicated stuff.

What does this mean:

$message = $messages[$_GET[‘message’]];

Follow the variable, and read up on arrays in the manual.

Say you had some links oin a page:
<a href =“linktest.php?message=mymessage”>mymessage</a>
<a href =“linktest.php?message=myothermessage”>myothermessage</a>
linktest.php


<?php
echo 'message from previous page was : '$_GET['message'] ;

$messages = array(
'mymessage' => "this is a message" ,
'myothermessage' => "this is another message"
);

// check what is in your array 
var_dump ( $messages );

// assign a variable to the chosen array key
$message = $messages[$_GET['message']];

echo $message ;
?>

Thanks! Does php have something comparable to target in html? I got a php-page. Header, navigation and footer are included files. Can I do the following:

when I click a link in the navigation, the information in the content area is supposed to be updated. I guess it means that I have to dynamically load a php page into the conte area using include.

Ok, I did figure out that myself.

I now have working links. But how do one solve this problem? Let’s say my content area shows content.php because I am using <?php include(“content.php”);?>
Now: If I click a link in the nav area, I want to substitue that content.php with another php file. I have been doing some experimenting but if I include another file into the content area, the content.php will show both php files instead of the file that my link points to.