Kaka DBA Kaka DBA - 2 months ago 4x
Ajax Question

passing a '+' sign within a JS variable

I am trying to pass a variable that may contain a '+" sign within, But after passing in the next page it is getting concatenated.

let say i pass '2+2' , from first.php to second.php on second.php it appears as '2 2'.But I need it as '2+2' only. The same is correctly working for '2-2'. This is my first.php

$(document).ready(function() {
$("#sub").click(function() {
var txt1 = $("#userquery").val(); //textbox value
type: "POST",
url: "second.php",
cache: false,
data: "txt1=" + txt1,
dataType: "html",
success: function(result) {
<textarea id='userquery' rows='5' cols='115' spellcheck='false' wrap='off' placeholder='Write SQL query here..'></textarea>
<input id='sub' type='submit' value='Execute' title='Ctrl+Enter'>
<hr><div id='sqlresult'><?php include('second.php'); ?></div>


You're passing a string for data, which means you're taking responsibility for it being properly encoded (but not properly encoding it). It happens that in URI encoding, + means space.

Just pass it as an object and jQuery will handle properly encoding it for you:

data: {txt1: txt1}