SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
-
Mar 26, 2009, 12:32 #1
- Join Date
- Oct 2008
- Posts
- 263
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP breaking at input with # in it
Hello
I have a form, but in case a field contains hash #, my php code doesn't continue...If i echo the input value where the hash is i get all the part up till the hash, ex. "myword" from an original string which is as "myword#ishere"
Why is this happening?
-
Mar 26, 2009, 13:02 #2
- Join Date
- Feb 2008
- Posts
- 655
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Can you post a bit of code? It sounds very unusual.
-
Mar 26, 2009, 13:34 #3
- Join Date
- Jul 2008
- Posts
- 213
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In PHP the # is used for commenting. So anything after it is not interpreted/displayed. You should filter user input. Look at addslashes and stripslashes
-
Mar 26, 2009, 13:36 #4
- Join Date
- Feb 2008
- Posts
- 655
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Mar 26, 2009, 13:44 #5
- Join Date
- Jan 2005
- Location
- Romania, Iasi
- Posts
- 119
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you're using get switch to post.
url: example.com/script.php?something=bla#other
You'll have $_GET['bla']=bla
if you use post, you'll get $_POST['bla']=bla#other
Should work if you switch to usign POST instead of GET (form method=post)
Not 100% sure but give it a try. Would love to hear back if it worked.
That will happen only when you eval() the input value. There's no reason for echo 'something#else'; to just echo 'something'.
-
Mar 26, 2009, 13:47 #6
- Join Date
- Oct 2008
- Posts
- 263
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In PHP the # is used for commenting. So anything after it is not interpreted/displayed. You should filter user input. Look at addslashes and stripslashes
Can you post a bit of code? It sounds very unusual.
I mean the form is read by jQuery, everything up to the php file seems okay, but when it reached the php file.. that behaviour takes pace..
Even i do simply
Code:echo $_GET['fieldName'];
An additional thing, if i input an &, the code stops functioning..
url: example.com/script.php?something=bla#other
You'll have $_GET['bla']=bla
if you use post, you'll get $_POST['bla']=bla#other
Should work if you switch to usign POST instead of GET (form method=post)
But Im using jQuery $.ajax() function...
-
Mar 26, 2009, 13:57 #7
- Join Date
- Apr 2008
- Location
- North-East, UK.
- Posts
- 6,111
- Mentioned
- 3 Post(s)
- Tagged
- 0 Thread(s)
As you're passing the variables using GET, a # denotes an anchor, therefore you lose the rest of the string.
You need to pass it via POST, which jQuery also supports when using Ajax.
@AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.
-
Mar 26, 2009, 14:07 #8
-
Mar 26, 2009, 14:22 #9
- Join Date
- Oct 2008
- Posts
- 263
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You need to pass it via POST, which jQuery also supports when using Ajax.
im specifying the request method to be "post" though, and still the same..
Or encode # | &
-
Mar 26, 2009, 14:36 #10
- Join Date
- Oct 2008
- Posts
- 263
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Okay solved the #, but the & is still bugging...
-
Mar 26, 2009, 14:52 #11
- Join Date
- May 2006
- Location
- Amsterdam
- Posts
- 206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A simple PHP form will accept the inputs:
"myword#ishere"
"myword&ishere"
"example.com/script.php?something=bla#other"
"example.com/script.php?something=bla&test=test#other"
PHP Code:<?php
if(!empty($_REQUEST['test'])) {
echo $_REQUEST['test'];
}
?>
<form method="post" action="">
<input name="test" type="text" />
<input type="submit" />
</form>
Are you serializing the form data, http://docs.jquery.com/Ajax/serializeArray, before posting it via JQuery?
-
Mar 26, 2009, 14:55 #12
- Join Date
- Oct 2008
- Posts
- 263
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Are you serializing the form data, http://docs.jquery.com/Ajax/serializeArray, before posting it via JQuery?
-
Mar 26, 2009, 17:04 #13
- Join Date
- Mar 2008
- Posts
- 1,149
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Then use escape() or encodeURIComponent() (preferably the latter). (These are not jQuery functions.)
Bookmarks