0

I'm using shortcode to process my HTML form. However, upon submit no result is being displayed. I'm not getting where I'm going wrong.

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

//result display form begins

function display_result_form_fields(){
    ob_start(); ?> 
        <form id="result_form" action="" method="POST">
            <fieldset>
                <p>
                    <label for="rollNumber"><?php _e('Roll Number'); ?></label>
                    <input name="rollNumber" id="rollNumber" class="required" type="number"/>
                </p>
                <p>
                    <input type="submit" value="<?php _e('Submit'); ?>"/>
                </p>
            </fieldset>
        </form>
    <?php
    return ob_get_clean();
}

function form_processing(){
if(isset($_POST['Submit'])){
    global $wpdb;
    $student_id = $_POST['rollNumber'];
    $query = "SELECT * FROM `wp_xenonresult` WHERE `student_id` = $student_id";
    $result = $wpdb->get_row($query);
    echo "Dear student, congratulations";}
}

//shortcode begins here
function result_form() {
            form_processing();
            $output = display_result_form_fields();
            return $output;
    }

add_shortcode('result_form', 'result_form'); //create shortcode
add_filter('widget_text','do_shortcode'); // Enable shortcodes in text widgets
?>
2
  • I think its because your function form_processing() is echoing the value and you are returning $output Why dont you try function result_form() { ob_start(); form_processing(); $output = display_result_form_fields(); return ob_get_clean(); }
    – bagpipper
    Commented Feb 13, 2017 at 10:39
  • @bagpipper With slight modification, this works very well. Can you please teach me proper use of ob_start() and return ob_get_clean() in this case?
    – Himanshu
    Commented Feb 13, 2017 at 11:25

2 Answers 2

1

You missed name attribute of <input type="submit" value="<?php _e('Submit'); ?>"/> HTML tag.

i.e.

It should be looks like: <input type="submit" name="Submit" value="<?php _e('Submit'); ?>"/>

Else, You have to change if condition inside function form_processing()

OLD: if(isset($_POST['Submit'])){

Replace with: if(isset($_POST['rollNumber'])){

Hope this will helps you.

1
  • Thank you. I just noticed this. I have corrected this and also added option to hide the form after submit button has been clicked. Now everything works fine.
    – Himanshu
    Commented Feb 13, 2017 at 11:20
0

With the help of @bagpipper and @AddWeb-Solution-Pvt-Ltd I finally managed to sort this issue.

<?php

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

//result display form begins

function display_result_form_fields(){
    ob_start(); ?> 
        <form id="result_form" action="" method="POST">
            <fieldset>
                <p>
                    <label for="rollNumber"><?php _e('Roll Number'); ?></label>
                    <input name="rollNumber" id="rollNumber" class="required" type="number"/>
                </p>
                <p>
                    <input type="submit" name = "submit" value="Submit"/>
                </p>
            </fieldset>
        </form>
    <?php
    return ob_get_clean();
}

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


//shortcode begins here
function result_form() {
    ob_start(); 
    if(isset($_POST['submit'])){
        form_processing();
        }
    else{
        $output = display_result_form_fields();
        return $output;
    }
    return ob_get_clean();
    }

add_shortcode('result_form', 'result_form'); //create shortcode
add_filter('widget_text','do_shortcode'); // Enable shortcodes in text widgets
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.