当前位置: 动力学知识库 > 问答 > 编程问答 >

php - line breaks inside of a textarea

问题描述:

I am having something strange happen.

My script is for editing information.

When I send the $_POST from my textarea to my php page. It sanitizes and inserts correctly, but I also want to re-output the data if called upon again. So i am basically using jquery $post to send and retrieve the information.

It all works great, except when I call the info back with the edit button and function "onmousedown" it is not giving me the correct format for my parameters.

If I only write on one line it comes out perfect, but if I use multiple lines it won't retain the line breaks and it chops off the closing " ' " so the function cannot be reset. Any ideas? Here is my callback echo. Thanks in advance...

The problem is in the "$againList" parameter...

$againList = $_POST['updated_songList']; // I am sending back the exact same data that was given without filter...

echo '<span><h4 style="margin-bottom:0px; font-size:14px;">'.$full_birthday.'

<a href="#" onclick="deleteSongList('.$songid.');" style="color:gray; float:right; font-weight:100; font-size:12px;">delete</a>

<a href="#" onclick="return false" onmousedown="editSongList(\''.$songid.'\', \''.$againList.'\');" style="margin-right:5px; color:gray; float:right; font-weight:100; font-size:12px;">edit</a></h4>'.$song_list.'<br /></span>';

If you need anything else let me know. Thanks!

网友答案:

Hmm, php has a nl2br() function that converts new line to breaks but I'm not sure that's what you are after. Does the linebreaks dissapear before or after they come to the php script?

Here is how to use nl2br();

$var = nl2br($_POST['myValue']);
网友答案:

Okay, so I figured it out and got it running. Thanks for all the help everyone!

The problem seems to be that you must at least add the "stripslashes" function to a $_POST before you can place it into a javascript parameter otherwise, I seems like at every '\n' it stops the function call and give the error "there is an unterminated string literal" meaning your last parameter does not have an " ' " at the end of it. I am not sure why though. If anyone has any insight, I would appreciate the learning experience.

Thanks everyone!

网友答案:

Try this :

$againList = nl2br($_POST['updated_songList']);

Basically, all textarea newlines are "\n" which HTML doesn't show. nl2br() just converts them into <br/> while displaying them. The newlines remain intact.

For the script that removes the "'" from the end, unless you show that script, you can't be helped.

分享给朋友:
您可能感兴趣的文章:
随机阅读: