Home > Software design >  Cannot locate problem with .php Contact page
Cannot locate problem with .php Contact page

Time:05-06

I am sure this will end up being a silly question, but hear me out please.

I am a novice developer and i'm building a website with html, css (w/ Bootstrap) and since the website has to have a contact form page, i am using php there. Originally when I was developing the pages of the website including the front-end of the Contact page. all my pages were .html. Now, following a tutorial online, i have added the php to the contact.html to have the contact from content sent to an email of my choosing, i have changed the file name from contact.html to contact.php and i have placed ALL the website files into MAMP's htdocs folder in my C drive in order to test. However, since switching to contact.php, i cannot view the page! Instead i receive an http error 500. Note that all the .html files render as they should.

No doubt somewhere along the line i have made a rudimentary mistake, but any assistance in clearing this problem is very much appreciated.

<!DOCTYPE html>

<?php
$message_sent = 'false';
if(isset($_POST['email']) && $_POST['email'] != ''){

    if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){

        $userName = $_POST['name'];
        $userEmail = $_POST['email'];
        $message = $_POST['message'];
        $messageMark = $_POST['mark'];

        $to = "[email protected]";
        $body = "";

        $body .  "From: ".$userName. "\r\n";
        $body .= "Email: ".userEmail. "\r\n";
        $body .= "Message: ".$message. "\r\n";
        $body .= "Marketing: ".$messageMark. "\r\n";

        mail($to, $body);

        $message_sent = true;
    }
}
?>

<html lang="en">
<main>
<?php
    if($message_sent):
?>

<h2>Thank you, we shall be in touch soon.</h2>

<?php
    else:
?>

<section id="contact">
<div >
    <div >
        <h1 >Contact Us</h1> 
        <hr>
        <div >
            <div >
                <form  name="form" autocomplete="off" method="POST" action="contactus.php">
                            
                    <label  for="name" style="color: white;">Name:</label>
                    <input  id="name" type="text" name="name" required>  
                                        
                    <label  for="email" style="color: white;">Email: <span  data-tooltip="Please enter your email address">?</span></label>
                    <input  id="email" type="email" placeholder="e.g. name:example.com" name="email" required>
                                            
                    <div >
                        <textarea id="query"  name="message" style="height: 250px; overflow-y: visible;"></textarea>
                        <label for="query" >Type your message here: <span  data-tooltip="Type your message here">?</span></label>
                    </div>
                    
                    <div >
                        <textarea id="query"  name="mark" style="height: 100px;"></textarea>
                        <label for="query" >How did you hear about us?:<span  data-tooltip="Just a few words on how you found our website and heard about our club">?</span></label>
                    </div>
                    <p>
                        <!--Instert recaptcha here? -->
                    </p>
                    <div >
                        <button type="submit" id="conbtn" >Send Now!</button>
                    </div>
                </form>
            </div>
        </div>  
    </div>
</div>
</section>
</main

CodePudding user response:

Instead . you have to use .= in this line: $body . "From: ".$userName. "\r\n";

CodePudding user response:

UPD: the main issue here is mail function. Look at the docs: https://www.php.net/manual/en/function.mail.php It requires 3 arguements:

string $to,
string $subject,
string $message

But in your code only 2 argements are passed: mail($to, $body);

typos:

You can look at highlighted code and see that $body .= "Email: ".userEmail. "\r\n"; looks different rather than it's neighbors.

userEmail lacks it's $ to be a variable :)


$message_sent = 'false'; => $message_sent = false;


$body . "From: ".$userName. "\r\n"; => $body .= "From: ".$userName. "\r\n";

CodePudding user response:

For the sake of clarity, here is the entirety of the code from my contact.php page which is producing an HTTP ERROR 500 response through MAMP:

<!DOCTYPE html>

<?php
$message_sent = false;
if(isset($_POST['email']) && $_POST['email'] != ''){

    if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){

        $userName = $_POST['name'];
        $userEmail = $_POST['email'];
        $message = $_POST['message'];
        $messageMark = $_POST['mark'];

        $to = "[email protected]";
        $body = "";

        $body .= "From: ".$userName. "\r\n";
        $body .= "Email: ".$userEmail. "\r\n";
        $body .= "Message: ".$message. "\r\n";
        $body .= "Marketing: ".$messageMark. "\r\n";

        mail($to, $subject, $body):

        $message_sent = true;
    }
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="images/LMRCupsc-removebg2.png">
<link 
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" 
rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"> 
</script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Beau Rivage&display=swap" 
rel="stylesheet">
<link href='//fonts.googleapis.com/css?family=Signika Negative:300,400,600' 
rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="CSS/contact.css">
<title>Largs Model Boat Club | Contact Us</title>
 </head>

<body>
<header>
    <nav >
        <div >
            <h1>Largs Model Boat Club</h1>
            <a  href="index.html"><img src="images/LMRCupsc- 
   removebg2.png" alt="Largs Model Boat Club Logo" style="width:200px;"></a>
            <ul >
                  <li >
                    <a  href="gallery.html">Gallery</a>
                  </li>
                 <li >
                    <a  href="history.html">History</a>
                  </li>
                  <li >
                    <a  href="news.html">News & Events</a>
                  </li>
                  <li >
                    <a  href="aboutlargs.html">About Largs</a>
                  </li>
                  <li >
                    <a  href="contactus.php">Contact 
   Us</a>
                  </li>
                  <li >
                    <a  href="index.html">Home</a>
                  </li>
            </ul>
        </div>
    </nav>
</header>

<main>
<?php
    if($message_sent):
?>

<h2>Thank you, we shall be in touch soon.</h2>

<?php
    else:
?>

<section id="contact">
<div >
    <div >
        <h1 >Contact Us</h1> 
        <hr>
        <div >
            <div >
                <form  name="form" autocomplete="off" method="POST" 
 action="contactus.php">
                            
                    <label  for="name" style="color: 
  white;">Name:</label>
                    <input  id="name" type="text" 
  name="name" required>  
                                        
                    <label  for="email" style="color: 
 white;">Email Address: <span  data-tooltip="Please enter your 
  email address">?</span></label>
                    <input  id="email" type="email" 
  placeholder="e.g. name:example.com" name="email" required>

                    <label  for="subject" style="color: 
  white;">Subject:</label>
                    <input  id="subject" type="text" 
  name="subject"> 
                                            
                    <div >
                        <textarea id="query"  name="message" 
  style="height: 250px; overflow-y: visible;"></textarea>
                        <label for="query" >Type your message 
 here: <span  data-tooltip="Type your message here">?</span> 
 </label>
                    </div>
                    
                    <div >
                        <textarea id="query"  name="mark" 
   style="height: 100px;"></textarea>
                        <label for="query" >How did you hear 
  about us?:<span  data-tooltip="Just a few words on how you 
  found our website and heard about our club">?</span></label>
                    </div>
                    <p>
                        <!--Instert recaptcha here? -->
                    </p>
                    <div >
                        <button type="submit" id="conbtn" >Send Now!</button>
                    </div>
                </form>
            </div>
        </div>  
    </div>
</div>
</section>

 <a href="https://en.wikipedia.org/wiki/Longship"><img 
 src="images/vikinggif.gif" id="vikinganim" alt="row, row, row your boat" 
  style="width: 300px; margin-left: 425px; margin-right: auto;"></a>

  </main>   

  <div ></div>  

  <footer>
<p id="foot_statement">Largs Model Boat Club &copy; 2022 | Designed & Developed 
by <a href="https://www.eassonweb.com" id="weblink"> EassonWeb.com</a></p><br>
<a href="https://en.wikipedia.org/wiki/International_maritime_signal_flags"><img 
src="images/LMBC_alphapng.png" id="LMBC_flags" style="width: 400px;"></a>
<div >
    <ul  id="footer-menu">
        <li >
            <a  href="gallery.html">Gallery</a>
        </li>
        <li >
            <a  href="history.html">History</a>
        </li>
        <li >
            <a  href="news.html">News & Events</a>
        </li>
        <li >
            <a  href="aboutlargs.html">About Largs</a>
        </li>
        <li >
            <a  href="index.html">Home</a>
        </li>
    </ul>
</div>
</footer>   
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>
  </body>
  
  </html>
  • Related