如何通过ajax将javascript变量传递给PHP

问题描述 投票:0回答:2

我试图制作联系表,但遇到一些问题:p 我读过将 javascript 变量传递给 php 的唯一方法是通过 ajax。我尝试了 stackoverflow 的一些建议,但没有任何效果。这是我的代码,我想听听您的意见。 我如何将 Javascript 变量 (jTemp2) 传递给 php?

示例.html

<!DOCTYPE html>
<html>
<head>

<title> Hello World </title>

</head>
<body>
<?php

// define variables and set to empty values
$fNameErr =  "";
$fName = "";
$temp = 0; //flag for mail
$temp2=0; //flag for button


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["fName"])) {
        $fNameErr = "Required";
        $temp=1;
        //echo $temp;
    } else {
        $fName = test_input($_POST["fName"]);
        // check if fName only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/", $fName)) {
            $fNameErr = "Invalid"; 
            $temp=1;
        }
    }
}    

function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}


?>
<script>
    var jTemp2 = "<?php echo $temp2; ?>";
</script>


<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"  id="form1" onsubmit="myFunction(jTemp2)">
<fieldset class="fiel">
<legend><b><span class="words">Contact</span></b></legend>
<table border="0">
<tr><td><b><span class="words"> Name: </span></b></td><td><input type="text" size="30" name="fName" id="fName" value="<?php echo $fName;?>"><span class="words"> *</span></td><td><span class="error"> <?php echo $fNameErr;?></span></td></tr>

<tr><td></td><td id="but"><input type="submit" value="Υποβολή" ></td></tr>
<tr><span class="words">* Required</span></tr>
<tr><td><span ><?php echo $temp; echo $temp2;  ?></span></td></tr>
</table>
</fieldset>
</form>


    <script>
    function myFunction(jTemp2)
    {
        jTemp2++;
    }
    </script>

    </body>
    </html>
javascript php ajax
2个回答
1
投票

将此添加到您的

<form> ... </form>

<script type="text/javascript">
     document.write('<input type="hidden" name="jTemp2" value="'+ jTemp2  +'">');
</script>

现在,当表单发布到 PHP 脚本时,变量的值将在里面:

<?php

$jTemp2 = isset($_POST['jTemp2']) ? $_POST['jTemp2'] : '';

?>

0
投票

非常感谢您的详细解释。我实现了它,是的!现在警报显示复选框状态的值。我已将变量选择为全局变量,以便能够进一步使用。但是,当我单击按钮时,我无法获取选择的值。这是经过更正的代码:

echo '<script>
function handleCheckboxChange(id) {
    var checkbox = $("#checkbox" + id);
    var isChecked = checkbox.prop("checked") ? 1 : 0;
    $.ajax({
        type: "POST",
        url: "update_js_php.php",
        data: {
            choosen: isChecked
        },
        success: function () {
            $.ajax({
                type: "GET",
                url: "get_session_value.php",  // PHP file to retrieve session
                success: function (response) {
                choose = response;      
          **alert(choose);///this shows "1" when checkbox is checked and "0" when unchecked**
                }
            });
        }
    });
};

$(document).ready(function () {
 var updatedConcatenatedText;
 var choose;


$("#Button1" + ' . $id . ').on("click", function () {  
    **alert(choose);//this shows  "undefined"**
});

...... 我声明的变量选择正确吗?

© www.soinside.com 2019 - 2024. All rights reserved.