Joran Dob Joran Dob - 6 months ago 22
jQuery Question

Ajax post form serialise, post quotes '

I am trying to let my code post HTML form values. It works but i cannot get it to post single quotes ' . I think this is due the usage of serialise. i so far changed to on post change the single quote to 2 single quotes, but it didnt work.

Javascript

$( "#verstuur-bericht" ).click(function() {
var nummeroftekensbericht=$.trim($("#message").val());
if(nummeroftekensbericht.length>0)
{
$.post('http://192.168.178.24/server/submit.php', $('#bericht-verstuur-form')).serialize()
$('#message').val('');
setTimeout(function(){
$("html, body").animate({ scrollTop: $(document).height()+40 }, 10);
}, 1200);
}
else {
Materialize.toast('Error: bericht is leeg!', 4000) // 4000 is the duration of the toast

}

});


Html

form id="bericht-verstuur-form" action="http://192.168.178.24/server/submit.php" method="post">
<input style="display:none;" type="text" name="idu" value="<?php echo $_SESSION['user_id']; ?>" id="user_id"/>
<input style="display:none;" type="text" name="nameu" value="<?php echo $_SESSION['user_full_name']; ?>" id="naam" placeholder="Enter name" />
<input style="display:none;" type="number" name="user_level" value="3" id="user_level"/>



<input placeholder="Typ hier!" style="width: calc(98% - 116px); color:#000; margin-left:15px;" name="message" id="message" type="text" class="validate input-chat-style">

<ul id="nav-mobile" class="right ">

<a style="color:#444444; font-weight: bold; background-color:transparent; " id="verstuur-bericht">VERSTUUR</a>
</form>

Answer

Adding .replace(/\'/g,'\\\''); to serialize solved the problem! $.post('https://--', $('#bericht-verstuur-form').serialize().replace(/\'/g,'\\''))