如何在成功执行php后隐藏表单

时间:2017-02-12 作者:Himanshu

我想在有人在我的插件表单中输入输入并点击提交按钮后隐藏表单。然而,由于我在插件文件中使用了shortcode函数,所以我无法使用简单的html来显示表单和隐藏表单。

如何隐藏此表单?这是我的插件文件php脚本:

<?php
function installer(){
    include(\'installer.php\');
}
register_activation_hook( __file__, \'installer\' ); //executes installer php when installing plugin to create new database

add_action(\'admin_menu\',\'result_menu\'); //wordpress admin menu creation
function result_menu()
{
    add_menu_page(\'Result\',\'Result\',\'administrator\',\'xenon-result\');
    add_submenu_page( \'xenon-result\', \'Manage Marks\', \' Manage Marks\', \'administrator\', \'Manage-Xenon-Marks\', \'Xenon_Marks\' );
}
function Xenon_Marks() //function to add marks addition form in admin view
{
    include(\'new/result-add-marks.php\');
}

function html_form_code() 
{
echo \'<h3 align="center" class="titlehead"><br><b>Shikhar Entrance Test - 2017 Result</b></h3>\';
echo \'<form action="" method="post" name="display_form">\';
echo \'<fieldset>\';
echo \'<legend>Student Information</legend>\';
echo \'Roll Number: <input type="number" min="170001" max="171000" name="rollNumber"><br>\';
echo \'<input type="submit">\';
echo \'<input type ="reset">\';
echo \'</form>\';
}

    function result_display(){
global $wpdb;
$student_id = $_POST[\'rollNumber\'];
$query = "SELECT * FROM `wp_xenonresult` WHERE `student_id` = $student_id";
$result = $wpdb->get_row($query);
    echo $result->student_name;}

if(isset($_POST[\'submit\']))
{
   result_display();
}

//shortcode for the plugin begins.
function display_shortcode() {
    ob_start();
    result_display();
    html_form_code();
    return ob_get_clean();
}

add_shortcode( \'xenon_result_display\', \'display_shortcode\' );
// Enable shortcodes in text widgets
add_filter(\'widget_text\',\'do_shortcode\');
?>

1 个回复
SO网友:majick

如果我正确理解了代码,那么您希望result_display() 通过将post检查移到shortcode函数内部,可以有条件地调用函数。ie。

function display_shortcode() {
    ob_start();
    if (isset($_POST[\'submit\'])) {result_display();}
    else {html_form_code();}
    return ob_get_clean();
}