在我的网站上,我试图制作一个模式,告知用户他们的用户名(根据他们的姓名和姓氏生成),并通知用户他们已被添加到审核中,以便他们的帐户获得批准。我在一个用ajax调用的php文件中创建帐户,然后使用JSON反馈一个整数,通知javascript进程是否成功,如果是,用户的用户名是什么。如果创建了帐户并且显示了有关批准过程的信息,我的模式就会打开,但是我似乎无法显示带有用户名的部分。您能否尝试查看我出错的地方。< / p>
的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");
PHP输出代码
$ Feedback = json_encode(array($ RegisterSuccess,$ username));
echo $ Feedback;
提前致谢!
答案 0 :(得分:1)
为什么不从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");