简体   繁体   中英

contact form in email not returning values

I'm using CKEditor to create a html mailer in which a contact form is being sent to email.
The problem is, there is no value being received on submission of that form in email.

Contact Form in E-Mail (code)

<form action="http://techindiainfotech.com/mail.php" method="post" name="test">
    <p>Your Name: <input maxlength="75" name="name" size="75" type="text" /></p>

    <p>Mobile Number: <input maxlength="10" name="mobile" size="10" type="text" /></p>

    <p>Business Name: <input maxlength="100" name="business" size="100" type="text" /></p>

    <p><input name="sub" type="submit" value="Submit" /></p>
</form>

Handler - mail.php

if ($_POST['sub'] != '') {
    unset($_POST['sub']);
    echo "Details received:<br>";
    foreach ($_POST as $val) {
        echo "$val<br>";
    }
} else {
    header("Location: http://www.techindiainfotech.com/files/contact_us.php");
    exit();
}

Screenshot from gmail's Message Text Garbled

消息文本乱码

if ($_POST['sub'] != '') {
    unset($_POST['sub']);

The above code means: if $_POST['sub'] is not an empty string, evaluate the statements below.

If your form wasn't submitted, $_POST['sub']; will be undefined and PHP will throw an error saying Undefined index .

I'd use isset() instead to properly check if the form was submitted or not.

if (isset($_POST['sub'])) {
    # code ...
}

The following should work:

if (isset($_POST['sub'])) 
{
    unset($_POST['sub']);
    echo "Details received:<br>";
    foreach ($_POST as $val) 
    {
        echo "$val<br>";
    }
}

Your form is so simple and the $_POST loop, that it narrows down the error sources:

  • file base: scripts are not in the folder you expect
  • CKEditor throws out HTML, either you strip it or,... have a look into the HTML sourcecode.
  • Use print_r($_POST); at the beginning of mail.php
  • enable PHP debugging / error reporting: http://blog.flowl.info/2013/enable-display-php-errors/
  • if you have javascript we cannot see in your sample code, remove it for further testing

Update:

  • the CKEditor changes your inputs in a way that they are not anymore labeled by name attributes or renders the form in any other invalid form (don't think that's the problem)

I copied your sample code onto my webserver and it's working. You might have something in your real code that doesn't appear in the code above.

Everything was fine except for the one, the form action attribute.
I'm submitting the form to http://techindiainfotech.com/mail.php but due to .htaccess it is being redirected to http://www.techindiainfotech.com/mail.php and that's why the request has been lost (I'm not getting the appropriate word here).

So, I just need to change in my action attribute which is, submit my form to http://www.techindiainfotech.com/mail.php not to http://techindiainfotech.com/mail.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM