Home > Mobile >  how to make wordpress plugin form text field take numbers
how to make wordpress plugin form text field take numbers

Time:01-02

I have a form in a plugin I am creatingin wordpress. The form is a simple test form and it has a hidden field and two text fields. If I enter a number into the text fields, it doesn't process it when I hit submit, it takes to me a page that says

It looks like nothing was found at this location. Maybe try a search?

here is the entire plugin showing the form and the action that processes the form.

<?php
/*
plugin name: deano plugin
description: deano test database to insert data into books table
author: Dean-O

*/
$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path.'/wp-load.php');
function deanoinsertdata() {
/**
 * Dean-O database insert book function
*/
global $wpdb;
if(isset($_POST['submitbtn'])){
    error_log('I am here');     
    $data=array(
        'wp_id'=>$_POST['wp_id'],
        'title'=>$_POST['title'],
        'author'=>$_POST['author'],
    );
    $table_name = 'books';
    $foundOne = 1;
    error_log('table_name = '.$table_name);
    error_log('foundOne = '.$foundOne);


    /*$wp_idin = $_POST['wp_id'];
    $titlein = $_POST['title'];
    $authorin = $_POST['author'];
    */
    $wp_idin = $data['wp_id'];
    $titlein = $data['title'];
    $authorin = $data['author'];

    error_log('wp_idin = '.$wp_idin);
    error_log('titlein = '.$titlein);
    error_log('author = '.$authorin);
    

    /*
    see if the record is already in the table
    */
    $sql = "select * from books"; 
    print $sql;
    $results = $wpdb->get_results($sql);
    foreach($results as $result) {
        if($result->wp_id==$wp_idin && $result->title==$titlein && $result->author==$authorin)
        {
            $foundOne = 0;
            error_log('foundOne = 0');
        }
    }
    //error_log('logged message');

    

    if($foundOne==1) {
        error_log('foundOne = 1 before insert');
        $resultinsert = $wpdb->insert($table_name,$data);//, $format=NULL);
        error_log('insert executed');
        error_log('resultinsert = '.$resultinsert);
        //wp_redirect( "http://localhost/tadpolewp/deano-plugin--duplicate-records/" );
        //exit();
        if($resultinsert==1) {
             //header('Location: http://localhost/tadpolewp/deano-plugin-successful/'); 
            error_log( 'successful' );
             wp_redirect( "http://localhost/tadpolewp/deano-plugin-successful/" );
             exit();
             http://localhost/tadpolewp/deano-plugin-successful/
            //error_log('Book saved 1');
            //echo "Book Saved 1";
        } else {
            //header('Location: http://localhost/tadpolewp/deano-plugin-failed/'); 
            error_log( 'failed to save' );
            wp_redirect( "http://localhost/tadpolewp/deano-plugin-failed/" );
            exit();
            //error_log('unable to save');
            //echo "Unable to Save";
        }
    } else {
        //error_log('Duplicate record found');
        //echo "Duplicate recortd found";
        //header('Location: http://localhost/tadpolewp/deano-plugin-duplicate-records/');
        error_log( 'duplicate record' );
        wp_redirect( "http://localhost/tadpolewp/deano-plugin-duplicate-records/" );
        exit();

    }

    

    

 }
?>
<form role="form" method="post">
                <div >
                      <?php
                // get current user ID, with default value, if empty
                $current_user_id = get_current_user_id();
            ?>

                    <input type="hidden" name="wp_id" value="<?php echo esc_attr( $current_user_id ); ?>" />
                </div>
<div >
    <label>Field 1</label><br>
    <input id="title" name="title" type="text" placeholder="<?php echo esc_attr( $current_user_id ); ?>"  required="">
</div>
<div >
    <label>Field 2</label><br>
    <input id="author" name="author" type="text" placeholder="Primary Author"  required="">
</div>
<div >
    <div >
        <br><input type="submit" value="Submit1"  name="submitbtn">
    </div>
 </div>
</form>


<?php
}
add_shortcode('deanoputdatain','deanoinsertdata');


?>

The only way I can get the Field 1 or Field 2 to take numbers is to change them to type="number"

Is there a varchar type that I can use?

My database has the field set as a varchar.

Thanks in advance Dean-O

CodePudding user response:

you should be set action for your form.

for example: 'test.php' or '/'.

It worked well for me.

i rewrite your code here:

<form role="form" method="post" action="{your menu slug}">
        <div >
            <?php
            // get current user ID, with default value, if empty
            $current_user_id = get_current_user_id();
            ?>

            <input type="hidden" name="wp_id" value="<?php echo esc_attr( $current_user_id ); ?>" />
        </div>
        <div >
            <label>Field 1</label><br>
            <input id="title" name="title" type="text" placeholder="<?php echo esc_attr( $current_user_id ); ?>"  required="">
        </div>
        <div >
            <label>Field 2</label><br>
            <input id="author" name="author" type="text" placeholder="Primary Author"  required="">
        </div>
        <div >
            <div >
                <br><input type="submit" value="Submit1"  name="submitbtn">
            </div>
        </div>
    </form>

CodePudding user response:

First:

You must enter the action attribute for the form tag.

If the problem was not resolved:

You may receive an error because their names are title and author! So, change them to other names and see the result

  • Related