Leonidus
Leonidus

Reputation: 448

ampersand not passing in a string variable via url

I am trying to pass a big url as a string to another php page and fetch it there via $_REQUEST[] but it's not working. Here is the code (JavaScript) from first page:

var url="index.php?module=Contacts&return_module=Campaigns&action=UD_DetailView&popuptype=detailview&select=enable&form=EditView&form_submit=false";
var intermediate_url="choose_template.php?variable_url="+url;

window.location=intermediate_url;

The code on other page is:

<?php echo "url = ".$_REQUEST['variable_url']; ?>

The echo I get on page choose_template.php is this :

url = index.php?module=Contacts

I don't understand. Why does it break at the occurance of first & ? What should I do to bypass it ?

Upvotes: 1

Views: 1290

Answers (4)

Warren
Warren

Reputation: 163

Try using urlencode():

var url="<?php echo urlencode('index.php?module=Contacts&return_module=Campaigns&action=UD_DetailView&popuptype=detailview&select=enable&form=EditView&form_submit=false')?>";

var intermediate_url="choose_template.php??variable_url="+url;

window.location=intermediate_url;

Upvotes: 1

faq
faq

Reputation: 3076

If i understand correctly you what to echo out the full name of the current page: Use this to get it:

function curpage() {
    $pu = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pu .= "s";}
    $pu .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
    $pu .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {$pu .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];}
    return $pu;
}

<?php echo "url = ".curpage(); ?>

This will alway work fine!

Upvotes: 0

Develoger
Develoger

Reputation: 3998

Try using in js:

url = encodeURIComponent(url);

Than in php:

echo "url = ". urldecode($_REQUEST['variable_url']);

Upvotes: 6

Lorenzo Marcon
Lorenzo Marcon

Reputation: 8169

When manipulating url, you should use urlencode and urldecode functions

http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php

In javascript:

url = encodeURIComponent(url)

Upvotes: 2

Related Questions