1

jQuery front end, php/mySql back end... I'm passing in a text area - scrubbing it for htmlspecialchars() and doing mysql_real_escape_string() everything goes in fine, but when I try to pull it out and 'edit' it in the same form, the jQuery ceases to work - to the point where the modal window wont even pop up...

everything works as expected - UNTIL i put a carriage return in the text area....

I'm using this js to populate the form fields...

// loop and populate - must have matching field names to key names
$.each( data, function( key, value ) {
    $( '#' + key ).val( value );
}); 

I'm using a JSON call to populate the edit form...and the JSON is coming back with the carriage return... so the issue isn't on the back end...

JSON

{ "id":"12", "for_customer_id":"18","customer_id":"20", "engagement_label":"", "part_number":"asdwew", "part_description":"wwe wew  wew", "defect_description":" asd asd asd asd as ", "notification_date":"01/01/2013", "notification_timeCTZ":"3pm", "emp_training_on_file":"Yes", "work_instructions":"hhhllkjijj asd  asd  a sd a
new row", "supervisor_id":"25", "start_date":"", "end_date":"", "date_completed":"" }

"work instructions" - where it says 'new row' is right after CR.

what am I missing??? THX

I may have chosen the "wrong" way to do this - but I decided to do it on the server in PHP... however even though I'm replacing chrs correctly, JSON still seems to have a problem consuming.... where can I find the needed info for JSON and carriage returns... HELP! thx.

MY PHP putting to the db is scrubbed...based on some reading - I've tried 1,2 and 3 slashes () (this is a function to format the string before it gets to the SQL statement)

function parse( $text ){
$parsedText = str_replace( chr(10), '', $parsedText );
return str_replace( chr(13), '\\\n', $parsedText );
}

MY PHP coming out - again based on some reading... I'm making my own JSON due to a specific data structure needed... (this is a function to format the string before it gets put in the JSON)

function parseString( $string ) {//function to make JSON CR and the like suitable for comsumption 
$string = str_replace( '\\', '\\\\', $string );
$string = str_replace( '/', '\\/', $string );
$string = str_replace( '"', '\\'.'"', $string );
$string = str_replace( '\b', '\\b', $string );
$string = str_replace( '\t', '\\t', $string );
$string = str_replace( '\n', '\\n', $string );
$string = str_replace( '\f', '\\f', $string );
$string = str_replace( '\r', '\\r', $string );
$string = str_replace( '\u', '\\u', $string );
return $string;
}
j-p
  • 3,698
  • 9
  • 50
  • 93
  • Similar question: http://stackoverflow.com/questions/4253367/how-to-escape-a-json-string-containing-newline-characters-using-javascript – darshanags Feb 18 '13 at 02:03
  • @ darshanags - thx - but I must be missing something - stil doesn't work. – j-p Feb 18 '13 at 03:12
  • What is the special structure which cannot be supplied by `json_encode()`? – Michael Berkowski Feb 18 '13 at 03:26
  • basically some meta data about the data... usually the data (row type repeating structure) is nested unside an array that contains the 'base' data - when I started fooling around with json_encode, it always returned stuff in the single looped array - which is great. I didn't have time to dink with it to see if it could do specifically what I needed - it was easier to write what I needed - but I'm obviously a noob and am finding holes. PHP is not my primary, and sometimes I feel way out of water – j-p Feb 18 '13 at 05:12
  • I've got it working... seems there was a misunderstanding on the \n and the \\n... – j-p Feb 18 '13 at 05:13
  • Glad it worked out. I was under the impression that you were using `json_encode`. Rule of thumb: it is a bad idea to generate json manually. I don't see a problem in using `json_encode` with nested arrays so give it a go! your code will become much more stable. – darshanags Feb 18 '13 at 05:28
  • @darsh, I agree, and when I actually have tim eto dig and futz - If I can get it to work, i'll go back and change the custom stuff... right now tho I can't waste a week just to find it can't do what I need... client's timeline currently mandates a rush. – j-p Feb 18 '13 at 07:05

0 Answers0