简体   繁体   English

在WordPress中提交表单后留在同一页面上

[英]Stay on same page after form submit within WordPress

I've added my HTML page as part of the admin wordpress (see screenshot) 我已将HTML页面添加为管理员wordpress的一部分(请参见屏幕截图)

WordPress中的HTML页面

And I'm trying to get it so on each submit button, if the record is successfully added to the database a pop up shows up saying "Success!" 我试图在每个提交按钮上使用它,如果将记录成功添加到数据库,则会弹出一个对话框,显示“成功!”。 and then clear the form data without leaving the page. 然后清除表单数据而不离开页面。 At the moment my current set up tries to load the external page instead of remaining on the page in the screenshot. 目前,我当前的设置尝试加载外部页面,而不是保留在屏幕快照的页面上。 Here's my HTML and PHP: 这是我的HTML和PHP:

<form class="pure-form" name="addAthlete" action="submitForm.php" method="POST">
    <fieldset class="pure-group">
        <input type="text" class="pure-input-2" name="membershipID" placeholder="Membership ID">

        <input type="text" class="pure-input-2" required="required" name="firstName" placeholder="First Name">

        <input type="text" class="pure-input-2" required="required" name="surname" placeholder="Surname">
    </fieldset>

    <button name="submitAthlete" type="submit" class="pure-button pure-input-2 pure-button-primary">Submit</button>
</form>

<?php
function showSuccess() {
    echo "Success! One record added.";
}

if (isset($_POST['submitAthlete'])) {
    addAthlete();
}

function addAthlete() {
    include 'addAthlete.php';
    showSuccess();
}
?>

I'm assuming the problem lies with the fact that the echo "Success" is trying to echo on the submitForm.php page which is how I've written it. 我假设问题在于回声“成功”正试图在submitForm.php页面上回声,这就是我编写它的方式。 This is because of the way the page is embedded into wordpress, you can see this below: 这是因为页面嵌入Wordpress的方式,您可以在下面看到此内容:

add_action( 'admin_menu', 'db_menu' );

function db_menu() {
    add_menu_page(
    'Database Form',     // page title
    'Database Form',     // menu title
    'manage_options',   // capability
    'database-form',     // menu slug
    'wpse_91693_render' // callback function
    );
}

function wpse_91693_render() {
    global $title;

    print '<div class="wrap">';
    print "<h1>$title</h1>";

    $file = plugin_dir_path( __FILE__ ) . "submitform.php";

    if ( file_exists( $file ) )
    require $file;

    print '</div>';
}

How can I get a pop up or something to show up within this WordPress page on each submit? 每次提交时,如何在此WordPress页面中弹出或显示某些内容?

Thanks! 谢谢!

When you submit a form, it posts data to a new webpage by loading it. 提交表单时,它会通过加载将数据发布到新网页。 In order to stay on the same page, the action should take you to that same page ( move your processing logic there aswell, checking for posted arguments ) or if you don't want to see a reload, use ajax instead. 为了停留在同一页面上,该操作应将您带到同一页面(将处理逻辑也移到该页面上,检查发布的参数),或者如果您不想看到重新加载,请使用ajax代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM