-1

I am trying to save form data to mysql database for last 2 days, however, the form data doesn't gets saved to mysql database, instead it downloads the User Info.php code file to my computer every time I press submit button. Need assistance! This is html form;

<form class="form-horizontal" role="form" action="User Info.php" method="post"/>
    <div class="form-group">
      <label class="control-label col-sm-2" for="First Name">First Name:</label>
      <div class="col-sm-10">
        <input type="text" name="FirstName" class="form-control" id="First Name"  placeholder="First Name">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2" for="Last Name">Last Name:</label>
      <div class="col-sm-10">
        <input type="Last Name" name="LastName" class="form-control" id="Last Name" placeholder="Last Name">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2" for="Company Name">Company Name:</label>
      <div class="col-sm-10">
        <input type="Company Name" name="CompanyName" class="form-control" id="Company Name" placeholder="Company Name">
      </div>
    </div>
    <div class="form-group">
      <label class="control-label col-sm-2" for="email">Email:</label>
      <div class="col-sm-10">
        <input type="Email" name="Email" class="form-control" id="Email" placeholder="Email">
      </div>
    </div>
    <div class="form-group">
        <label class="control-label col-sm-2" for="Message">Message</label>
       <div class="col-sm-10"> 
        <textarea type="Message" name="Message" class="form-control" rows="3" columns="50" id="Message" placeholder="Message"></textarea></div>
        </div>

  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
    <button name="submit" class="submit-btn" type="submit">Submit</button>

This is php code db.php;

<?php
    $conn = mysql_connect('localhost','root','....', 'testforlivesite') or die("error while connecting to the database");
    $db = mysql_select_db('testforlivesite', $conn) or die("error while connecting to the database");
?>

User Info.php

<?php
    include_once('db.php');

    $FirstName = $_POST['FirstName'];
    $LastName = $_POST['LastName'];
    $CompanyName = $_POST['CompanyName'];
    $Email = $_POST['Email'];
    $Message = $_POST['Message'];

    if(mysql_query("INSERT INTO ContactUs (FirstName, LastName, CompanyName, Email, Message) VALUES ('$FirstName', '$LastName', '$CompanyName', '$Email', '$Message')"))
        echo "Thanks for contacting!";
    else 
        echo "Please rewrite the message.";
?>
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
Raza
  • 45
  • 8
  • 1
    This may help you: http://stackoverflow.com/questions/18422140/apache-is-downloading-php-files-instead-of-displaying-them – Marc May 16 '15 at 07:14
  • 1
    Also do not have spaces in your filename/action – mplungjan May 16 '15 at 07:18
  • while reading the link, how can I check in httpd.conf if my php module is running. I have oppened httpd.conf file, what to do next? – Raza May 16 '15 at 07:52
  • 1. Have you done anything to `..htcaccess` file ? 2. Never name any file (php file specificallly) with spaces. – Bhavesh G May 16 '15 at 09:11

1 Answers1

0

change your file name from User Info.php to UserInfo.php remove the space and also rename your file from User Info.php to UserInfo.php.

<form class="form-horizontal" role="form" action="UserInfo.php" method="post"/>
        <div class="form-group">
          <label class="control-label col-sm-2" for="First Name">First Name:</label>
          <div class="col-sm-10">
            <input type="text" name="FirstName" class="form-control" id="First Name"  placeholder="First Name">
          </div>
        </div>
        <div class="form-group">
          <label class="control-label col-sm-2" for="Last Name">Last Name:</label>
          <div class="col-sm-10">
            <input type="Last Name" name="LastName" class="form-control" id="Last Name" placeholder="Last Name">
          </div>
        </div>
        <div class="form-group">
          <label class="control-label col-sm-2" for="Company Name">Company Name:</label>
          <div class="col-sm-10">
            <input type="Company Name" name="CompanyName" class="form-control" id="Company Name" placeholder="Company Name">
          </div>
        </div>
        <div class="form-group">
          <label class="control-label col-sm-2" for="email">Email:</label>
          <div class="col-sm-10">
            <input type="Email" name="Email" class="form-control" id="Email" placeholder="Email">
          </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-2" for="Message">Message</label>
           <div class="col-sm-10"> 
            <textarea type="Message" name="Message" class="form-control" rows="3" columns="50" id="Message" placeholder="Message"></textarea></div>
            </div>

      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
        <button name="submit" class="submit-btn" type="submit">Submit</button>
Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
  • I updated the filename and also the html form, however, the result is same. I am very new to php and appache server so may be I am not able to set php server in xampp. My appache server is running and mysql server also. – Raza May 16 '15 at 07:51
  • if you have done that i recommended to do then i do not see any reason for UserInfo.php file to download instead of executing the script. Can you tell me the extension of the file that is downloading, i also suspect that in your code somewhere you have an header type set up that is causing the file to download. – Sourabh Kumar Sharma May 16 '15 at 08:12
  • Have you done something with `.htaccess` file ? – Bhavesh G May 16 '15 at 09:08
  • The problem solved. I was not including the file to htadocs in xampp folder. Now, I have done that and its working perfect. Thanks to all – Raza May 18 '15 at 16:04
  • @Raza, i am glad that finally you have your code running successfully. – Sourabh Kumar Sharma May 18 '15 at 16:07
  • Now in the form, I have put "required" in all the input boxes, however, when the submit button is pressed, it sends all the data to database without doing validation and displays the required message later. I checked few websites, however, didn't find a way out. – Raza May 19 '15 at 11:48
  • are you following this syntax: ``? – Sourabh Kumar Sharma May 19 '15 at 11:59