0

I cannot seem to get this to work properly, As far as i can see this is right? I might be doing it wrong but I am not sure, any help would be much appreciated

when i hit the submit button, nothing happens...

<div id="form" action="#" method="post"> <!-- form -->
<p class="form-title">Enquire Today!</p>
<p class="form-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis congue lorem, quis posuere ex. Suspendisse interdum semper urna mollis eleifend. Donec vulputate imperdiet nisi sed eleifend. Mauris vulputate quam libero, vel efficitur odio suscipit vel. Etiam efficitur lacinia dictum.<br><br>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis congue lorem, quis posuere ex. Suspendisse interdum semper urna mollis eleifend. Donec vulputate imperdiet nisi sed eleifend. Mauris vulputate quam libero, vel efficitur odio suscipit vel. Etiam efficitur lacinia dictum.</p>
<form class="form-left"> 
<p class="form-text">Name: <input type="text" name="name"></p>
<p class="form-text">Email: <input type="text" name="email"></p>
</form>
<form class="form-right">
<p class="form-text">Phone: <input type="text" name="phone"></p>
<p class="form-text">Company: <span class="form-margin"><input type="text" name="company"></span></p>
</form>
<a class="button-enquire" type="submit" name="submit" value="Submit" href="#form">Enquire Now!</a>
</div> <!-- form -->
<?php 
if(isset($_POST['Submit'])){
    $to = "placeholder";
    $from = $_POST['email'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $company = $_POST['company'];
    $subject = " - Form submission - " . $email;
    $message = "Name: " . $name . "\n\n" . "Email: " . $email . "\n\n" . "Phone: " . $phone . "\n\n" . "Company: " . $company . "\n\n" . "Submitted form on the website, follow up with a call/email";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

    }
?>

Thanks

3 Answers3

1

Form elements are made for a reason. It is not just you can put anything.

<a class="button-enquire" type="submit" name="submit"
   value="Submit" href="#form">Enquire Now!</a>

The above code is not a form element. Kindly replace it with either:

  • <button>
  • <input type="submit">

And it should work. Also, never ever check with isset($_POST["submit"]), as it is not reliable.

Finally your code should be:

<input class="button-enquire" type="submit" name="submit" value="Submit" />

Also make sure you replace your action attribute to the place where you are POSTing the stuff:

<form id="form" action="phpfilename.php" method="post">

And most important thing, please have only one <form> element. It looks like you are having multiple and nested <form> elements, which is invalid HTML. And most important, you have given the method and action to the <div>, where it doesn't even care those attributes.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • how should I check if there is something in the variable/does it exist if not with isset() ? –  Mar 10 '16 at 17:46
  • @ksno A lot of ways: `count($_POST)`, `isset($_POST["name"])`... Should I say everything? – Praveen Kumar Purushothaman Mar 10 '16 at 17:47
  • > Also, never ever check with isset($_POST["submit"]), as it is not reliable. Why did you say that ? –  Mar 10 '16 at 17:48
  • @ksno I asked you to search: http://stackoverflow.com/questions/7775512/using-ifisset-postsubmit-to-not-display-echo-when-script-is-open-is-not, http://stackoverflow.com/questions/3337682/php-upload-why-isset-postsubmit-is-always-false, http://www.vbforums.com/showthread.php?562749-PHP-Checking-if-a-form-has-been-submitted-the-correct-way, etc... – Praveen Kumar Purushothaman Mar 10 '16 at 17:50
  • I mean if you code you form correctly, not like in those questions you listed. why isnt it reliable to check if form is being submitted? –  Mar 10 '16 at 17:52
  • @ksno *There are two ways of submitting a form, one is to click the submit button, the other is to hit return, of which will envoke the submit button. The problem occurs when you hit the return button (as most people do), when using Internet Explorer (the most commonly used browser) and when the submit button does not have focus. This will submit the form, but it will not send the submit variable, which would mean the script we are using above will fail.* - Here you go. – Praveen Kumar Purushothaman Mar 10 '16 at 17:52
  • *when using Internet Explorer (the most commonly used browser)* oh come on... made me laugh. but yeah.... does this problem occurs also in other browsers? like newest chrome, firefox or opera? –  Mar 10 '16 at 17:59
  • @ksno Yes... Personally got screwed up too! `:)` Man, take my experience then. – Praveen Kumar Purushothaman Mar 10 '16 at 18:01
  • why so rude? I was just wondering why this and that, trying to learn something new, asking, communicating... screwed up... tfff –  Mar 10 '16 at 18:03
1

Check this it will give you basic idea how form works and you will get basic idea from following code and need more study how form works replace with your path phpfilename.php it will get php post values there. you have written method and action to div it will not work

<form id="form" action="phpfilename.php" method="post"> <!-- form -->
    <p class="form-title">Enquire Today!</p>
    <p class="form-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis congue lorem, quis posuere ex. Suspendisse interdum semper urna mollis eleifend. Donec vulputate imperdiet nisi sed eleifend. Mauris vulputate quam libero, vel efficitur odio suscipit vel. Etiam efficitur lacinia dictum.<br><br>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis congue lorem, quis posuere ex. Suspendisse interdum semper urna mollis eleifend. Donec vulputate imperdiet nisi sed eleifend. Mauris vulputate quam libero, vel efficitur odio suscipit vel. Etiam efficitur lacinia dictum.</p>

    <p class="form-text">Name: <input type="text" name="name"></p>
    <p class="form-text">Email: <input type="text" name="email"></p>


    <p class="form-text">Phone: <input type="text" name="phone"></p>
    <p class="form-text">Company: <span class="form-margin">
    <input type="text" name="company"></span></p>

    <button class="button-enquire" type="submit" name="submit" value="Submit" >Enquire Now!</button>
    </form>


    <?php 
    if(isset($_POST['Submit'])){
        $to = "placeholder"; // this is your Email address  
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $company = $_POST['company'];
        $subject = " - Form submission - " . $email;
        $message = "Name: " . $name . "\n\n" . "Email: " . $email . "\n\n" . "Phone: " . $phone . "\n\n" . "Company: " . $company . "\n\n" . "Submitted form on the website, follow up with a call/email";
        $headers = "From:" . $from;
        mail($to,$subject,$message,$headers);

        }
    ?>
Santosh Ram Kunjir
  • 1,074
  • 1
  • 12
  • 23
0

If you want to send a form with post method, then you need to set its method, like this:

<form action="target.php" method="post">
<!-- form elements -->
</form>

If you want to make sure you send something to your php backend, then you need to have input with name and value inside the form, like this:

<input type="submit" name="Submit" value="foobar">

The name will be the key and the value will be the value on server-side.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175