eveo
eveo

Reputation: 2833

PHP echo $_SERVER['PHP_SELF'] with added variable?

Just want to do $_SERVER['PHP_SELF'] as a link with ?logout=1 appended to it.

<a href="<?php echo ''$_SERVER['PHP_SELF']'.logout=1' ?>" id="add"><input type="button" value="LOGOUT" /></a>

gives

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in F:\export\srv\www\vhosts\main\htdocs\php\assign3\m_a2_functions.php on line 90

Upvotes: 5

Views: 16084

Answers (6)

Paul
Paul

Reputation: 141829

Change:

<?php echo ''$_SERVER['PHP_SELF']'.logout=1' ?>

To:

<?php echo $_SERVER['PHP_SELF'], '?logout=1' ?>

Upvotes: 5

Sebasti&#225;n Grignoli
Sebasti&#225;n Grignoli

Reputation: 33432

You are defining an anchor (<a>) and putting a button inside it, but the button will not send you to that url. You should make the "onclick" event of the button redirect the user to the page you want.

Also, if you just want to add a variable to the current URL you don't really need to use PHP_SELF, the browser will know what to do.

<input type="button" value="LOGOUT" onclick="window.location.href='?logout=1';"/>

Upvotes: 0

Sampson
Sampson

Reputation: 268344

This may be a place where printf() would come in handy.

<a href="<?php printf( '%s?logout=1', $_SERVER['PHP_SELF'] ); ?>">Foo</a>

This cuts down on the number of times we hop in and out of a string.

Upvotes: 1

Ali Hamze
Ali Hamze

Reputation: 1590

have you tried:

<?php echo($_SERVER['PHP_SELF'] . "?logout=1") ?>

Upvotes: 2

JT Smith
JT Smith

Reputation: 741

<a href="<?php echo $_SERVER['PHP_SELF'] . '?logout=1' ?>" id="add">

Upvotes: 2

Mike B
Mike B

Reputation: 32155

Your quotes are all over the place:

<a href="<?php echo $_SERVER['PHP_SELF'] . '?logout=1'; ?>" id="add"><input type="button" value="LOGOUT" /></a>

Upvotes: 1

Related Questions