2

I am using this code to serialize the form of the current submit button. It works fine!

data: $(this.form).serialize() // it is used in $.ajax() 

But when I try to add my own custom parameter like this

$(this.form).serialize() +'&address_serial=' + 
$('#address').val().replace(new RegExp('\n', 'g'), '<br />')

It is more likely to break the code, and the form reloads instead of staying static and sending the ajax request.

Note that I do have the return false at the end of the code to stop further posting of the form while submit is clicked.

How can I either add a custom parameter, or alter the current value of the textarea so that each enter key gets saved!

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • I think you should encode the `
    ` like `
    `. It breaks the query string. But it's better to convert the newlines to HTML line break tag at server side.
    – Hashem Qolami Jan 20 '14 at 17:02
  • @HashemQolami, nope even that doesn't work. – Afzaal Ahmad Zeeshan Jan 20 '14 at 17:04
  • Look for the request in your browser dev tools and post the actual url that gets requested. – Jason P Jan 20 '14 at 17:06
  • Why do you have to replace the newlines to send them to the server, they are not removed, and if using something like PHP you can just use nr2br when outputting? – adeneo Jan 20 '14 at 17:06
  • @JasonP, I always try to keep a track there while making the `ajax` request. But the page reloads which means nothing is sent via ajax. It loads the css, js, html files required for the page and the page is loaded once again just like a plain request. a plain form post... – Afzaal Ahmad Zeeshan Jan 20 '14 at 17:07
  • Ummm @adeneo, I am using GET request. So when I use `POST` the enter keys will be saved? Since in the GET request I don't get the enter key value in the textarea; database table column. – Afzaal Ahmad Zeeshan Jan 20 '14 at 17:08
  • I doubt your `return false` at the end of code is executed due to the broken code.. – techBeginner Jan 20 '14 at 17:08
  • I would leave the new lines alone and do that conversion server-side when sanitizing it before inserting into db. – Kevin B Jan 20 '14 at 17:10
  • I was pretty sure jQuery escaped the newlines so they could be passed with GET, but I could be wrong, don't have the time to test it now. – adeneo Jan 20 '14 at 17:11
  • @adeneo It might be that it gets sanitized within $.param, but since he's building the param manually, that gets skipped. – Kevin B Jan 20 '14 at 17:12
  • @dotNETbeginner, yes, that is the problem! The code is broken and the return false is not executed; thus causing a page reload! – Afzaal Ahmad Zeeshan Jan 20 '14 at 17:13
  • @KevinB, aha! So when I will use `POST` request, that would be preserved? Right? – Afzaal Ahmad Zeeshan Jan 20 '14 at 17:13
  • @AfzaalAhmadZeeshan remove the return false and instead prevent the submit using event.preventDefault at the top. Continue debugging (you will now be able to see the error message) – Kevin B Jan 20 '14 at 17:14
  • @AfzaalAhmadZeeshan No, POSTvsGET has nothing to do with this. – Kevin B Jan 20 '14 at 17:14
  • @AfzaalAhmadZeeshan, I think of possible duplicate (http://stackoverflow.com/questions/6627936/jquery-post-with-serialize-and-extra-data) – techBeginner Jan 20 '14 at 17:14
  • 1
    `$(this.form).serialize() +'&address_serial=' + encodeURIComponent($('#address').val());` – adeneo Jan 20 '14 at 17:15
  • or `$(this.form).serialize() + '&' + $.param({address_serial:$('#address').val()});` – Kevin B Jan 20 '14 at 17:16
  • If the issue is the added param, url encode it, and the newlines will be passed as `%0D%0A` without issues? And yes, @KevinB, jQuery's param does the same thing – adeneo Jan 20 '14 at 17:16
  • @KevinB, I have tried your method, but it doesn't work and page was still reloaded. However I have tried your other method of `event.preventDefault()` that is helpfull. And I have recieved the output! What would I do now? The `encoding?` :) Let me try that.. – Afzaal Ahmad Zeeshan Jan 20 '14 at 17:17
  • @adeneo, your method works! :) Collectively with KevinB's `event.preventDefault()`. Thanks guys :) – Afzaal Ahmad Zeeshan Jan 20 '14 at 17:21
  • There you go, @KevinB will edit his answer for you to accept. – adeneo Jan 20 '14 at 17:26

1 Answers1

2

.serialize creates a paramstring, therefore, to add params to it, use string concatenation.

var data = $("#myform").serialize() + "&" + $.param({foo: "bar"});

using $.param additionally ensures that the resulting string is properly encoded.

Alternatively, you can use encodeURIComponent:

$(this.form).serialize() +'&address_serial=' + encodeURIComponent($('#address').val());

And, to help with debugging, it's almost always better to use event.preventDefault() rather than return false simply because return false won't be reached if an error occurs while event.preventDefault() will if you use it at the top.

Kevin B
  • 94,570
  • 16
  • 163
  • 180