1

Why does the jquery gets disabled when I print data with line breaks from mysql? The problem here is that all the functions in javascript and js are disabled and wasn't able to do their task

For example from mysql I have this data:

You
are
good
in
english

and then I printed it like this:

<p id = "messagebody" style ="white-space:pre">  <?php echo htmlspecialchars($row['d_body']);?></p>

But when I have a data like this:

You are good in english

The jquery works! How come?

Any ideas?

UPDATE

I have this sample code here:

$mybody = "Hello
           User";

also this one:

$mybody = "Hello \n User";

But both doesn't work!

But if it's:

$mybody = "Hello User";

It worked!

both are used by this;

<p id = "messagebody" style ="white-space:pre">  <?php echo $mybody;?></p>

Also have this button:

<button id = "<?php echo $rowcomment['comment_id']; ?>" class = "editcom"> Edit</button >

JQUERY:

    $(document).on("click",".editcom",function(event){
        var thisid = event.currentTarget.id; 
        alert(thisid);
    });
Makudex
  • 1,042
  • 4
  • 15
  • 40

2 Answers2

1

I faced this issues long back :)

When you write

var response = "You
are                   // ??
good
in
english

Look at the code how it broken ? The latter 4 lines considered as script and script engine trying to evaluate them and hence it broke.

Where as the

var response = "You are good in english"

Is sweet for script engine. That's a string variable with sweet data. You can see why it broken.

What you can do is replace all magical symbols between those words before assigning to any script variable.

Solution I have that time to remove all magical cartridges between words is

https://stackoverflow.com/a/25681807/1927832

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • I'm not assigning it to script. I just displayed it and then the button wouldn't work – Makudex Sep 13 '15 at 07:03
  • @Makudex I mean placing that string on DOM :) weather the script or as a text on button. – Suresh Atta Sep 13 '15 at 07:04
  • As you can see the data with line breaks in every word is from the DB so I don't know what to do – Makudex Sep 13 '15 at 07:04
  • @Makudex As I said, make you sentence cartridge free. – Suresh Atta Sep 13 '15 at 07:05
  • Actually I have no problem with the button itself, if and only if the data retrieved from db is not broken lines. However if it is broken line, it prints but the buttons and other html dom used in jquery/javascript get disabled. How would I resolve that? :( – Makudex Sep 13 '15 at 07:05
  • Values are from db. Please could you give me some idea on how to do that? – Makudex Sep 13 '15 at 07:06
  • Another thing, tried to use cartridge a while ago but this time manually like this: `$mybody = "Hello \n Wendell";` but didn't work. I'm stuck with this! – Makudex Sep 13 '15 at 07:08
  • @Makudex Don't ech it directly. Replace and then assign it – Suresh Atta Sep 13 '15 at 07:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89503/discussion-between-ss--and-makudex). – Suresh Atta Sep 13 '15 at 07:12
1

Finally! The problem has been solved which took me almost 6 hours thinking what could possibly be the the solution.

Just did something on my code like this:

$mybody = htmlspecialchars("Hello
                WOrld"); 
 $mybody = preg_replace("#\r|\n#", "<br>", $mybody);

Just replaced the \r or \n to <br> and it's all SET!

Thanks you guys, I'm able to figure out these \r and \n.

Makudex
  • 1,042
  • 4
  • 15
  • 40