Problem Custom Shortcode for Custom contact form
-
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)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘Problem Custom Shortcode for Custom contact form’ is closed to new replies.