使用AJAX / JavaScript从PHP文件中获取变量

时间:2014-04-16 18:24:13

标签: javascript php ajax echo take

I have a form that uses AJAX and Javascript to submit itself.

HTML:

<h3>Sign Up Here</h3>
<form name="signupform" id="signupform" onsubmit="return false;">
<div>Username:</div>
<input id="username" type="text" onblur="checkusername()" onkeyup="restrict('username')" maxlength="16" />
<span id="unamestatus"></span>
<div>Email Address:</div>
<input id="email" type="text" onfocus="emptyElement('status')" onkeyup="restrict('email')" maxlength="88" />
<div>Create Password:</div>
<input id="pass1" type="password" onfocus="emptyElement('status')" />
<div>Confirm Password:</div>
<input id="pass2" type="password" onfocus="emptyElement('status')" />
<div>Gender:</div>
<select id="gender" onfocus="emptyElement('status')">
    <option value=""></option>
    <option value="m">Male</option>
    <option value="f">Female</option>
</select>
<div>Country:</div>
<select id="country" onfocus="emptyElement('status')">
    <option value="england">England</option>
</select>
<button id="signupbtn" onclick="signup()">Create Account</button>
<span id="status"></span>

的Ajax / JS:

function signup(){
    var u = _("username").value;
    var e = _("email").value;
    var p1 = _("pass1").value;
    var p2 = _("pass2").value;
    var c = _("country").value;
    var g = _("gender").value;
    var status = _("status");

    if(u == "" || e == "" || p1 == "" || p2 == "" || c == "" || g == ""){
        status.innerHTML = "Message 1"; 
    } else if (p1 != p2){
        status.innerHTML = "Message 2!";
    } else {
        _("signupbtn").style.display = "none";  
        status.innerHTML = "please wait...";
        var ajax = ajaxObj("POST", "signup.php");
        ajax.onreadystatechange = function(){
            if(ajaxReturn(ajax) == true){
                if(ajax.responseText != "signup_success"){
                    status.innerHTML = ajax.responseText;
                    _("signupbtn").style.display = "block"; 
                } else {
                    _("signupform").innerHTML = "Message 3";
                }
            }
        }
        ajax.send("u="+u+"&e="+e+"&p="+p1+"&c="+c+"&g="+g);
    }
}

如您所见,我的第二段代码中有Message 1Message 2Message 3。 这是我遇到麻烦的部分:我将有多个语言文件,其中包含与某些文本相关的一些变量。例如,在我的french.php文件中,我将:$welcome = 'Bonjour!'。然后我将检查MySQL表中的默认语言,并确定它是什么。这很简单。但是,如何让AJAX使用$welcome变量并使用它代替Message 1

对于沙里科夫:

<head>
<?= include_once("language.php"); ?>
<script type="text/javascript"> 
    window.onload = function() {
        function _(x){ 
            return document.getElementById(x); 
        } 
        var welcome = <?=json_encode($welcome);?>
        _("test").innerHTML = '<h2>Mesajul este: ' + welcome + '</h2>'; 
    }
</script>
</head>
<body>
   <div id="test"></div>
</body>

1 个答案:

答案 0 :(得分:0)

添加代码的新答案

<?= include_once("language.php"); ?> - 这是错误的。只有在输出变量(<?= == <?=$welcome;?>)时才能使用<?php echo $welcome; ?>。您必须从此php标记中删除=符号。 <? include_once("language.php"); ?>是正确的。

使用console.log(welcome)alert(welcome)确保已分配欢迎。

OLD ANSWER 你输出这个js和html抛出php,对吗?

您可以使用:

// some php code
?>
<script type="text/javascript">
    var welcome = <?=json_encode($welcome); ?>
    // alert(welcome); // to make sure value is assigned in js
</script>
<?
//more php code or html output

您必须使用<?=json_encode($welcome); ?>而非简单<?=$welcome;?>Marc B的解释:

  

永远不要将任意文本从PHP转储到javascsript代码中。你是   很可能会引入JS语法错误并杀死整个   脚本。总是使用json_encode:<?= json_encode($welcome) ?>

附加示例:

// more php
include_once("language.php"); 
// var_dump($welcome); // to debug what value assigned to $welcome
?> 
<script type="text/javascript"> 
    window.onload = function() {
        function _(x){ 
        return document.getElementById(x); 
        } 
        var welcome = <?=json_encode($welcome);?>
        _("test").innerHTML = '<h2>Mesajul este: ' + welcome + '</h2>'; 
    }
</script>
<?
// more php