Forum Moderators: coopster

Message Too Old, No Replies

ampersand in URL, urldecode, htmlspecialchars decode

         

drooh

3:22 pm on Jun 24, 2010 (gmt 0)

10+ Year Member



file.php?file=this&that

how do i receive this as this&that

ive tried urldecode($file) and htmlspecialchars_decode($file)

L33t_J0rdan

3:39 pm on Jun 24, 2010 (gmt 0)

10+ Year Member



Could you post the code please? I don't get what you're trying to say.

EDIT: I think you're trying to say %26?

drooh

4:09 pm on Jun 24, 2010 (gmt 0)

10+ Year Member



yes!, how do i get %26 ?

rocknbil

4:50 pm on Jun 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how do i receive this as this&that


When you say receive, do you mean as in receive from a get? Or are you reading it in from a file, XML feed, or page capture?

If it's from a get, PHP should already parse that for you. & is required for valid (X)HTML, if you're reading it into a PHP script for HTML output, don't change it, leave it alone. It will be fine.

Maybe a better overview of what you're trying to do with this string would be helpful.

drooh

5:07 pm on Jun 24, 2010 (gmt 0)

10+ Year Member



i mean how do i convert & to %26

enigma1

5:18 pm on Jun 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
echo rawurlencode('&');
?>

drooh

5:34 pm on Jun 24, 2010 (gmt 0)

10+ Year Member



very weird, ive got

action=\"javascript:del_sec_con('".rawurlencode($file)."','this file');\">

then my javascript
<script language="JavaScript">
function del_sec_con(file,copy)
{
if (confirm("Are you sure you want to clear" +' ' + '-' + copy + '-'))
{
window.location.href = 'deletefile.php?file=' + file;
}
}
</script>

then when it goes, it looks like JavaScript converts the %26 back to &

drooh

5:42 pm on Jun 24, 2010 (gmt 0)

10+ Year Member



got it

rawurlencode(rawurlencode($file))

Matthew1980

6:19 pm on Jun 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Could you not just do:-

str_replace("&amp;","&",$file) as opposed to double rawurlencode() ?

This is just in that javascript instruction isn't it, not an actual URL string, because URL's should remain as &amp; or the validator plays up, been a long time since I did a plain old ampersand ;)

Or am I missing the point, so long as it works ;)

[EDIT:] As Rocknbil enquires, what is going wrong when you have is as the standard URL string? There should be no interference at all, I use similar things and leave the &amp; there for validity purposes.

Cheers,
MRb

rocknbil

4:50 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, we're getting closer. :-) Here's a very important clue.

action=\"javascri.....

and the other clue,

window.location.href = 'deletefile.php?file=' + file;

I can tell you, you're going about this the hard way. You're using whatever value is in file (or the file name, or whatever) and trying to set that as a query string parameter in your form action, which is a bad idea in the first place (not bad, just . . more unweildy.) Then you use JS to post to a location in a query string. Given what you've told us so far, see if this will help you. If it does, a lot of your problem will go away.


<?php
// Do whatever you do to get $file. No need to encode, decode, or anything.
echo '
<form action="deletefile.php" method="post" onsubmit="return verifyDelete(\'' . $file . '\');">
<input type="hidden" name="file" value="' . $file . '">
<input type="submit" value="delete ' . $file . '">
</form>
<script type="text/javascript">
function verifyDelete(file) {
var ok = confirm('Delete file ' + file + '?');
return ok;
}
</script>
'; // end echo
?>


The only difference now is in deletefile.php will look for $_POST['file'], not $_GET['file'].

drooh

7:45 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



@rocknbil that looks good and i know that is the way to do it. In my scenario I actually have an icon of a trash can that you click on, i suppose you could just use an image as the submit button?

rocknbil

11:14 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, hindsight is 20/20 . . . since my echo block is single quoted, I have an error, instead of

var ok = confirm('Delete file ' + file + '?');

use

var ok = confirm(\'Delete file \' + file + \'?\');

use an image as the submit button?


Three ways, the third with javascript dependence.

Use CSS:

.submitButton { background: url(submit-image.gif) top left no-repeat; text-indent:-5000px; }

<input type="submit" class="submitButton" value="Submit">

The text indent should make the text invisible.

Input type image:

<input type="image" src="submit-image.gif">

Being an input type, it's a form control (and will submit an x and y coordinate for the exact coordinates of your click.) But as a link, anchors aren't form controls, so you need Javascript to reference the form:

Last of all, but used a lot, a javascript dependent anchor.

<a href="#" onclick="document.forms[0].submit(); return false;"><img src="submit-image.gif"></a>

If there's multiple forms in the document, you have to be sure to reference the right one, by id, name, or index in the document.