Jump to content

Need urlencode help


spinman

Recommended Posts

Hi,

 

As a relative newbie, I am trying to create a link on one page to another page on my site using urlencode to encode the URL (together with a little javascript onclick code).  The problem I have is that when I run the code using urlencode(), I get a 403 permission denied error. If I run the same thing without urlencode it works find.

 

I tried this both on my locally installed wamp setup as well as on my hosted site. The code (which I have simplified as much as possible for this question) is:

 

<?php

//This is the "go_from.php" page 
// Run this and then click one of the buttons

$url=urlencode("go_to.php?var=1");
$url1="go_to.php?var=1";

$text .=
"<form method='get'>
<div><input type='button' value='Select the Go To Page WITH encoded URL'  onclick=\"javascript:window.location='$url';\"></div>
<div><input type='button' value='Select the Go To Page WITHOUT encoded URL'  onclick=\"javascript:window.location='$url1';\"></div>
</form>";

echo $text;
?>

 

...and it is calling this page:

 

<?php
//this is the "go_to.php" page which is called by "go_from.php"

echo "This is the go to page";
echo $_GET['var'];

?>

 

Any ideas as to why the top button errors out?

 

Thanks in advance,

 

Dennis

 

 

PS: Correction - I used GET (not POST)

 

Link to comment
Share on other sites

The "?" in a URL should not be url encoded.  The parts to encode are the values of each get variable.  In your case, you would encode the value "1" (which just encodes to itself).

 

But if you wanted to set "var=&" for example, you would need to urlencode the "&"

Link to comment
Share on other sites

Thanks for clearing that up for me!

 

So if I understand you correctly, if I needed to build an encoded URL which passes multiple variables, I would do something like:

<?php
$url="go_to.php?var1=" . urlencode($var1) .
             "&var2=" . urlencode($var2) .
             "&var3=" . urlencode($var3);
?>

 

This works fine, I was just hoping for an easier way to convert a url in one go, but thanks in any case!

 

Dennis

 

Link to comment
Share on other sites

Yep, that's basically it.  I don't know of any simple way to do that encoding.

 

About the &, you may not need that if you are setting the url using window.location.  I'm not sure about that.  The whole thing about when you need & kinda confuses me actually :)  If it doesn't work, try it as just plain "&"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.