www.webdeveloper.com
Results 1 to 5 of 5

Thread: PHP GET URL String with "&"

  1. #1
    Join Date
    Dec 2004
    Posts
    47

    PHP GET URL String with "&"

    Hi,

    I am using PHP get and creating a URL string with data variables retrieved from a mySQL table. My problem is that some values have a "&" included in their text. As we know the "&" is used to separate variables in the URL string and therefore my value being passed is truncted at the "&".

    Does ayone know of a way to pass the variable so tht the whole string is read as the variable - including the "&". I have tries enclosing in separate quotes, brackets etc. but nothing seems to work.

    In my PHP code I echo the following:
    <? echo "<a href='$PHP_SELF?cmd=update&data1=$data1&data2=$data2'>update</a>"; ?>
    where for example the element $data1 has an "&" in the text e.g. Apples & Oranges.

    Many Thanks
    Paulb

  2. #2
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Use the urlencode() function.

  3. #3
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    A & can be replaced with &#37;38 in a url.

  4. #4
    Join Date
    Dec 2004
    Posts
    47
    Thanks for your response and I have been looking around at how to use the URLENCODE() function but cannot really work out how to do it. Are you able to give me an example using my sample URL in my original post.

    Thanks and appreciate your help - I'm a newbie to PHP!!

  5. #5
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    After looking at your sample url closer, you don't have what you think you have.

    You don't need urlencode()

    ?cmd=update&data1=$data1&data2=$data2 //three "get" variables are being passed in the url.

    means;

    PHP Code:
    $data1 "apples";
    $data2 "oranges";

    $var1 $_GET['cmd'];  //   = update
    $var2 $_GET['data1'];  //  = apples
    $var3 $_GET['data2'];  // = oranges

    // the url has three variables not one, and the it would be better to express it this way

    $data1 "apples";
    $data2 "oranges";
    $url $_SERVER['PHP_SELF']."?cmd=update&data1=".$data1."&data2=".$data2;
    echo 
    "<a href=".$url.">update</a>";   //  http://www.somewebsite.com/index.php?cmd=update&data1=apples&data2=oranges 
    However if the variables $data1 had more than simple "apples" or "oranges" for content. Such as "apples and oranges" then you would need to handle that situation with urlencode() which would replace the spaces with + signs. & is an expected variable separator and should not be used in any other context. I suppose &#37;38 would work there. You could use str_replace() for that.
    Last edited by Dasher; 04-11-2012 at 07:25 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles