-1

I have just started learning PHP and was trying to create a responsive contact form using PHP. But the problem is that my PHP code gets printed as it is on the web page as it is and not in the required format as I need it.

Here's my code:

<!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">
    <title>Contact Us</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="styles.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/start/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-
ui.min.js"></script>
</head>
<body>
<!-- <img src="call.jpg" id="background"> -->
    <div id="container-fluid">  
        <div class="row">
            <div class="col-sm-offset-1 col-sm-10 contactform">
                <h1>Contact Us:</h1>
                <?php
                    $name = $_REQUEST["name"];
                    $email = $_REQUEST["email"];
                    $message = $_REQUEST["message"];
                    $submit = $_REQUEST["submit"];
                    $to = "abc1996@xyz.com";
                    $var = rand(1,1000);
                    $subject = 'Complaint Registered. Complaint No.: '. echo "$var";
                    if($submit)
                    {
                        if(!$name)
                            $errors = $errors."<p><strong>Name missing!</strong></p>";
                        else
                            $name = filter_var($name,FILTER_SANITIZE_STRING);
                        if(!$email)
                            $errors = $errors."<p><strong>Email missing!</strong></p>";
                        else
                        {
                            $name = filter_var($email,FILTER_SANITIZE_STRING);
                            if(!filter_var($email,FILTER_VALIDATE_EMAIL))
                            {
                                $errors = $errors."Please give a valid Email-Address!";
                            }
                        }
                        if(!$message)
                        {
                            $errors = $errors."<p><strong>Message box can't be empty!</strong></p>";
                        }
                        else
                        {
                            $message = filter_var($message,FILTER_SANITIZE_STRING);
                        }
                        if($errors)
                        {
                            $finalmsg = "<div class='alert alert-danger'> ".$errors "</div>";
                        }
                        else
                        {
                            $date = date('d MM YY');
                            $content = "Hi $name. Thank you for your complaint. Your complaint has been registered on $date and your complaint number is: $var";
                            if(mail($to, $subject, $content))
                            {
                                $finalmsg = '<div class="alert alert-success">Your mail has been sent and we will REQUEST back to you asap</div>';
                            }
                            else
                            {
                                $finalmsg = '<div class="alert alert-warning">Error Sending Mail. Try again later!</div>';
                            }
                        }
                        echo $finalmsg;
                    }
                ?>
                <form action="" method="post">
                    <div class="form-group">
                        <label for="name">Name &#42: </label>
                        <input type="text" name="name" id="name" placeholder="Enter your Name " class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="email">Email &#42:</label>
                        <input type="email" name="email" id="email" placeholder="Enter your Email " class="form-control">
                    </div>
                    <div class="form-group">
                        <label for="message">Message &#42:</label>
                        <textarea id="message" name="message" class="form-control" rows="5"></textarea>
                    </div>
                    <input type="submit" name="submit" class="btn btn-success btn-lg" value="Send Message" id="submit">
                </form>
            </div>
        </div>
    </div>
</body>

Also, it would be great if you help me in generating a random number and use it in my code. I have tried it in my code, please tell me whether it is correct or not.

Thank You

ankastic
  • 171
  • 2
  • 14
  • 6
    does your file have a .php extension? Is PHP installed and running on the webserver? – ADyson Aug 11 '17 at 09:14
  • as for your question about your random number generator, what do you define as "correct"? Describe how you expect it to behave. Then tell us, from your testing, does it do what you expect? If so then it's probably correct for your purpose. If it doesn't, tell us what it _does_ do instead. – ADyson Aug 11 '17 at 09:15
  • run it in the wamp server i fyou using windows – Sugumar Venkatesan Aug 11 '17 at 09:15
  • 3
    One last thing, personally I would obfuscate your email address in the example above unless you want bots to trawl this page and spam you for years to come :-) – ADyson Aug 11 '17 at 09:16
  • 2
    PHP requires a server to run, since it is essentially a server-side language. Simply loading it as a static file will not work: try looking into server stacks, like WAMP/MAMP/AMPPS – Terry Aug 11 '17 at 09:16
  • @ADyson yes it does have a .php extension. And for the random number generator, I was asking about my implementation, whether it is correct or not. :P – ankastic Aug 11 '17 at 09:25
  • @Terry I have XAMPP installed on my laptop. How do I proceed to run my file on that server? – ankastic Aug 11 '17 at 09:29
  • @AnkushSoni "I was asking about my implementation, whether it is correct or not". I know. As I already mentioned, you can determine that by testing it. Again, we don't know what exactly you want it to do, because you haven't said. If it doesn't work as you expected, then you can explain to us what the problem is. Just asking "is it correct" is an unanswerable question without knowing the precise requirements. – ADyson Aug 11 '17 at 09:32
  • @AnkushSoni, if your server is xampp then you have to put your .php file in xampp>htdocs try it – Puja Aug 11 '17 at 09:32
  • @AnkushSoni to get your file running in the webserver you have to put the file in whatever folder you configured as your website root when you installed/configured it (or a subfolder of that), and if the server is running (on port 80, hopefully) then from the same machine you can go to something like `http://localhost/myscript.php` in your browser to run it. – ADyson Aug 11 '17 at 09:34

1 Answers1

0

Is this page served by a web server and does it have a .php extension?

PHP is a preprocessor which means that it needs to be run by a web server. Try WAMP (Windows) or MAMP (for Mac), rename your file to .php and retry.

Fotis
  • 1,322
  • 16
  • 30