PHP之前的头 - 如何使用jquery函数

时间:2016-07-12 08:11:21

标签: jquery wordpress

要在wordpress中创建新用户,请使用前端表单。当表单被提交时,php代码在页面的head部分之前执行。现在所有的javascript代码(jQuery和我的函数)都被加载到head部分。当php-part被执行时,没有任何函数会给我错误:

  

ReferenceError:未定义functionone

<!-- WHEN FORM IS SUBMITTED -->
<?php  
    if(isset($_POST['sent']) && $_POST['sent'] == 'true') {

        $username = $_POST['email'];    
        $salutation = $_POST['salutation']; 
        $email = $_POST['email'];   
        $lastname = $_POST['lastname']; 
        $firstname = $_POST['firstname'];   
        $company = $_POST['company'];   
        $status = $_POST['leuchterstatus']; 
        $message = $_POST['message'];   

        // Create a new WordPress user
        $userdata = array(
            'user_login'=> $username,
            'user_email' => $email,
            'last_name' =>  $lastname,
            'first_name'    =>  $firstname,    
            'user_pass'   =>  NULL,
            'role' => 'subscriber'
        );

       if ( username_exists( $email ) ) {
            echo '<script> functionone(); </script>';
            }
        else {
            echo '<script> functiontwo(); </script>';
        }
    }
?>

<?php get_header(); ?>

1 个答案:

答案 0 :(得分:2)

只需将PHP代码向下移动,以便在<{em> head部分发送到浏览器之后发送它的输出,以便script你正在生成的元素之后加载jQuery等等。然后函数将存在,你的脚本元素可以调用它。