Namita Jamwal
Namita Jamwal

Reputation: 53

PHP files not working when using Bootstrap

I am new to Programming hence need some help. Below is the code I am using for a Contact Form using Bootstrap but my PHP code doesn't seem to work. I have been keeping "index.php" file with other files of Bootstrap. But when I run this it shows an error and doesn't run. And when I save this file as "index.html", the HTML part of the code runs but PHP files don't. In order to run PHP file in wamp server where should I save the files in Bootstrap or in Wamp?

<?php

if ($_POST['submit']) {

    if(!$_POST['name']) {
        $error="<br/>-Please enter your name";
    }

    if(!$_POST['email']) {
        $error.="<br/>- Please enter your email";
    }

    if(!$_POST['subject']) {
        $error.="<br/>- Please enter a subject";
    }

    if(!$_POST['message']) {
        $error.="<br/>- Please enter a message";
    }

    if(!$_POST['check']) {
        $error.="<br/>- Please confirm you are human";
    }

    if($error) {
        $result="Whoops, there is an error; Please correct the following: $error";
    } else {
        mail("[email protected]", "Contact message", "Name: ".$_POST['name']."
        Email: ".$_POST['name']."
        Subject: ".$_POST['subject']."
        Message: ".$_POST['message']);"

        {
        $result="Thank you, I'll be in touch shortly";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Contact Us</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>


    <section id="contact">
        <div class="container">

            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    <h1>Contact Us</h1>

                    <?php echo $result;?>

                    <p>Send a message via the form below</p>

                    <form method="post" role="form">

                        <div class="form-group">
                            <input type="text" name="name" class="form-control" placeholder="Your Name" value="<?php echo $_POST['name']; ?>">
                        </div>

                        <div class="form-group">
                            <input type="email" name="email" class="form-control" placeholder="Your email" value="<?php echo $_POST['email']; ?>"> 
                        </div>

                        <div class="form-group">
                            <textarea name="subject" rows="5" class="form-control" placeholder="Subject"><?php echo $_POST['subject']; ?></textarea>
                        </div>

                        <div class="form-group">
                            <textarea name="message" rows="5" class="form-control" placeholder="Message..."><?php echo $_POST['message']; ?></textarea>
                        </div>

                        <div class="checkbox">
                            <label>
                                <input type="checkbox" name="check">I am Human
                            </label>
                        </div>

                        <div align="center">
                        <input type="submit" name="submit" class="btn btn-secondary" value="send message"/>
                        </div>
                    </form>

                </div>
            </div>


        </div>
    </section>



    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

Upvotes: 1

Views: 3485

Answers (4)

always-a-learner
always-a-learner

Reputation: 3794

I think You simply missing the point:

After reading your question I think you just put the project files directly into the www directory of wamp. so obesely that not gone work. Also, you mention that in index.html your PHP code is not working is also obvious error that to run a PHP code you need to give that file a .php extension.

In order to run PHP file in wamp server where should I save the files in Bootstrap or in Wamp?

To run a PHP file with WAMP you need to follow this basic step:


  1. Install Wamp: Here is the all related detail about WAMP and you could download it and install on your local drive. wampserver

  2. After installing it go to the WAMP directory and find WWW this is the place where you can put your projects in.

  3. Now Simply Create your project folder. i.e test_project WAMP/WWW/test_project

  4. Now inside this project folder, you can add your project bootstrap template and your index.php. After reading your question I think you just put the project template files into www directory so please change that.

  5. Now to open this index.php you can go to your browser and type localhost/test_project. This will automatically run an index.php file of your project.

Upvotes: 1

Manh Nguyen
Manh Nguyen

Reputation: 439

You must save your file with ".php" extension (like "test.php") and save it in your wamp server folder : "c:\wamp\www".
Run your php file in browser : localhost/test.php. Hope this help you bro.

Upvotes: 0

FULL STACK DEV
FULL STACK DEV

Reputation: 15971

You need to change your extension `.html` to `.php`

If you still want to serve php files as .html create an .htaccess file in in your directory and

 `AddType application/x-httpd-php .html .htm`

it will start working

Upvotes: 0

Difster
Difster

Reputation: 3270

In order to process php, the file needs to have a .php extension and you would store it in your webroot which is the same place you keep the .html files.

If your php isn't running, you either have a php error or your webserver isn't configured properly to process php files.

Upvotes: 0

Related Questions