• Resolved anasiul05

    (@anasiul05)


    Hi!! I’m working on a custom form that we’ll be using on several pages in our website (including pop-ups). The data will be stored in a DB.

    I created the form and created a shortcode.

    The problem is if I put the short code on two or more pages and filled and submit only one form then in the database the record is stored as many times as I put the shortcode on my website.

    For example, I put the shortcode in two different pages. if I submit the form in one page then the record is stored twice in DB

    Here’s my code:

    <?php
    if ( ! defined( 'ABSPATH' ) ) exit;
    
    function create_db(){
    	global $wpdb;
    
        $table = $wpdb->prefix . "table_test"; 
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE IF NOT EXISTS $table (
            <code>id</code> mediumint(9) NOT NULL AUTO_INCREMENT,
            <code>name</code> text NOT NULL,
    		<code>company</code> text NOT NULL,
    		<code>email</code> text NOT NULL,
        UNIQUE (<code>id</code>)
        ) $charset_collate;";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
    }
    
    function form() {
        ?>
    
        <form action="<?php the_permalink(); ?>" method="post" id="subs_form">
    
    		<input type="text" name="fname" id="fname" placeholder="Name" value="<?php echo esc_attr($_POST['fname']);?>">   				
    		<input type="text" name="company" id="company" placeholder="Company" value="<?php echo esc_attr($_POST['company']); ?>">
    		<input type="text" name="email" id="email" placeholder="Email" value="<?php echo esc_attr($_POST['email']); ?>">
    		<input type="submit" name="submit_form" value="Enviar" class="et_pb_contact_submit et_pb_button"/>
        </form>	
    <?php
    }
    
    function custom_shortcode(){
    	
    	global $wpdb;
    	create_db();
    	ob_start();
        form();
    	$html = ob_get_clean();
       
    	if (isset( $_POST["submit_form"] ) ) 
    	{
    		if ($_POST["fname"]!='' && $_POST["company"]!='' && $_POST["email"]!='')
    		{	
    			$table = $wpdb->prefix."tb_colombeia";
    			$name = strip_tags($_POST["fname"], "");
    			$company = strip_tags($_POST["company"], "");
    			$email = strip_tags($_POST["email"], "");
    	
    			$wpdb->insert( 
    				$table, 
    				array( 
    					'name' => $name,
    					'company' => $company,				
    					'email' => $email,
    				)
    			);
    			$html = "<p>Form send</p>";
    		}
    		else
    			$html .= "<p>Error.</p>";
    	}	
        return $html;
    }
    
    add_shortcode('MY_SHORTCODE', 'custom_shortcode');
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Why go through all of this? Why not use a nice form plugin (like Formidable)? You really do not need to create custom tables.

    Moderator bcworkz

    (@bcworkz)

    Your handler that inserts a row in a table is not called multiple times because the shortcode exists in other content in the DB. It is called multiple times due to the same single content! Other content is unrelated to this. There are other processes at work behind the scenes that also need to run content through “the_content” filter, to which do_shortcode() is hooked. Anytime a process is required to run once and only once per request, and it is hooked into an action or filter, special handling is required to ensure it indeed only runs once.

    Since virtually all custom code is related to a hook, directly or indirectly, it is fallacious to assume it will only be called once per request. You must assume it will always be called more than once and take measures to prevent it from running more than once. Often the solution is to have a callback remove itself from a hook, but since your shortcode is only indirectly related to a hook, a different method is required. Perhaps you could set a static variable after the row is inserted. When your shortcode handler is called, it returns without doing anything if that static variable is set.

    I agree with Steve that it does not appear a separate table is warranted, but we don’t have the entire picture. Regardless of what you chose to do, always assume your code will be called more than once per request.

    Thread Starter anasiul05

    (@anasiul05)

    Hi thanks for your answers!!!

    I found the solution which was quite obvious and I don’t know why I didn’t see it before (I’m still a rookie)

    I just needed to add an ‘id’ to my form through the shortcode [my_shorcode id = form-1], [my_shortocde id = form-2], etc.

    Now it works fine (although I still have a thousand things to add)

    Thanks guys.!!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Problem Custom Shortcode for Custom contact form’ is closed to new replies.