0

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

login.php

<script>
function myformsubmit(){
        document.theForm.action="register.php";
        document.theForm.submit();
        }
</script>
<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>
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" name="email" class="form-control" id="email" placeholder="Enter email">
    </div>
    <div class="form-group">
      <label for="pwd">Phone:</label>
      <input type="number" name="phone" class="form-control" id="phone" placeholder="Enter phonenumber">
    </div>
  <div class="form-group">
      <label for="comment">Reason to leave</label>
      <textarea class="form-control" name="reason" rows="5" id="comment"></textarea>
    </div>
    <button type="submit"  name="submit" class="btn btn-default" onclick="myformsubmit()">Submit</button>
  </form>
</div>

registration.php

<?php
if(isset($_REQUEST['submit']))
{   try{
    $handler=new PDO('mysql:host=127.0.0.1;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');
}

    $name=$_POST['username'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $reason=$_POST['reason'];
$sql="INSERT into popup(username,email,phone,reason,posted) VALUES (?,?,?,?,NOW())";
$query=$handler->prepare($sql);
$query->execute(array($name,$email,$phone,$reason));
echo "THANKYOU";
}

else
    {echo "sorry";}

?>
1
  • 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
0

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(){
    if(isset($_REQUEST['submit'])){
        //Your form processing code.
     }
}

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

2
  • 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.