Contact Form Issue?

#1

Hi I’m having an issue with a contact form I’m testing sending emails without including any information in email. This is my HTML, and PHP


HTML
<!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>Untitled Document</title>
    <!-- Bootstrap -->
	<link href="css/bootstrap-4.0.0.css" rel="stylesheet">
	<link href="css/maincontact.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
  </head>
<body>
<nav class="navbar fixed-top navbar-light bg-light"> <a class="navbar-brand" href="#"><img src="images/triplelogooutlineb-01.svg" alt="" width="200" class="img-fluid"/> </a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button>
  <div class="collapse navbar-collapse  justify-content-center" id="navbarSupportedContent1">
    <ul class="navbar-nav">
      <li class="nav-item active"><a class="nav-link" href="index.html">HOME<span class="sr-only">(current)</span></a> </li>
      <li class="nav-item"> <a class="nav-link" href="about.html">ABOUT</a> </li>
	  <li class="nav-item"> <a class="nav-link" href="projects.html">PROJECTS</a> </li>
	  <li class="nav-item"> <a class="nav-link" href="contact.html">CONTACT</a> </li>
      <li class="nav-item dropdown">
<div class="dropdown-menu" aria-labelledby="navbarDropdown1"> <a class="dropdown-item" href="#">Action</a> <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a> </div>
      </li>
      <li class="nav-item"> </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
</form>
  </div>
</nav>

	
<div class="container-fluid hero"> <div class="container">
<div class="row">
      <div class="col-xl-12"><p>
        MULTIMEDIA<br>
		  DESIGN</p>
	    
     </div>
    </div>
  </div>
</div>
	
	
	
	
	
<div class="container-fluid contact">
    
  <div class="container text-center contactbg">
    <div class="row">
      <div class="col-xl-6">
		  
		   <h1 class="contactheadertext">CONTACT</h1>
		  
		  
		   <div class="contactformcontainer">
  <form action="action.php">
    <div class="row">
      <div class="column-25">
        <div class="amslabel" for="name">NAME</div>
      </div>
      <div class="column-75">
        <input type="text" id="name" name="name" placeholder="Your name..">
      </div>
    </div>
    <div class="row">
      <div class="column-25">
        <div class="amslabel" for="email">EMAIL</div>
      </div>
      <div class="column-75">
        <input type="text" id="email" name="email" placeholder="Your email..">
      </div>
    </div>
    <div class="row">
      <div class="column-25">
        <div class="amslabel" for="message">MESSAGE</div>
      </div>
      <div class="column-75">
        <textarea id="message" name="message" placeholder="Write something.." style="height:200px"></textarea>
      </div>
    </div>
    <div class="row">
      <input type="submit" value="Submit">
    </div>
  </form>
</div> 	  
</div>
        
      <div class="col-xl-6">
        <div class="row">
          <div class="col-xl-12">
            <div class="contactinfo"><h1>Email</h1><a href="mailto:my.email@gmail.com">support@example.net</a>
                
                <h1>Phone</h1>
				<p>
                 (212) 470-9588</p>
                <h1>Address</h1>
				<p>
                6480 Kissena Blvd
                 Flushing, NY 11367</p>
			
			</div>
          </div>
		  <div class="col-xl-12 googlemap"><iframe src="https://www.google.com/maps?q=6480%20Kissena%20Blvd%0AFlushing%2C%20NY%2011367&amp;output=embed&amp;hl=en&amp;z=14" class="wpcw-widget-contact-map" frameborder="0"></iframe></div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="container-fluid portfolio">
  <div class="row">
    <div class="col-xl-4 text-center"><img src="images/Huds1.gif" alt="" width="200"/>
<div class="text-portfolio">QUALITY</div>
    </div>
    <div class="col-xl-4 text-center"><img src="images/Huds2.gif" width="200" alt=""/>
<div class="text-portfolio">QUINTESSENTIAL</div></div>
    <div class="text-center col-xl-4"><img src="images/Huds1.gif" width="200" alt=""/>
<div class="text-portfolio">QUANTITTATIVE</div></div>
  </div>
</div>
<div class="container-fluid footer text-center">
  <div class="row">
    <div class="col-xl-12">
      <p>Copyright © 2019</p>
		
    </div>
    <div class="col-xl-12"><div class="socialicons">
		  
		
             <a href="index.html"><i class="fab fa-facebook-f "></i></a>
             <a href="index.html"><i class="fab fa-behance "></i> </a>
             <a href="index.html"><i class="fab fa-wix "></i> </a>
             <a href="index.html"><i class="fas fa-spinner fa-pulse fa-2x"></i></a>
        
		  
		</div></div>

  </div>
</div>




<!-- body code goes here -->


  <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script src="js/jquery-3.2.1.min.js"></script>

	<!-- Include all compiled plugins (below), or include individual files as needed -->
	<script src="js/popper.min.js"></script> 
  <script src="js/bootstrap-4.0.0.js"></script>
<dw-container-div>
<script src="/static/files/deviceClientScript.js"></script>
<script src="/static/files/IOSScrolling.js"></script>
<script src="/static/files/HTMLDOMDiff.js"></script>
<script src="/static/files/HTMLSimpleDOM.js"></script>
<script src="/static/files/HTMLTokenizer.js"></script>
<script src="/static/files/murmurhash3_gc.js"></script>
<script src="/static/files/RemoteFunctions.js"></script>
<script src="/static/files/LiveUpdateTools.js"></script>
<script>$dwJQuery = window.parent.$; window.DevicePreview_RenderSuccess = true; DW_LiveEdit_SetupDoc('<!DOCTYPE html>\n<html lang="en" data_liveedit_tagid="0000000020D25230">\n  <head data_liveedit_tagid="0000000020D25360">\n    <meta charset="utf-8" data_liveedit_tagid="0000000020D25940">\n	<meta http-equiv="X-UA-Compatible" content="IE=edge" data_liveedit_tagid="0000000020D25B80">\n	<meta name="viewport" content="width=device-width, initial-scale=1" data_liveedit_tagid="0000000020D25DC0">\n    <title data_liveedit_tagid="0000000020D26000">Untitled Document<\/title>\n    <!-- Bootstrap -->\n	<link href="css\/bootstrap-4.0.0.css" rel="stylesheet" data_liveedit_tagid="0000000020D265E0">\n	<link href="css\/main.css" rel="stylesheet" type="text\/css" data_liveedit_tagid="0000000020D26820">\n  <\/head>\n<body data_liveedit_tagid="0000000020D26B90">\n<nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light" data_liveedit_tagid="0000000020D26DF0"> <a class="navbar-brand" href="#" data_liveedit_tagid="0000000020D27050">Navbar<\/a>\n  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation" data_liveedit_tagid="0000000020D273E0"> <span class="navbar-toggler-icon" data_liveedit_tagid="0000000020D27640"><\/span> <\/button>\n  <div class="collapse navbar-collapse  justify-content-center" id="navbarSupportedContent1" data_liveedit_tagid="0000000020D279D0">\n    <ul class="navbar-nav" data_liveedit_tagid="0000000020D27C30">\n      <li class="nav-item active" data_liveedit_tagid="0000000020D27E90"> <a class="nav-link" href="#" data_liveedit_tagid="0000000020D280F0">Home <span class="sr-only" data_liveedit_tagid="0000000020D28350">(current)<\/span><\/a> <\/li>\n      <li class="nav-item" data_liveedit_tagid="0000000020D28810"> <a class="nav-link" href="#" data_liveedit_tagid="0000000020D28A70">Link<\/a> <\/li>\n	  <li class="nav-item" data_liveedit_tagid="0000000020D28F30"> <a class="nav-link" href="#" data_liveedit_tagid="0000000020D29190">Link<\/a> <\/li>\n	  <li class="nav-item" data_liveedit_tagid="0000000020D29650"> <a class="nav-link" href="#" data_liveedit_tagid="0000000020D298B0">Link<\/a> <\/li>\n	  <li class="nav-item" data_liveedit_tagid="0000000020D29D70"> <a class="nav-link" href="#" data_liveedit_tagid="0000000020D29FD0">Link<\/a> <\/li>\n      <li class="nav-item dropdown" data_liveedit_tagid="0000000020D2A490">\n<div class="dropdown-menu" aria-labelledby="navbarDropdown1" data_liveedit_tagid="0000000020D2A6F0"> <a class="dropdown-item" href="#" data_liveedit_tagid="0000000020D2A950">Action<\/a> <a class="dropdown-item" href="#" data_liveedit_tagid="0000000020D2ACE0">Another action<\/a>\n          <div class="dropdown-divider" data_liveedit_tagid="0000000020D2B070"><\/div>\n          <a class="dropdown-item" href="#" data_liveedit_tagid="0000000020D2B2D0">Something else here<\/a> <\/div>\n      <\/li>\n      <li class="nav-item" data_liveedit_tagid="0000000020D2B8C0"> <\/li>\n    <\/ul>\n    <form class="form-inline my-2 my-lg-0" data_liveedit_tagid="0000000020D2BD80">\n<\/form>\n  <\/div>\n<\/nav>\n<div class="container-fluid hero" data_liveedit_tagid="0000000020D2C370">\n  <div class="container" data_liveedit_tagid="0000000020D2C5D0">\n<div class="row" data_liveedit_tagid="0000000020D2C830">\n      <div class="col-xl-12" data_liveedit_tagid="0000000020D2CA90">\n        <p data_liveedit_tagid="0000000020D2CCF0">MULTIMEDIA<br data_liveedit_tagid="0000000020D2CF50">\n			DESIGN\n		  <\/p>\n      <\/div>\n    <\/div>\n  <\/div>\n<\/div>\n<div class="container-fluid whoweare" data_liveedit_tagid="0000000020D2D780">\n  <div class="container" data_liveedit_tagid="0000000020D2D9E0">\n    <div class="row" data_liveedit_tagid="0000000020D2DC40">\n      <div class="col-xl-4 offset-xl-0" data_liveedit_tagid="0000000020D2DEA0"><img src="..\/Dreamweaver BootStrap Exercise Files (3)\/DW Exercise Files\/Images\/graphic-old-books.jpg" alt="old books in a library" width="403" height="319" class="img-fluid" data_liveedit_tagid="0000000020D2DFD0"\/><\/div>\n      <div class="offset-xl-1 col-xl-7" data_liveedit_tagid="0000000020D2E210">\n        <p data_liveedit_tagid="0000000020D2E470">WHO WE ARE <\/p>\n        <p data_liveedit_tagid="0000000020D2E800">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim.<\/p>\n      <\/div>\n    <\/div>\n  <\/div>\n<\/div>\n<div class="container-fluid services" data_liveedit_tagid="0000000020D2F050">\n	\n	<div class="row align-content-center" data_liveedit_tagid="0000000020D2F2B0">\n    <div class="col-xl-4" data_liveedit_tagid="0000000020D2F510">\n      <div class="row" data_liveedit_tagid="0000000020D2F770">\n        <div class="col-xl-12 text-center" data_liveedit_tagid="0000000020D2F9D0"><img src="..\/Dreamweaver BootStrap Exercise Files (3)\/DW Exercise Files\/Images\/icon-1-print.png" alt="" width="157" height="153" class="img-fluid" data_liveedit_tagid="0000000020D2FB00"\/><\/div>\n        <div class="col-xl-12 text-center" data_liveedit_tagid="0000000020D2FD40">\n          <p data_liveedit_tagid="0000000020D2FFA0">PRINT<\/p>\n        <\/div>\n      <\/div>\n    <\/div>\n    <div class="col-xl-4" data_liveedit_tagid="0000000020D306C0">\n		\n      <div class="row" data_liveedit_tagid="0000000020D30920">\n        <div class="col-xl-12 text-center" data_liveedit_tagid="0000000020D30B80"><img src="..\/Dreamweaver BootStrap Exercise Files (3)\/DW Exercise Files\/Images\/icon-2-app.png" alt="" width="157" height="153" class="img-fluid" data_liveedit_tagid="0000000020D30CB0"\/><\/div>\n        <div class="col-xl-12 text-center" data_liveedit_tagid="0000000020D30EF0">\n          <p data_liveedit_tagid="0000000020D31150">APP DEV<\/p>\n        <\/div>\n      <\/div>\n    <\/div>\n    <div class="col-xl-4" data_liveedit_tagid="0000000020D31870">\n		\n      <div class="row" data_liveedit_tagid="0000000020D31AD0">\n        <div class="text-center col-xl-12" data_liveedit_tagid="0000000020D31D30"><img src="..\/Dreamweaver BootStrap Exercise Files (3)\/DW Exercise Files\/Images\/icon-3-photo.png" alt="" width="157" height="154" class="img-fluid" data_liveedit_tagid="0000000020D31E60"\/><\/div>\n        <div class="col-xl-12 text-center" data_liveedit_tagid="0000000020D320A0">\n          <p data_liveedit_tagid="0000000020D32300">PHOTOGRAPHY<\/p>\n        <\/div>\n      <\/div>\n    <\/div>\n\n<\/div><\/div>\n<div class="container-fluid portfolio" data_liveedit_tagid="0000000020D32B50">\n  <div class="row" data_liveedit_tagid="0000000020D32DB0">\n    <div class="col-xl-3" data_liveedit_tagid="0000000020D33160">\n		<img src="..\/Dreamweaver BootStrap Exercise Files (3)\/DW Exercise Files\/Images\/portfolio-1.jpg" alt="" width="293" height="220" class="img-fluid" data_liveedit_tagid="0000000020D333C0"\/>\n      <div class="text-portfolio" data_liveedit_tagid="0000000020D33600">Content for New div Tag Goes Here<\/div>\n    <\/div>\n    <div class="col-xl-3" data_liveedit_tagid="0000000020D33AC0">\n	  	<img src="..\/Dreamweaver BootStrap Exercise Files (3)\/DW Exercise Files\/Images\/portfolio-1.jpg" alt="" width="293" height="220" class="img-fluid" data_liveedit_tagid="0000000020D33D20"\/>\n      <div class="text-portfolio" data_liveedit_tagid="0000000020D33F60">Content for New div Tag Goes Here<\/div><\/div>\n    <div class="col-xl-3" data_liveedit_tagid="0000000020D342F0">\n	  	<img src="..\/Dreamweaver BootStrap Exercise Files (3)\/DW Exercise Files\/Images\/portfolio-1.jpg" alt="" width="293" height="220" class="img-fluid" data_liveedit_tagid="000000001EDE6140"\/>\n      <div class="text-portfolio" data_liveedit_tagid="000000001EDE6380">Content for New div Tag Goes Here<\/div><\/div>\n    <div class="col-xl-3" data_liveedit_tagid="000000001EDE6710">\n	  	<img src="..\/Dreamweaver BootStrap Exercise Files (3)\/DW Exercise Files\/Images\/portfolio-1.jpg" alt="" width="293" height="220" class="img-fluid" data_liveedit_tagid="000000001EDE6970"\/>\n      <div class="text-portfolio" data_liveedit_tagid="000000001EDE6BB0">Content for New div Tag Goes Here<\/div><\/div>\n  <\/div>\n<\/div>\n<div class="container-fluid footer text-center" data_liveedit_tagid="000000001EDE71A0">\n  <div class="row" data_liveedit_tagid="000000001EDE7400">\n    <div class="col-xl-12" data_liveedit_tagid="000000001EDE7660">\n      <p data_liveedit_tagid="000000001EDE78C0">Copyright © 2019<\/p>\n    <\/div>\n  <\/div>\n<\/div>\n\n<!-- body code goes here -->\n\n\n  <!-- jQuery (necessary for Bootstrap\'s JavaScript plugins) --> \n  <script src="js\/jquery-3.2.1.min.js" data_liveedit_tagid="000000001EDE8480"><\/script>\n\n	<!-- Include all compiled plugins (below), or include individual files as needed -->\n	<script src="js\/popper.min.js" data_liveedit_tagid="000000001EDE8930"><\/script> \n  <script src="js\/bootstrap-4.0.0.js" data_liveedit_tagid="000000001EDE8B90"><\/script>\n<\/body>\n<\/html>');</script>
</dw-container-div></body>
</html>

<?php
	
		$name = $_POST['name'];
		$visitor_email = $_POST['email'];
		$message = $_POST['message'];

		$email_from = 'Website';

        $email_subject = "New Form Submission";

        $email_body = "User Name: $name.\n".
                       "User Email: $visitor_email.\n".
                        "User Message: $message.\n";

$to = "my.email@gmail.com";

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
    
header("Location: contact.html")
		
?>


I’ve also tried these scripts

<?php 

if (empty($_POST) === false) {
    $errors =array ();
    
    
    $name = $_POST['name']
    $email = $_POST['email']
    $message =$_POST['message']


    if (empty($name) === true || empty ($email) === true || empty($message) === true) {
        $errors [] ='Name, email and message are required!';
    } else {
        if (filter_var ($email, FILTER_VALIDATE_EMAIL) === false) {
         $errors [] ='That\'s not a valid email address';   
        }
        if (ctype_alpha ($name) === false) {
         $errors [] ='Name must only contain letters!';   
        }
        if (empty ($errors) === true) {
         mail('my.email@gmail.com', Contact form, $message, 'From:'.$email);
            exit();
        }
    }
        
?>

I’m more of a graphic artist who knows some CSS, Flexbox, Grid, Bootstrap, Adobe Animate and a little JavaScript. I’ve never built anything in PHP other then email forms.

#2

Isn’t that what you’re doing here? What is different about this one that it does not work, when presumably the other ones you’ve done did work?

I think the problem is that in this line

  <form action="action.php">

you don’t specify method="post", and I think that means it will default to using GET instead. You could try that easily by adding

var_dump($_POST);
var_dump($_GET);
exit;

at the start of your PHP code to see what is being sent by the form.

In here

         mail('my.email@gmail.com', Contact form, $message, 'From:'.$email);

in your second bit of PHP code, you should consider that a lot of mail servers won’t allow you to send emails from an address that they’re not configured for. It’s a security thing, allowing anyone to send an email as if they’re someone else. Equally, though, your second parameter is Contact form without any quotes around it, so it’ll probably fall over with a syntax error in any case. If it’s not, that further suggests that the $_POST array does not exist, and it’s never executing that line of code.

Once you have it working, you could look at using something like PHPMailer instead of the built-in mail() function, I read on here that the latter is less reliable in real-world usage.

1 Like
#3

Can’t I just add method=“POST” to the form? Also

what is the correct syntax to validate this form? W3Schools use to have more detailed instructions on forms back in 2006. I’m not looking for advanced Form functionality. I just want to add a simple animated pop up confirmation for now so I can replace my Wordpress site because my current site is cheaper, and quicker to customize for me. I’ve seen a bunch of tutorials on YouTube but I don’t want to recode the form that I have because I have a few sites with the same form. This form is fine except for the floated button not working inside of Bootstrap. I took this form from another site that I built that doesn’t use Bootstrap.

#4

Correct, the default methos is get. To use post you must specifiy that method in the form: method="post"

The correct way to test for a post submission:-

 if($_SERVER['REQUEST_METHOD'] === 'POST'){
   // Parse the form data here...
}

Agreed, it’s easier and more reliable.

Yes, theres a good chance of it ending up the spam box even if the server does send it.

The string ‘Website’ isn’t even a valid address, it should be an address for your domain, you can always have another address as the reply address.

On an HTML point:-

Why are the labels divs and not labels?

1 Like
#5

I wondered about that, figured it was just a place-holder.

Yes, that would probably be a good thing.

#6

The other email forms that I used were a long time ago, and I forgot the specific syntax that I used. I think that the W3Schools, and W3.org’s websites used different reference material back then pertaining to forms, email, etc… I think that I renamed the classes within the form as a quicker way around Bootstrap’s CSS file and restyling issues. I basically just cut, and pasted this form from another site that I coded without Bootstrap. I was going to change the ‘Website’ already. I was just checking this code to see if the PHP worked. This site isn’t even attached to the correct domain yet. I also have a Managed Wordpress site but I don’t really like Wordpress that much to be honest as far as customizing templates the way I would like quickly, and I don’t need a CMS, and already have Bootstrap.

#7

Bootstrap’s labels are already styled, and I cut, and pasted the code from another site that is manually coded with Flexbox only. I’m aware of the label semantics, and browsers, and search engines reading the label however recoding the form later is easier for me. I just wanted to make sure I was able to incorporate a functional PHP script, and gallery first because of Bootstrap’s J Query, and JavaScript files potentially causing unknown structural issues through conflicting syntax. This is basically just a portfolio site to show off design layout ideas, flyers, logos, web mockups, drawings, etc… I don’t have a development team, and I’m not a full fledged developer myself. If someone were to ever inquire about a project beyond the scope of what I was fluent in I would hire a developer. I basically am fairly proficient in Photoshop, Illustrator, and to a lesser extent InDesign, and have used Adobe Animate, and After Effects. I also know some HTML, HTML 5, CSS, Flexbox, Grid, and Bootstrap, and a little bit of JavaScript which I remember the least besides PHP because I usually only use the language in galleries. I built a lightbox before and galleries using ActionScript also but I remember the HTML, and CSS better because I’m not really interested in building web applications, CMS’s, frameworks, gallery scripts, etc…

#8

My concerns were not about styling or search engines, it’s more about accessible and valid code that functions like it should.

1 Like
#9

I understand what you are saying but this is not an accessible site, and I don’t write much PHP so ensuring that the scripts are correct is my primary concern right now. Once my scripts are correct (JS and PHP), and my photo gallery is built, and functional, and the Javascript doesn’t conflict with Bootstrap’s Javascript I can edit the HTML, and CSS later if that were actually mandatory to send an email because I am not even sure my current layout will be the one that I show people. Right now I just want to validate the current PHP, and redirect to a confirmation page or modal when the submit button is pressed.

#10

What’s the simplest way to modify the current code as far as syntax is concerned for validation purposes?

<?php
	
		$name = $_POST['name'];
		$visitor_email = $_POST['email'];
		$message = $_POST['message'];

		$email_from = 'Website';

        $email_subject = "New Form Submission";

        $email_body = "User Name: $name.\n".
                       "User Email: $visitor_email.\n".
                        "User Message: $message.\n";

$to = "my.email@gmail.com";

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
    
header("Location: contact.html")
		


?>

As far as formatting is there a way to wrap a function around the current code without rewriting the entire code? Something like this?

<?php
if (isset($POST['email']))
    
{
	
		$name = $_POST['name'];
		$visitor_email = $_POST['email'];
		$message = $_POST['message'];

		$email_from = 'Website';

        $email_subject = "New Form Submission";

        $email_body = "User Name: $name.\n".
                       "User Email: $visitor_email.\n".
                        "User Message: $message.\n";

$to = "email@gmail.com";

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
    
header("Location: success.html")
		
}

else 

{
header("Location: failure.html")

}

?>

And is it possible to echo a URL?

<?php
	
if (isset($_REQUEST['email']))
    
{

    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST ['subject'] ;
    $message = $_REQUEST ['message'] ;
    mail("mitchell.carlos.riverandroid@gmail.com", $subject,
    $message, "From:" . $email);
    echo "URL";
}

else
    
{
   echo 
    
}
?>
#11

Off Topic:
@mrivera: the forums are crawled by search engine spiders, and very probably many less-friendly bots. Unless you want to be inundated with Spam, I suggest you remove your email address (and any other private information) before posting here. (I’ve already edited the posts above.)

#12

thank you.

#13

How you validate really depends upon what you consider to be valid data.
In the form none of a inputs have attributes for maxlength, minlegnth, pattern or required which leaves validation wide open, as if you were happy to receice any/no data wich gives us (and your users) no clue as to what you will accept as valid data or reject as invalid.

Just about any block of cade can be made into a function for reuse wherever it’s needed.
The typical syntax for a PHP function is like:-

function functionName($paramerers){

    // Some code that does something

    return $result ;
}

Then call the function like:-

$result = functionName($parameters);

Becuase you are working with $_POST data, it’s a superglobal so will be available in the scope of the function without having to pass it in as a parameter.

#14

Just want to add it’s much easier if you used a mailing libraries such as ‘PHPmailer’ just saying base on my exp.

#15

I just want to add a workable if, else statement (and not a function) sorry around my current code for now so I can finish my sites and galleries, and go back to designing better logos, mockups, design kits, etc… Unless a function is an easier method. I prefer the If else statement because I already understand the conditions but I think maybe I’m using the wrong syntax because I’ve tried different variations of the codes below, and they keep redirecting to my PHP page, and not sending mail.

<?php
if (isset($_POST['email']))
    
{
	
		$name = $_POST['name'];
		$visitor_email = $_POST['email'];
		$message = $_POST['message'];

		$email_from = 'Website';

        $email_subject = "New Form Submission";

        $email_body = "User Name: $name.\n".
                       "User Email: $visitor_email.\n".
                        "User Message: $message.\n";

$to = "email@gmail.com";

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
    
header("Location: contact.html")


}

else 

{
header("Location: index.html")

}

?>
<?php
if (isset($_POST['email']))
    
{
	
		$name = $_POST['name'];
		$visitor_email = $_POST['email'];
		$message = $_POST['message'];

		$email_from = 'Website';

        $email_subject = "New Form Submission";

        $email_body = "User Name: $name.\n".
                       "User Email: $visitor_email.\n".
                        "User Message: $message.\n";

$to = "email@gmail.com";

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
    
header("Location: contact.html")


}

else 

{
    
header("Location: index.html")

}

?>

Can I use echo to go to a url? If so what is the correct format? Basically I just want the form to go to one url if the form is filled in, and another if the form is not for now.

#16

Echo just outputs stuff into the document. To forward to a URL you can use header with location.

The first if you want is what I posted earlier:-

 if($_SERVER['REQUEST_METHOD'] === 'POST'){
   // Parse the form data here...
}
else{
   // This is not a form submission!!
  // However you want to deal with that.
}

Then you will probably want more conditions for validation purposes and one for if the mail sends or not.

#17

What’s wrong with this code?

<?php
if (isset($_POST['email']))
    
{
	
		$name = $_POST['name'];
		$visitor_email = $_POST['email'];
		$message = $_POST['message'];

		$email_from = 'Website';

        $email_subject = "New Form Submission";

        $email_body = "User Name: $name.\n".
                       "User Email: $visitor_email.\n".
                        "User Message: $message.\n";

$to = "email@gmail.com";

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
    
header("Location: contact.html")


}

else 

{
header("Location: index.html")

}

?>
#18

In what way? Does it work? If it does, then there’s not much wrong with it, except that it still uses the old mail() function.

If it doesn’t work, give us a clue.

Here’s one thing:

$headers = "From: $email_from \r\n";

$headers = "Reply-To: $visitor_email \r\n";

You are replacing $headers in the second line of code, rather than concatenating. That means you’re sending the email without a from-address.

1 Like
#19

O.K., no the page gets redirected to the PHP page when I press submit once I add the if, else statement. The code works without the if, else statement on my end.

#20

What do you mean by “on my end”? Are you testing this in two different environments and getting different results?

Did you add the method="post" to your form code?