在模态中显示Javascript变量


Showing Javascript variables in a modal

在我的网站上,我试图使一个模态,通知他们的用户名(从他们的名字和姓氏生成)的用户,并通知用户,他们已被添加到一个审核que为他们的帐户被批准。我在一个php文件中创建了一个用ajax调用的帐户,然后使用JSON反馈一个整数,通知javascript过程是否成功,如果成功,用户的用户名是什么。如果创建帐户并显示有关审批过程的信息,则我的模式打开良好,但是我似乎无法让它显示用户名中的部分。你能试着看看我哪里出错了吗?

Javascript

var RegisterSuccess = new Array();
var RegisterSuccess = JSON.parse(Feedback);
if (RegisterSuccess[0] == 0){
    $('#mdlInfo').html('<p>Your account has been created under the username: "<strong><span id="spnUsername"></span></strong>". You <strong>must</strong> remember this as you will require it to login to your account.</p>');
    $('#mdlInfo').html('<p>Your account has also been added to a moderation que. You must wait until a member of staff activates your account!</p>');
    $('#spnUsername').html(RegisterSuccess[1]);
    $("#mdlRegister").modal("show");
输出代码

$Feedback = json_encode(array($RegisterSuccess, $username));

echo $反馈;

提前感谢!

为什么不从PHP返回一个正确的json_encoded数组呢?不需要赋值变量,只需要赋值即可echo json_encode(["success"=>"true","username"=>"$username"]);在javascript中(不需要将RegisterSuccess分配为数组),只需这样做var returnValue = JSON.parse(Feedback);(您应该避免调用变量RegisterSuccess—选择一个中性的名称,以便您可以在成功和错误中使用它而不会出现问题。然后输入

$('#mdlInfo').html('<p>Your account has been created under the username: <strong><span id="spnUsername">'+returnValue['username']+'</span></strong>. You <strong>must</strong> remember this as you will require it to login to your account.</p><p>Your account has also been added to a moderation que. You must wait until a member of staff activates your account!</p>');
$("#mdlRegister").modal("show");