Jeet Gandhi
Jeet Gandhi

Reputation: 65

Form in html not getting redirected to PHP file

I am creating a simple HTML website using bootstrap and CSS and am using PHP for form handling. I am using the POST method to submit the values of the form to a PHP file where I am trying to receive the values and printing them. However, when I press the submit button on the form, the HTML page is not getting redirected to the PHP file that is in the action element of the form. It just gets stuck on the HTML page. Please take a look at the below code and help me figure out why the values of the form are not getting submitted into the PHP file.

This is the code for the form in the HTML file:

<div class="container">
  <div class="row">
    <div class="contact-form">
      <form action="contact.php" method="post" id="contact-form" role="form">
        <fieldset>
          <div class="col-sm-6">
            <div class="form-group">
              <input type="text" name="fullname" class="form-control" id="full-name" placeholder="Full Name*" data-error="Full name is required" required>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-6">
            <div class="form-group">
              <input type="email" name="email" class="form-control" id="email" placeholder="Email Address*" data-error="Email is required" required>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-6">
            <div class="form-group">
              <input type="text" name="phone" class="form-control" placeholder="Phone number" id="phone" data-error="Phone number is required" required>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-6">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Subject*" name="subject" id="subject" data-error="Subject is required" required>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-12">
            <div class="form-group">
              <textarea cols="40" rows="10" name="message" class="textarea form-control" placeholder="Your Message" id="message" data-error="Message is required" required></textarea>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-4">
            <div class="form-group">
              <button class="btn-send" type="submit" name="submit">Send</button>
            </div>
          </div>
          <div class="col-sm-12">
            <div class='form-response'></div>
          </div>
        </fieldset>
      </form>
    </div>
  </div>
</div>

The contact.php file looks something like this:

<?php
if ( isset( $_POST['submit'] ) ) {
  $fullName = $_POST['fullname'];
  $email = $_POST['email'];
  echo 'Your name is ' . $fullName .' and your email ID is' . $email;
}
?>

I have been unable to figure out exactly what the problem is. Please take look at this code and help me figure this problem out. Thanks in advance.

Upvotes: 1

Views: 61

Answers (3)

Prasanna Mane
Prasanna Mane

Reputation: 59

put php code in same file. use this html

<div class="container">
  <div class="row">
    <div class="contact-form">
      <form action="" method="post">
        <fieldset>
          <div class="col-sm-6">
            <div class="form-group">
              <input type="text" name="fullname" class="form-control" id="full-name" placeholder="Full Name*" data-error="Full name is required" required>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-6">
            <div class="form-group">
              <input type="email" name="email" class="form-control" id="email" placeholder="Email Address*" data-error="Email is required" required>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-6">
            <div class="form-group">
              <input type="text" name="phone" class="form-control" placeholder="Phone number" id="phone" data-error="Phone number is required" required>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-6">
            <div class="form-group">
              <input type="text" class="form-control" placeholder="Subject*" name="subject" id="subject" data-error="Subject is required" required>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-12">
            <div class="form-group">
              <textarea cols="40" rows="10" name="message" class="textarea form-control" placeholder="Your Message" id="message" data-error="Message is required" required></textarea>
              <div class="help-block with-errors"></div>
            </div>
          </div>
          <div class="col-sm-4">
            <div class="form-group">
              <button class="btn-send" type="submit" name="submit">Send</button>
            </div>
          </div>
          <div class="col-sm-12">
            <div class='form-response'></div>
          </div>
        </fieldset>
      </form>
    </div>
  </div>
</div>

Upvotes: 1

Shalin Nipuna
Shalin Nipuna

Reputation: 488

Your code is perfectly working. Make sure your folders are in right place (eg : in wamp your folders must be inside www folder). And make sure your server is running.

Upvotes: 0

Rakesh
Rakesh

Reputation: 216

It should work. Have you made sure that the contact.php file is in the same folder as this html page?

Upvotes: 1

Related Questions