Search code examples
phpjqueryformsiframefancybox

Iframe with a submit form in a Fancybox not posting data


I'm trying to submit a form inside a fancybox. It is a simple contact us form with name and email and a submit button. I set the fancybox to iframe and it loads "download.php". This is what "download.php" has:

    <form action="sendmail.php" type="post">
                <fieldset style="width: 300px;">
            <legend>Please fill out completely:</legend>
                <span style="color: red;">* required field</span> 
                <br>
                <label for="txtName">Name:</label><br>
                <input type="text" id="txtName" style="width: 200px;" name="txtName"><span id="lblName" style="color: red;">*</span><br>
                <label for="txtEmail">Email:</label><br>
                <input type="text" id="txtEmail" style="width: 200px;" name="txtEmail"><span style="color: red;">*</span><br><br>
                <button id="btnSubmit">Submit</button>
                <input type="text" id="txtH" style="display: none;" name="txtH">
            </fieldset>
    </form>

When it submits it works properly other than "txtName" and "txtEmail" being blank. I can't seem to get the values to send. It sends an email after submitting properly otherwise which is why I need to use PHP and not jQuery. Thanks for any help!

Here is the sendmail.php code.

ini_set('SMTP','mail.######.com' ); 
ini_set('smtp_port', '25');
ini_set('sendmail_from', 'info@#####.com');

  $name = $_POST["txtName"];
  $email = $_POST["txtEmail"];
  $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

  $msgBody =  'The following information was submitted via the download form.' . PHP_EOL . PHP_EOL . 
  'Name: ' . $name . PHP_EOL .
  'Email: ' . $email . PHP_EOL .
  'URL: ' . $url . PHP_EOL;


  $to = "me@me.com";
  $subject = "A White Paper was downloaded";

  $headers = "From: info@me.com" . "\r\n";

  if (mail($to, $subject, $msgBody, $headers)) {
    header('Location: download-success.php');
  }
  else {
    header('Location: same.php');
  }

}


Solution

  • You have a mistake here,

    <form action="sendmail.php" type="post">
    

    it should be method="post" not type="post"

    <form action="sendmail.php" method="post">
    

    and also in your submit button, add type="submit"

    <button id="btnSubmit" type="submit">Submit</button>
    

    and here you don't need to set display:none; you can use type="hidden" if you want to hide the input

    <input type="hidden" id="txtH" name="txtH">
    

    and In your PHP, use isset

    if (isset($_POST['txtName']) {
        $name = $_POST["txtName"];
        //and rest of the code goes here
    }
    

    And always

    Add error reporting to the top of PHP file which will help find errors.

    error_reporting(E_ALL); //Error reporting enable
    ini_set('display_errors',1);