输出表单将成功提交到页面而不是重定向

时间:2011-09-24 11:05:30

标签: php javascript jquery forms wordpress

我正在使用WordPress侧边栏小部件来收集我的简报的电子邮件。该插件已被编写为在提交后重定向到“成功页面”,这不是很好,因为我宁愿它只是在提交后在表单下的div中输出成功消息,以便访问者留在页面上。我怎样才能做到这一点?我正在考虑将成功页面上的元刷新回到访问者所在的页面,但是当我非常确定我已经看到成功消息刚刚输出到的页面电子邮件捕获表单时,这非常笨拙该页面没有重定向。

如果你不介意,我将在这里复制相当多的插件代码,因为我不确定需要在这里调整什么来获得我正在寻找的那种提交。或者,如果有任何其他方式可以告诉我这样做,我真的很感谢你的帮助。非常感谢。

<?php 
function wp_email_capture_form($error = 0) {
    $url = get_option('home');
    $url = addLastCharacter($url);
?>
<div id="wp_email_capture"><form name="wp_email_capture" method="post" action="<?php echo $url; ?>">
<?php
    if (isset($_GET["wp_email_capture_error"])) {
        $error = sanitize($_GET["wp_email_capture_error"]);
        echo "<div style='width:80%;background-color: #FFCCCC; margin: 5px;font-weight'>Error: ". $error ."</div>";
    }
?>
<label class="wp-email-capture-name">Name:</label> <input name="wp-email-capture-name" type="text" class="wp-email-capture-name"><br/>
<label class="wp-email-capture-email">Email:</label> <input name="wp-email-capture-email" type="text" class="wp-email-capture-email"><br/>
<input type="hidden" name="wp_capture_action" value="1">
<input name="Submit" type="submit" value="Submit" class="wp-email-capture-submit">
</form>
</div>
<?php
    if (get_option("wp_email_capture_link") == 1) {
        echo "<p style='font-size:10px;'>Powered by <a      href='http://www.gospelrhys.co.uk/plugins/wordpress-plugins/wordpress-email-capture-plugin' target='_blank'>WP Email Capture</a></p>\n";
    }
}

function wp_email_capture_form_page($error = 0) {
    $url = get_option('home');
    $url = addLastCharacter($url);
    $display .= "<div id='wp_email_capture_2'><form name='wp_email_capture_display'     method='post' action='" . $url ."'>\n";
    if (isset($_GET["wp_email_capture_error"])) {
        $error = sanitize($_GET["wp_email_capture_error"]);
        $display .= "<div style='width:80%;background-color: #FFCCCC; margin:      5px;font-weight'>Error: ". $error ."</div>\n";
    } 
    $display .= "<label class='wp-email-capture-name'>Name:</label> <input name='wp-email-capture-name' type='text' class='wp-email-capture-name'><br/>\n";
    $display .= "<label class='wp-email-capture-email'>Email:</label> <input name='wp-email-capture-email' type='text' class='wp-email-capture-email'><br/>\n";
    $display .= "<input type='hidden' name='wp_capture_action' value='1'>\n";
    $display .= "<input name='Submit' type='submit' value='Submit' class='wp-email-capture-submit'></form></div>\n";
    if (get_option("wp_email_capture_link") == 1) {
        $display .= "<p style='font-size:10px;'>Powered by <a href='http://www.gospelrhys.co.uk/plugins/wordpress-plugins/wordpress-email-capture-plugin' target='_blank'>WP Email Capture</a></p>\n";
    } 

    return $display;
}

function wp_email_capture_display_form_in_post($content) {
    $get_form = wp_email_capture_form_page();
    $content = str_replace("[wp_email_capture_form]", $get_form, $content);
    return $content;
}
?>

1 个答案:

答案 0 :(得分:1)

这是我第一次尝试Stack Overflow答案:

您的表单操作当前会指向其他页面,但您需要它才能返回您已经在的页面。您可以使用空白操作属性执行此操作,如下所示:

<form action="">

完成此操作后,您可以在同一页面上使用$_POST数据。在您想要输出的页面div中,尝试以下内容:

if($_POST['wp_capture_action'] == 1)
{
    // The form has been submitted, so
    // print the success/failure message here
}
else
{
    // The form hasn't been submitted, so print
    // the form here.
}

我希望这有帮助!