tags:

views:

488

answers:

4

Wierd thing is happening when I send data to my database. Anything after the ampersand is dropped as if it were never typed and everything before it is left intact. Can someone please tell me what I should use to preserve this character? REGEX? PHP function?

Thanks

EDIT:

code:

// The POST var contains data entered by the user so could be anything really. This is coming from a WYSIWYG editor.

$string = addslashes($_POST['txtarea']);

mysql_query("INSERT INTO...'$string'");


        WYSIWYG.updateTextArea(n);
        var form = WYSIWYG_Core.findParentNode("FORM", this.getEditor(n));
        if(form == null) {
            alert("Can not submit the content, because no form element found.");
            return;
        }
        form.submit();

I want to echo the string out of what is being submitted. Can someone tell me what I need to place into this case to do that please? Is it alert(n)?


OK guys.. I got it to echo. I'm using an AJAX interface for this and just echo'd the values from there. WHen I add an ampersand in the the string, I get:

test&&

So.... it's not the editor..

+1  A: 

My guess is somehow the '&' is making it, unencoded, into your query string like:

http://yoursite/?message=hello&whatsup

This will resulting in the GET variable "message" Having the value of "hello" and the GET variable "whatsup" having an empty value.

If this is happening, you just need to stop it.

You can encode it as '%26' in the querystring.

Noon Silk
Hi silky, this is a POST var, not GET. Can I still encode with a POST var?
Yes, indeed you can.
Noon Silk
Thanks Silky. I really appreciate it. So that I'm on the same page, you're talking about using urlencode, right?
Yep. I'm not 100% certain this is your problem though, but give it a shot :)
Noon Silk
Thanks Silky.. brb, gonna give it a shot now.
No SIlky.. it encodes everything including the alpha characters. It's a totally unreadable string now. I think I may have to decode it now.
Indeed, decode it on the other side.
Noon Silk
I did... It won't work. It **still** drops the ampersand.
Okay. As per your edited post, can you please print the execute query that is being executed, and show it to us?
Noon Silk
Hey Silky.. Forget the query for a moment because before the data ever goes into the database, the amersand is still missing. The issue is in PHP nt the db.
+1  A: 

I think something is wrong with your WYSIWYG editor.

Salman A
Hmmm.. That may be the case indeed. I'm not so sure how to troubleshoot it. I mean, it is really tightly woven into the HTML. If I could just.. Actually, I have an idea. I'm going to disable the inline javascript tag and I should only be left with a textarea. I should be able to try it like that.
@Jim - that's a great idea, will definitely help isolate the problem.
Dominic Rodger
Hey Dominic. This looks harder than I thought because when I disable the editor the save button goes with it and I can't save it so the data isn't POSTed. Any suggestions?
Hey Dominic, I edited my post above with my echo'd values. It isn;t the editor
+2  A: 

Looks like the bug is with your WYSIWYG editor, not with the insertion of data into the database then. Try capturing the output with JavaScript before the form is posted to be sure.

Dominic Rodger
FANTASTIC IDEA!!!! Thanks Dominic! Thanks and brb.
Hey Dominic, I'm far from a javascript expert. I have the case from the editor source. Can you please tell me where to insert the alert()? I'll post the source abobve
Hi Jim - yep, you want an `alert(n)` call, where n is the value being posted. You want to write a short function that grabs the value being posted, and creates an alert. Call that function `doSomething()` or whatever, then change your form's submit button to look like `<input type="submit" value="Submit" onClick="doSomething();">`
Dominic Rodger
Hmm.. That won't echo anything. I also tried alert(form); and nothing. Am I doing something wrong?
Try `alert('foo');`, just to make sure your function is getting called.
Dominic Rodger
A: 

This may or may not solve your problem, but in general, when ever you need to escape arbitrary characters, you can use PHP's addcslashes, which allows you to define a set or range of characters to escape.

Justin Johnson