简体   繁体   English

如何将PHP传递的值连接到AJAX?

[英]How to concatenate passed values from PHP to AJAX?

I want to concatenate all the values of showFname , showMname , showLname and show the result on showName . 我想连接showFnameshowMnameshowLname所有值,并在showName上显示结果。

My problem is the concatenated names won't show in my showName . 我的问题是串联的名称不会显示在我的showName

HTML HTML

<input type="text" id="showFname" name="showFname" />
<input type="text" id="showMname" name="showMname" />
<input type="text" id="showLname" name="showLname" /> 
<label id="showName"></label>

JavaScript/jQuery: JavaScript / jQuery:

$(document).ready(function(){
    var $emailCodeResult = $('#emailCodeResult');
    var showFname = $('#showFname');
    var showMname = $('#showMname');
    var showLname = $('#showLname');
    var showName = $('#showName');

    var countTimerShowInfo = setInterval(function(){
        showInfo(); 
        showName = showFname + showMname +showLname;  
    }, 500);

    function showInfo(){
        $.ajax({
            url:"files/view/profileInfo.php",
            type:"GET",
            data: { term : $('#emailCodeResult').val() },
            dataType:"JSON",
            success: function(result) {
                $("#showFname").val(result.per_fname);
                $("#showMname").val(result.per_mname);
                $("#showLname").val(result.per_lname);
            }
        });
    };
});

尝试这个

$('#showName').text(concatenatedNames)

you have to get and set the value of the labels with the text() or html() function: 您必须使用text()html()函数获取设置标签的值:

$(document).ready(function(){

var  $emailCodeResult = $('#emailCodeResult');
var showFname = $('#showFname').val();
var showMname = $('#showMname').val();
var showLname = $('#showLname').val();
var showName = $('#showName');

function showInfo(){
        $.ajax({
        url:"files/view/profileInfo.php",
        type:"GET",
        data: { term : $('#emailCodeResult').val() },
        dataType:"JSON",
        success: function(result) {

        $("#showFname").val(result.per_fname);
        $("#showMname").val(result.per_mname);
        $("#showLname").val(result.per_lname);
        showName.html(showFname + showMname +showLname); 
        }

    });

};

setInterval( showInfo(), 500);

});

UPDATE: 更新:

there was a problem with your setInterval definition, fixed that, and also wrote the showName.html() inside the showInfo() function. 您的setInterval定义有问题,已解决,并在showInfo()函数内编写了showName.html()

What you want to do is sum up the .val() of those elements and not the elements. 您想要做的是总结那些元素而不是元素的.val()。

first you assign like this: 首先,您要像这样分配:

var showFname = $('#showFname').val();
var showMname = $('#showMname').val();
var showLname = $('#showLname').val();
var showName = $('#showName').val();

then you add these up like this: 然后将它们加起来像这样:

 showName = showFname + showMname +showLname;  

Just use .text() to append text.Or simply u can use html() also. 只需使用.text()附加文本即可,或者您也可以使用html()

$("#showname").text(showFname + showMname + showLname );

AND Important use.... 和重要用途。

function showInfo(){

var showFname = $('#showFname').val();
var showMname = $('#showMname').val();
var showLname = $('#showLname').val();

//ur code ......
}

Take variables inside function . 在函数内部获取变量。 In ur code , U get textbox value BLANK as its cached on Document Ready event as the page loads . 在您的代码中,当页面加载时,U获取文本框值BLANK,作为在文档就绪事件中缓存的文本框值。 So now scan updated values entered by user.Not on decument ready event. 因此,现在扫描用户输入的更新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM