1

I am trying to send a form to a PHP file via jQuery. The problem is, that the content, which has to be sent to the PHP file, contains slashes (/) since there is BBcode inside. So I tried the following:

$.ajax( 
    { 
        type: "POST", 
        url: "create.php", 
        data: "content=" + encodeURIComponent(content),
        cache: false,
        success: function(message)
        {   
            $("#somediv").html(message);
        }               
  });

In the PHP file I use rawurldecode() to decode the content and get my BBcodes back which I can then transform into HTML. The problem is as soon as I put the encodeURIComponent() it will output: [object HTMLTextAreaElement]

What does that mean, where is my mistake?

vascowhite
  • 18,120
  • 9
  • 61
  • 77
Chris
  • 6,093
  • 11
  • 42
  • 55
  • So, let me clarify: `[object HTMLTextAreaElement]` is what is contained in `#somediv`? That sounds crazy to me and that's what your question implies, so I thought I'd ask. – Levi Morrison Nov 18 '11 at 01:54
  • can you show what is represented by the `content` variable? – vascowhite Nov 18 '11 at 01:55
  • the content variable should contain the content of the textfield with the ID "content". – Chris Nov 18 '11 at 02:01

3 Answers3

5

Package your data as an object literal, and and let jquery worry about the dirty details:

// javascript
 $.post(
      "create.php",
      {
           content: "here is my content / slashes included."
      },
      function( message ) {
           $("#somediv").html( message );
      }
 )


 ///////////////////////////////////////////////

 // php
 $formData = $_POST["content"];
 echo $formData;
 // yields: here is my content / slashes included.
Christopher
  • 764
  • 4
  • 6
  • And of course, as the other folks have said, make sure you've got the actual content of the element in question when you prepare to post(), *not* the element itself. – Christopher Nov 18 '11 at 02:03
  • Now its the same thing as at the very beginning. If my content is: [b]something[b] it is ok, but if it is [b]something[/b] it doesnt show anything. This is my code now, should be working or not? `var content = $("#content").val(); $.post( "create_preview.php", { type: "POST", content: content }, function(message) { $("#previewdiv").html(message); } )` – Chris Nov 18 '11 at 02:11
  • Could you post the relevant portion of your php script? – Christopher Nov 18 '11 at 02:15
  • My php is just: `echo $bbcode->parse_bbcode($_POST["content"]);` – Chris Nov 18 '11 at 02:17
  • What's parse_bbcode() actually doing? Is it stripping out stuff enclosed by tags? What's the resulting output if you post a string like "something within tag alongside something outside of tag"? – Christopher Nov 18 '11 at 02:20
  • Omg now it is working with ur code! Guess what was the problem: I call the function in the content textfield with onchange. And I have buttons adding bbcode to the textfield. The problem is that adding bbcode to the textfield via mouse click seems not to count as "onchange". In fact I have to put the bbcode inside and then add another letter for the function to get called. So now it works, thanks alot! – Chris Nov 18 '11 at 02:27
1

your content variable is a textarea element, not the text inside the textarea (if that's what you're looking for, since you didn't state what content is)

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
0

Not too sure, but the value of your variable content appears to be an HTML element as opposed to the contents of that HTML element.

Elsewhere in your code you may have something like:

var content = $("selector");

...but it should be:

var content = $("selector").val();

Can't tell for sure without seeing the rest of your code, but that's what it seems like to me.

Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
  • content is the ID of the textfield. Ive put var content = $("#content").text(); in the script, now the weird message doesnt appear anymore but there is no output at all – Chris Nov 18 '11 at 02:00
  • 1
    use $("#content").val() instead. – Christopher Nov 18 '11 at 02:04
  • Now its the same thing as at the very beginning. If my content is: [b]something[b] it is ok, but if it is [b]something[/b] it doesnt show anything – Chris Nov 18 '11 at 02:12