user1023395
user1023395

Reputation: 263

echo certain text from php links

I have:

<a href="index.php?mypage=one">one</a>

And:

<a href="index.php?mypage=two">two</a>

PHP page:

<?php
if mypage=one then 
echo "my stuff text........one";

if mypage=two then
echo "my stuff text........two";
?>

I want to get text separately for each link from same php page

Upvotes: 1

Views: 84

Answers (2)

phihag
phihag

Reputation: 287885

Umm, that php is not even remotely valid code. You want a switch statement:

<?php
$mypage = isset($_GET['mypage']) ? $_GET['mypage'] : '';
switch ($mypage) {
case 'one':
    echo "my stuff text........one";
    break;
case 'two':
    echo "my stuff text........two";
    break;
default:
    header('HTTP/1.0 404 Not Found');
    echo 'This page does not exist';
}

Upvotes: 2

Samar
Samar

Reputation: 1955

First of all, if then construct is not available in PHP so your code is syntactically wrong. The use of switch as suggested already is a good way to go. However, for your problem, you should use $_GET['mypage'] instead of $_POST['mypage']. It seems you are beginning PHP. Once you get some good basics, you will probably be making use of the functions such as include() and require(). Make sure you do not make mistakes beginners do:

<?php
if (isset($_GET['mypage'])
{
    @include($_GET['mypage']);
}
?>

The above code works and looks simple but it has a very dangerous implementation allowing the malicious users to perform an attack known as file inclusion attack. So you should try to use the switch statements such as:

<?php
$mypage = $_GET['mypage']; //also you might want to cleanup $mypage variable
switch($mypage)
{
    case "one":
        @include("one.php");
        break;

    case "two":
        @include("two.php");
        break;

    default:
        @include("404.php");
}
?>

Upvotes: 2

Related Questions