简体   繁体   English

如何在提交表单时将PHP变量传递到Javascript Cookie中?

[英]How to pass a PHP variable into a Javascript cookie on submit of form?

I am setting up a custom form to be used in my wordpress site. 我正在设置一个自定义表格以在我的wordpress网站中使用。 What I would like to do is grab the AUTO_INCREMENT ID of that submission and pass it into a Javascript cookie when the form is submitted. 我想做的就是获取提交内容的AUTO_INCREMENT ID,并在提交表单时将其传递到Javascript Cookie中。 Currently the ID is working, but because the ID isn't grabbed until after the form is submitted, I'm having trouble figuring out how I can pass the variable into the cookie immediately after the form has been submitted(currently I have to submit the form twice before the cookie with the ID is created, and its 1 number lower than it should be since it's a submission behind). 目前该ID可以使用,但是由于在提交表单后才抓取该ID,因此我很难弄清楚如何在提交表单后立即将变量传递到Cookie中(目前,我必须提交表单在创建具有ID的Cookie之前两次,其编号比应有的数字低1个数字,因为它后面是提交的)。

Here's what I have currently: 这是我目前所拥有的:

<?php
if(isset($_POST["submit"])) {
    $name = $_POST['full_name'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $email = $_POST['email'];

    $wpdb->insert(
        'reps',
        array(
            'name' => stripslashes($name),
            'city' => stripslashes($city),
            'state' => stripslashes($state),
            'email' => stripslashes($email)
        )
    );

    $lastid = $wpdb->insert_id;
}
?>
<script>
$('#dealer-form').submit(function() {
    var repID = '<?php echo $lastid ?>';
    setCookie('ID', repID);
});
</script>

As I mentioned this code works on the second submission(since the variable has no value on the first submission), and the ID is 1 number behind because it is grabbing the ID of the previous submission before reassigning the variable value. 正如我提到的,此代码在第二个提交中起作用(因为该变量在第一个提交中没有值),并且ID后面紧跟1个数字,因为它在重新分配变量值之前获取了前一个提交的ID。

PHP, being a server-side scripting language, is executed before the data is sent to your browser. PHP是一种服务器端脚本语言,在将数据发送到浏览器之前已执行。 JavaScript, a client-side scripting language, is executed as soon as the script is encountered by the browser. 一旦浏览器遇到脚本,就会执行JavaScript(一种客户端脚本语言)。

Your approach is forgetting this separation between front- and back-end. 您的方法忘记了前端和后端之间的这种分离。

To accomplish what you're trying to do, simply output the setCookie call when you've submitted your form in php : 要完成您要执行的操作,只需在php中提交表单输出setCookie调用即可:

<?php
    if(isset($_POST["submit"])) {
        $name = $_POST['full_name'];
        $city = $_POST['city'];
        $state = $_POST['state'];
        $email = $_POST['email'];

        $wpdb->insert(
            'reps',
            array(
                'name' => stripslashes($name),
                'city' => stripslashes($city),
                'state' => stripslashes($state),
                'email' => stripslashes($email)
            )
        );

        $lastid = $wpdb->insert_id;

        printf( '<script>setCookie("ID", %d);</script>', $lastid );
    }
?>

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

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