weixin_33675507 2014-12-01 17:15 采纳率: 0%
浏览 16

使用ajax处理表格

I am working on a form where visitors can subscribe to a mailing list.

The code is:

    function subscribe_form() {
        global $my_subme;

        $form = '';
        $msg = '';

        if ( isset( $_GET['status'] ) ) {
            if ( 'subscribed' === $_GET['status'] ) {
                $msg = __( 'Thank you for confirming your subscription', 'subme' );
            } else if ( 'unsubscribed' === $_GET['status'] ) {
                $msg = __( 'You have successfully unsubscribed.', 'subme' );
            } else {
                $msg = __( 'Sorry, but something went wrong.', 'subme' );
            }

            $form .= '<p>';
            $form .= esc_html( $msg );
            $form .= '</p>';
        } else {
            /* Process the form when submitted */
            if ( ( isset( $_POST['subscribe'] ) || isset( $_POST['unsubscribe'] ) ) && isset( $_POST['source'] ) && 'widget' === $_POST['source'] ) {
                /* Check nonce */
                if ( ! isset( $_POST['subme_widget_nonce'] ) || ! wp_verify_nonce( $_POST['subme_widget_nonce'], 'subme_widget' ) ) {
                    return;
                }

                if ( ! isset( $_POST['email'] ) ) {
                    return;
                }

                if ( ! $my_subme->is_valid_email( strtolower( $_POST['email'] ) ) ) {
                    $msg = __( 'Sorry, but this does not seem like a valid email address.', 'subme' );

                } else {
                    if ( isset( $_POST['subscribe'] ) ) {
                        $msg = $my_subme->subscribe( $_POST['email'] );
                    } else {
                        $msg = $my_subme->unsubscribe( $_POST['email'] );
                    }
                }
            }

            $form .= '<form method="post">';
            $form .= wp_nonce_field( 'subme_widget', 'subme_widget_nonce', true, false );
            $form .= '<input type="hidden" name="source" value="widget" />';
            $form .= '<p>';
            $form .= __( 'Your email address:', 'subme' ) . '<br />';
            $form .= '<input type="text" name="email" /><br /><br />';
            $form .= '<input type="submit" name="subscribe" value="' . __( 'Subscribe', 'subme' ) . '" />';
            $form .= ' ';
            $form .= '<input type="submit" name="unsubscribe" value="' . __( 'Unsubscribe', 'subme' ) . '" />';
            $form .= '</p>';
            $form .= '</form>';

            if ( strlen( $msg ) > 0 ) {
                $form .= '<p>';
                $form .= esc_html( $msg );
                $form .= '</p>';
            }
        }

        echo $form;
        echo '</div>';

    }


subscribe_form();

How do I change this code so that it is processed using ajax? As you can see, currently it is a simply page refresh when user submits the form.

  • 写回答

2条回答 默认 最新

  • weixin_33720078 2014-12-01 17:27
    关注

    If you have two submits, only the second one will be sent to the form... You need to do something like 2 forms, or add a onclick event to change the name or type or a hidden value... something like:

    <input type="submit" onclick="document.getElementById('action').value='subscribe'" value="Subscribe" />
    <input type="submit" onclick="document.getElementById('action').value='unsubscribe'" value="Unsubscribe" />
    <input type="hidden" name="action" id="action" />
    
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?