Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过'+';在JS变量中签名_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 通过'+';在JS变量中签名

Javascript 通过'+';在JS变量中签名,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图传递一个变量,该变量可能包含一个“+”符号,但在下一页传递后,它会被连接起来 假设我将'2+2'从first.php传递到second.php上的second.php,它显示为'2 2'。但我只需要它为'2+2'。这对'2-2'是正确的。这是我的first.php <script> $(document).ready(function() { $("#sub").click(function() { var txt1 = $("#userquery").val();

我试图传递一个变量,该变量可能包含一个“+”符号,但在下一页传递后,它会被连接起来

假设我将'2+2'从first.php传递到second.php上的second.php,它显示为'2 2'。但我只需要它为'2+2'。这对'2-2'是正确的。这是我的first.php

<script>
$(document).ready(function() {
    $("#sub").click(function() {
 var txt1 = $("#userquery").val(); //textbox value
        $.ajax({
            type: "POST",
            url: "second.php",
            cache: false,
            data: "txt1=" + txt1,    
            dataType: "html",
            success: function(result) {
                $("#sqlresult").html(result);
            }
        });
    });
});
</script>    
<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>

$(文档).ready(函数(){
$(“#子”)。单击(函数(){
var txt1=$(“#userquery”).val();//文本框值
$.ajax({
类型:“POST”,
url:“second.php”,
cache:false,
数据:“txt1=“+txt1,
数据类型:“html”,
成功:功能(结果){
$(“#sqlresult”).html(结果);
}
});
});
});

像这样试试


在像url参数一样传递值之前,必须对值进行url编码

var txt1 = encodeURIComponent($("#userquery").val()); 

您正在为
数据
传递一个字符串,这意味着您要对它的正确编码负责(但没有正确编码)。在URI编码中,
+
表示空间

只需将其作为对象传递,jQuery将为您正确处理编码:

data: {txt1: txt1}
data: {txt1: txt1}