如果$ POST或cookie为空,则调用Java Script函数?

时间:2017-07-28 02:36:59

标签: javascript php wordpress post cookies

如果这些条件适用,我想调用名为expandRegisterForm()的java脚本函数:

    if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
        //call expandRegisterForm()
    }

    if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
        //call expandRegisterForm()
    }


    if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
        //call expandRegisterForm()
    }

    if ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) {
        //call expandRegisterForm()
    }

    if ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) ) {
        //call expandRegisterForm()
    }

    if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
        //call expandRegisterForm()
    }

    if ( isset( $_POST['billing_state'] ) && empty( $_POST['billing_state'] ) ) {
        //call expandRegisterForm()
    }

这个JS函数扩展了寄存器的公式。只有在没有输入的情况下才应该扩展。我现在的问题是,如何调用这个JS函数?

我已经在header.php和function.php中尝试了这些代码,其中包含调用JS函数的两种变体:

  1. echo <script>expandRegisterForm()</script>;
  2. ?><script>expandRegisterForm();</script><?php
  3. 他们都没有工作。可能是因为我在header.php和function.php中插入了代码吗?

    我是否可以在其他地方插入代码?如果是的话,在哪里?我有一个wordpress网站只是为了知道。

    我的第二个想法是我可以用饼干来做。如果用户按下按钮,我会设置一个1小时流动时间的cookie。如果设置了cookie,我会调用expandRegisterForm() JS函数。但同样在这里。如何以及在何处调用该功能?

    对帮助非常高兴! 亲切的问候

2 个答案:

答案 0 :(得分:0)

因为你在header.php上调用了expandRegisterForm,所以在该函数应该扩展的表单之前很快被调用,甚至在DOM中创建。 也许你可以试试这个。在header.php上

 $expand_form = false;
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
    $expand_form = true;
}
...

on footer.php

if($expand_form ==true)
{
    echo "<script>expandRegisterForm()</script>";
}

我必须指出,这并不是最好的方法。理想情况下,您希望在提交之前使用javascript验证字段。

答案 1 :(得分:0)

不要越过溪流!

WordPress最佳做法,您应该使用wp_localize_script()

functions.php

// check for required vars. If any are missing, 'true' will be assigned
$expand_form_var = ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) )
               || ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) )
               || ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) )
               || ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) )
               || ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) )
               || ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) )
               || ( isset( $_POST['billing_state'] ) && empty( $_POST['billing_state'] ) )
    ? true
    : false;


// Register the script the normal WordPress way
wp_register_script( 'my_theme_script', 'path/to/myscript.js' );

// Add the variable to an array, where the arbitrary key name will be used to call the var in the JS script
$php_to_js_vars = array(
    'expand_form_key' => $expand_form_var,
);

// Localize the script to make the variable available in the JS script
wp_localize_script( 
    'my_theme_script', // you need to pass in script handle used in 'wp_register_script' to make the var available in it.
    'php_checks', // this is an arbitrary name that will be used to call the object in the localized JS
    $php_to_js_vars // an array of variables to pass to localize in the JS script. These will be made available using dot annotation (object.property) in the Js file
);

// finally enqueue the previously registered script using the handle you defined in the top function
wp_enqueue_script( 'my_theme_script' );

然后在path/to/myscript.js中你会像这样使用它:

// 'php_checks' is the object name defined in the 'wp_localize_script' function. 'expand_form_key' was the key from the PHP array where the required variable was assigned.
if ( php_checks.expand_form_key === true )
    expandRegisterForm();

这样,所有的PHP var逻辑都在PHP中得到了彻底的处理,而JS在运行代码之前只需要条件。