WordPress - 执行按钮上的onclick函数,当页面加载时
P粉575055974
P粉575055974 2023-08-30 00:33:02
[PHP讨论组]
<p><strong>背景</strong></p> <p>我正在创建一个注册页面,并希望在用户按下“创建账户”按钮时运行一个函数。我有一个如下的按钮:</p> <pre class="brush:php;toolbar:false;">&lt;button type=&quot;submit&quot; name=&quot;wp-submit&quot; onclick=&quot;register_process();&quot; data-ppt-btn class=&quot;mt-5&quot;&gt; &lt;?php echo __(&quot;创建账户&quot;); ?&gt; &lt;/button&gt;</pre> <p>然后我在同一个PHP文件中有相应的函数:</p> <pre class="brush:php;toolbar:false;">function register_process(){ // 检查复选框中的用户类型 if(!jQuery(&quot;#user_type&quot;).is(':checked')){ &lt;?php // 标头 $headers = &quot;MIME-Version: 1.0&quot; . &quot;\r\n&quot;; $headers = &quot;Content-type:text/html;charset=UTF-8&quot; . &quot;\r\n&quot;; // 获取用户电子邮件 $user_registration_email = &quot;test987&quot;; //$_POST['user_email']; // 邮件的收件人、主题和正文 $admin_email_address = get_option('admin_email'); $email_subject = &quot;测试标题&quot;; $email_body = &quot;电子邮件:{$user_registration_email}&quot;; // 发送邮件 wp_mail( $admin_email_address, $email_subject, $email_body, $headers); ?&gt; } }</pre> <p><strong>问题</strong></p> <p>有多个问题,但首先是函数“register_process”在我加载页面时就运行了,甚至没有按下按钮。我在一些地方读到应该从onclick属性中删除(),这解决了这个问题。然而,按下按钮后什么也不会发生。</p> <p>我还尝试检查用户电子邮件字段是否为空,只有在为空时才发送电子邮件。然而,这样我根本收不到任何邮件。</p> <p>奇怪的是,我的函数中还有其他的一些东西,比如检查输入字段是否为空。这些错误/警告只有在按下按钮时才会触发,它们看起来像这样:</p> <pre class="brush:php;toolbar:false;">if(!jQuery(&quot;#privacypolicy&quot;).is(':checked')){ jQuery(&quot;#ppt-form-error&quot;).addClass('text-danger mb-4').html(&quot;&lt;?php echo __(&quot;您必须接受我们的隐私政策才能加入我们的网站。&quot;) ?&gt;&quot;); CanContinue =0; return; }</pre> <p>如何使函数只在按下按钮时发送邮件?</p>
P粉575055974
P粉575055974

全部回复(1)
P粉212114661

在onclick="register_process();"中只需要移除"();"

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号