PHP MySQL Fill Textarea with Text that contains newlines using jQuery
I have a form that users fill out which contains a textarea. This form is uploaded to a MySQL database, and then later the user can edit the info. The issue I am having is that when I use AJAX to suggest data for the textarea, javascript gives a "Invalid or unexpected token" error wherever a newline appears.
In the suggestion box it is a list with rows like so...
<li onClick="selectBenefiting('$benefiting')">$benefiting</li>
And selectBenefiting() is written like this...
function selectBenefiting(val) {
$("#benefiting").val(val);
$("#benefiting-sugg").hide();
}
Can anyone help me achieve this while preserving the line breaks? I have been searching for hours, and while I see other people with the same issue, I have yet to find a solution.
As I've said, it is a MySQL database. If I echo the data in the textarea using PHP it displays correctly, so I know it is in the database correctly. The issue is when using jQuery and AJAX to attempt to populate the textarea.
Any help would be greatly appreciated.
UPDATE:
$benefiting_js = str_replace("rn", "\r\n", $benefiting);
Slightly modified solution from Nick did the trick.
javascript php jquery html mysql
add a comment |
I have a form that users fill out which contains a textarea. This form is uploaded to a MySQL database, and then later the user can edit the info. The issue I am having is that when I use AJAX to suggest data for the textarea, javascript gives a "Invalid or unexpected token" error wherever a newline appears.
In the suggestion box it is a list with rows like so...
<li onClick="selectBenefiting('$benefiting')">$benefiting</li>
And selectBenefiting() is written like this...
function selectBenefiting(val) {
$("#benefiting").val(val);
$("#benefiting-sugg").hide();
}
Can anyone help me achieve this while preserving the line breaks? I have been searching for hours, and while I see other people with the same issue, I have yet to find a solution.
As I've said, it is a MySQL database. If I echo the data in the textarea using PHP it displays correctly, so I know it is in the database correctly. The issue is when using jQuery and AJAX to attempt to populate the textarea.
Any help would be greatly appreciated.
UPDATE:
$benefiting_js = str_replace("rn", "\r\n", $benefiting);
Slightly modified solution from Nick did the trick.
javascript php jquery html mysql
Can you show the output HTML on the client side?
– Ahmad
Nov 25 '18 at 6:50
add a comment |
I have a form that users fill out which contains a textarea. This form is uploaded to a MySQL database, and then later the user can edit the info. The issue I am having is that when I use AJAX to suggest data for the textarea, javascript gives a "Invalid or unexpected token" error wherever a newline appears.
In the suggestion box it is a list with rows like so...
<li onClick="selectBenefiting('$benefiting')">$benefiting</li>
And selectBenefiting() is written like this...
function selectBenefiting(val) {
$("#benefiting").val(val);
$("#benefiting-sugg").hide();
}
Can anyone help me achieve this while preserving the line breaks? I have been searching for hours, and while I see other people with the same issue, I have yet to find a solution.
As I've said, it is a MySQL database. If I echo the data in the textarea using PHP it displays correctly, so I know it is in the database correctly. The issue is when using jQuery and AJAX to attempt to populate the textarea.
Any help would be greatly appreciated.
UPDATE:
$benefiting_js = str_replace("rn", "\r\n", $benefiting);
Slightly modified solution from Nick did the trick.
javascript php jquery html mysql
I have a form that users fill out which contains a textarea. This form is uploaded to a MySQL database, and then later the user can edit the info. The issue I am having is that when I use AJAX to suggest data for the textarea, javascript gives a "Invalid or unexpected token" error wherever a newline appears.
In the suggestion box it is a list with rows like so...
<li onClick="selectBenefiting('$benefiting')">$benefiting</li>
And selectBenefiting() is written like this...
function selectBenefiting(val) {
$("#benefiting").val(val);
$("#benefiting-sugg").hide();
}
Can anyone help me achieve this while preserving the line breaks? I have been searching for hours, and while I see other people with the same issue, I have yet to find a solution.
As I've said, it is a MySQL database. If I echo the data in the textarea using PHP it displays correctly, so I know it is in the database correctly. The issue is when using jQuery and AJAX to attempt to populate the textarea.
Any help would be greatly appreciated.
UPDATE:
$benefiting_js = str_replace("rn", "\r\n", $benefiting);
Slightly modified solution from Nick did the trick.
javascript php jquery html mysql
javascript php jquery html mysql
edited Nov 25 '18 at 7:58
Fred J Langemark III
asked Nov 25 '18 at 6:45
Fred J Langemark IIIFred J Langemark III
568
568
Can you show the output HTML on the client side?
– Ahmad
Nov 25 '18 at 6:50
add a comment |
Can you show the output HTML on the client side?
– Ahmad
Nov 25 '18 at 6:50
Can you show the output HTML on the client side?
– Ahmad
Nov 25 '18 at 6:50
Can you show the output HTML on the client side?
– Ahmad
Nov 25 '18 at 6:50
add a comment |
1 Answer
1
active
oldest
votes
The problem is that Javascript doesn't like newlines in strings. So where you have them, you need to escape the newline characters. Try something like
$benefiting_js = str_replace("n", "\n", $benefiting);
And then change your <li>
to
<li onClick="selectBenefiting('$benefiting_js')">$benefiting</li>
Have to also include r, but this did the trick. Thank you Nick.
– Fred J Langemark III
Nov 25 '18 at 7:55
@FredJLangemarkIIIr
or\r
? I'll edit the answer to correct it.
– Nick
Nov 25 '18 at 7:57
I updated the question to show what worked. You're the man. I had been working in this for so long focusing on the new line character, and your answer reminded me of the return character.
– Fred J Langemark III
Nov 25 '18 at 7:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465291%2fphp-mysql-fill-textarea-with-text-that-contains-newlines-using-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that Javascript doesn't like newlines in strings. So where you have them, you need to escape the newline characters. Try something like
$benefiting_js = str_replace("n", "\n", $benefiting);
And then change your <li>
to
<li onClick="selectBenefiting('$benefiting_js')">$benefiting</li>
Have to also include r, but this did the trick. Thank you Nick.
– Fred J Langemark III
Nov 25 '18 at 7:55
@FredJLangemarkIIIr
or\r
? I'll edit the answer to correct it.
– Nick
Nov 25 '18 at 7:57
I updated the question to show what worked. You're the man. I had been working in this for so long focusing on the new line character, and your answer reminded me of the return character.
– Fred J Langemark III
Nov 25 '18 at 7:59
add a comment |
The problem is that Javascript doesn't like newlines in strings. So where you have them, you need to escape the newline characters. Try something like
$benefiting_js = str_replace("n", "\n", $benefiting);
And then change your <li>
to
<li onClick="selectBenefiting('$benefiting_js')">$benefiting</li>
Have to also include r, but this did the trick. Thank you Nick.
– Fred J Langemark III
Nov 25 '18 at 7:55
@FredJLangemarkIIIr
or\r
? I'll edit the answer to correct it.
– Nick
Nov 25 '18 at 7:57
I updated the question to show what worked. You're the man. I had been working in this for so long focusing on the new line character, and your answer reminded me of the return character.
– Fred J Langemark III
Nov 25 '18 at 7:59
add a comment |
The problem is that Javascript doesn't like newlines in strings. So where you have them, you need to escape the newline characters. Try something like
$benefiting_js = str_replace("n", "\n", $benefiting);
And then change your <li>
to
<li onClick="selectBenefiting('$benefiting_js')">$benefiting</li>
The problem is that Javascript doesn't like newlines in strings. So where you have them, you need to escape the newline characters. Try something like
$benefiting_js = str_replace("n", "\n", $benefiting);
And then change your <li>
to
<li onClick="selectBenefiting('$benefiting_js')">$benefiting</li>
answered Nov 25 '18 at 7:45
NickNick
27k111939
27k111939
Have to also include r, but this did the trick. Thank you Nick.
– Fred J Langemark III
Nov 25 '18 at 7:55
@FredJLangemarkIIIr
or\r
? I'll edit the answer to correct it.
– Nick
Nov 25 '18 at 7:57
I updated the question to show what worked. You're the man. I had been working in this for so long focusing on the new line character, and your answer reminded me of the return character.
– Fred J Langemark III
Nov 25 '18 at 7:59
add a comment |
Have to also include r, but this did the trick. Thank you Nick.
– Fred J Langemark III
Nov 25 '18 at 7:55
@FredJLangemarkIIIr
or\r
? I'll edit the answer to correct it.
– Nick
Nov 25 '18 at 7:57
I updated the question to show what worked. You're the man. I had been working in this for so long focusing on the new line character, and your answer reminded me of the return character.
– Fred J Langemark III
Nov 25 '18 at 7:59
Have to also include r, but this did the trick. Thank you Nick.
– Fred J Langemark III
Nov 25 '18 at 7:55
Have to also include r, but this did the trick. Thank you Nick.
– Fred J Langemark III
Nov 25 '18 at 7:55
@FredJLangemarkIII
r
or \r
? I'll edit the answer to correct it.– Nick
Nov 25 '18 at 7:57
@FredJLangemarkIII
r
or \r
? I'll edit the answer to correct it.– Nick
Nov 25 '18 at 7:57
I updated the question to show what worked. You're the man. I had been working in this for so long focusing on the new line character, and your answer reminded me of the return character.
– Fred J Langemark III
Nov 25 '18 at 7:59
I updated the question to show what worked. You're the man. I had been working in this for so long focusing on the new line character, and your answer reminded me of the return character.
– Fred J Langemark III
Nov 25 '18 at 7:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465291%2fphp-mysql-fill-textarea-with-text-that-contains-newlines-using-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show the output HTML on the client side?
– Ahmad
Nov 25 '18 at 6:50