1

I am creating a insertion query and retrieving the query by php and setup the data to input form using jQuery. but there is a new line issue which showing jQuery error and data not showing in input form. the sql insert query was :

INSERT INTO challan (to_messers,challan_date,address,transportation_type,trailer_no,driver_name,in_outstatus,vessel_name,rotation_no,voyage_no,yard_no,country_name,gate_no,remarks) VALUES ("Marks textile","2016-01-15","Welcome","CCTCL","DME-233000","Maksud","GateIn","KOTA AKBAR","2015/2069","X","Yard-1","16","Gate-4","test text\r\nweldone")

After inserting when I retrieving the data and want to show it using jquery like below one

$('textarea[name=descript]').html("<?php echo $remarks; ?>");

It will be broken down to new line. check the image enter image description here

Also mysql database input data is showing like below image enter image description here

The text should be like this one.

"test text\r\nweldone"

I actually didn't understand quite cleary this part. if I change the input to double slash. "test text\r\nweldone"

than it will ok and show the format correctly like "test text\r\nweldone"

Please help me on this matter how can my sql query have \\r\\n slash.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • If you're referring to why you have to query it like `\\r\\n` then it's due to the fact that a single backslash is used for escaping things in PHP. – Matt Jan 15 '16 at 04:20
  • I m using raw mysql_query() function for this query. so how php is escaping it here? – Md Emrul Easir Jan 15 '16 at 04:34

2 Answers2

1

When you echo the string you want it to correctly preserve the lines using json_encode. You can resolve the issue by changing this:

$('textarea[name=descript]').html("<?php echo $remarks; ?>");

To this:

$('textarea[name=descript]').html(<?php echo json_encode($remarks); ?>);

Read more on how to do multiline strings in JavaScript here: https://stackoverflow.com/a/6247331/747678

Read more on json_encode here: http://php.net/json_encode

json_encode — Returns the JSON representation of a value

Usage: string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

Community
  • 1
  • 1
Clay
  • 4,700
  • 3
  • 33
  • 49
  • Its working. but Now I get content with double quote. like - "test text weldone" in the textarea input field – Md Emrul Easir Jan 15 '16 at 04:29
  • ah, that's right, we need to remove the double quotes for the html() call... look at my updated post. Needs to be `html();` .. json_encode will add the double quotes and make it JavaScript safe. – Clay Jan 15 '16 at 04:30
  • Yahoo! its working well. Great help.You are a life saver man. but didn't understand why input is showing that way in phpmyadmin edit section. Many thanks. – Md Emrul Easir Jan 15 '16 at 04:49
1

\ is used to escape a special character in javascript and mysql

In your javascript, the first backslash \ allows the special character \, which is the second backslash to present in the string.

I think in mysql you can just try with \\\, which allows you to escape the \\

Conta
  • 565
  • 1
  • 4
  • 12