I am using this code in my exit popup plugin to show a form whenever user leaves the website.But my form is not getting submitted (data is not getting inserted) i have added the myformsubmit function in functions.php file and included register.php file there on call of this function.Please suggest


function myformsubmit(){
<div class="container">

  <form role="form" method="post" name="theForm">
    <div class="form-group">
      <label for="username">Name:</label>
      <input type="text" name="username" class="form-control" id="username" placeholder="Enter name">
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" name="email" class="form-control" id="email" placeholder="Enter email">
    <div class="form-group">
      <label for="pwd">Phone:</label>
      <input type="number" name="phone" class="form-control" id="phone" placeholder="Enter phonenumber">
  <div class="form-group">
      <label for="comment">Reason to leave</label>
      <textarea class="form-control" name="reason" rows="5" id="comment"></textarea>
    <button type="submit"  name="submit" class="btn btn-default" onclick="myformsubmit()">Submit</button>


{   try{
    $handler=new PDO('mysql:host=;dbname=popup','root','');
    $handler->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);    //set attributes on pdo
catch(PDOException $e){
    echo $e->getMessage();
 die('sorry database problem');

$sql="INSERT into popup(username,email,phone,reason,posted) VALUES (?,?,?,?,NOW())";
echo "THANKYOU";

    {echo "sorry";}

  • If this is WordPress related in any way: Please format your code properly and explain why this connects to the WP API. – kaiser Sep 3 '16 at 9:04

First of all you are doing everything wrong as a WordPress developer.

  1. Make a habit of using wp_nonce and other security steps.
  2. When possible try to avoid inline JavaScript or JavaScript on the same file.
  3. Important: why you are creating new database connection..? Why you are writing database queries in your own way? Why don't you follow WordPress codex? WordPress has very simple way to do this.

After making those changes do something like this in your functions.p hp or register.php file

add_action( 'init', 'insertFn');
function insertFn(){
        //Your form processing code.

Please remember sometimes looking at the official documentation is very helpful.

  • 1
    Nonces for anonymous users are useless. They are all the same. – fuxia Aug 14 '16 at 13:59
  • @toscho : Thanks for the information. I have never checked the source code before! – Rituparna sonowal Aug 14 '16 at 23:02

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