1

I am having trouble with a jquery script to send a serialized form data along with a modified variable.i have tried a couple different methods, but the added variable is not posting.

I have tried a few methods, the best I think using jquery.param(), however, it is not passing the variable message through.

// Code the input (message) var message = $('textarea[name="content"]').html($('#emailbody').code()); // Submit the form using AJAX. $.ajax({ type: 'POST', url: 'mailer.php', data: $(form).serialize()+$.param(message), }) 

I am guessing my syntax for passing the variable is incorrect. Any tips?

Thank you, Nigel

1
  • What is the .code() in $('#emailbody').code(). Commented May 26, 2015 at 4:23

5 Answers 5

2

If the textarea with name content is within the form then just set its value and serialize the form

$('textarea[name="content"]').val($('#emailbody').code()); // Submit the form using AJAX. $.ajax({ type: 'POST', url: 'mailer.php', data: $(form).serialize(), }) 

Else you could try

// Submit the form using AJAX. $.ajax({ type: 'POST', url: 'mailer.php', data: $(form).serialize() + '&content=' + encodeURIComponent($('#emailbody').code()), }) 
Sign up to request clarification or add additional context in comments.

5 Comments

thanks. The textarea is in the same form, however, it is coded, so I have had to modify it before re-adding it to the form (it is a summernote.js input)
@npasco in that case there is no need to use any of that.. see jsfiddle.net/arunpjohny/457dr535/3
hmmm... I tried this, however, the resultant POST variable shows the html code.
@npasco then what do you want... the editor will give you the html code
doh!... my bad, I apologise. There was no issue with the above, but, there was an extra space between one of the php mail headers (i.e. $email_header . = "something";) that was stopping the html from displaying proper. Your answer above is fantastic, and hopefully usefull to someone else.
0

You could try:

data : $(form).serialize() + '&' + $.param(message) 

Comments

0

There should be a simple solution of your problem like below:

  1. Have one hidden field inside element. 2.Before you post your form, assign variable value to hidden field like below:

    $('#HiddenFieldIdHere').val(variablevalue);

  2. Then make ajax call like you did without appending variable value.

1 Comment

Good idea, it seems to do the job... however, the resultant POST is '[object Object]'... any ideas?
0
var message = $('#form_id').serialize(); 

$.ajax({ url: "mailer.php", type: 'POST', data: message, success: function (data) { $("#form_result").html(data); } });

Comments

0

Try:

data :{'form': $('#form').serialize(),'content':$('#emailbody').code()} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.