www.webdeveloper.com
Results 1 to 7 of 7

Thread: Contact form

  1. #1
    Join Date
    Oct 2013
    Posts
    38

    Contact form

    Hey guys,

    so I have this issue. I need to make a contact form for my website... Here's the code for a really basic email form:
    Code:
    <?php 
    if($_POST['submit']) {
    	if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
    		$error = true;
    	} else {
    	
    		$to = "mymail@yahoo.com";
    		$name = trim($_POST['name']);
    		$email = trim($_POST['email']);
    		$comments = trim($_POST['comments']);
    		
    		$subject = "Contact Form";
    		
    		$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
    		$headers = "From:" . $name;
    		$mailsent = mail($to, $subject, $messages, $headers);
    		
    		if($mailsent) {
    			$sent = true;
    		}
    	}
    }
    
    ?>
    
    
    <!doctype html>
    <html lang='en'>
    <head> 
    	<meta charset='utf-8'>
    </head>
    <body>
    	<div id='wrapper'>
    	<?php if($error == true) { ?>
    	<p class='error'>Error</p>
    	<?php } if ($sent == true) { ?>
    	<p class='sent'>Message sent</p> 
    	<?php } ?>
    	<div id='contactform'>
    		<form name='contact' action='<?php echo $_SERVER['PHP_SELF']; ?> method='post'>
    			<label for='name'>Name:</label>
    			<input type='text' name='name' />
    			<label for='email'>Email:</label>
    			<input type='email' name='email' />
    			<label for='comments'>Comments:</label>
    			<textarea name='comments'></textarea>
    			<input type='submit' name='submit' class='submit' value='Submit' />
    		</form>
    	</div>
    	</div>
    </body>
    </html>
    But, when I upload the file to my test site and click the submit button, it doesn't work. In the current state I didn't even expect it to work, because there is no command for the submit button to do. I did it from a tutorial, because my php knowledge is still at 0

    What do I have to do in order to have a functional emailing form?
    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    Many mail servers only allow the From value to be a valid email address on that server, so you may need to set the headers something like:
    PHP Code:
    $headers "From: valid_email_address_here\r\nReply-To: $name"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2013
    Posts
    38
    This is the error I get, after 'submitting' the form.
    Code:
    Not Found
    
    The requested URL /form.php method= was not found on this server.
    
    Apache/2.2.22 (Debian) Server at fld.php5.sk Port 80
    Can it be a host server issue? Also, it seems to me, that there's some information missing, regarding the submit button. How does it know what to do?
    But again, this is the first time I do something like this, so I am just guessing..

  4. #4
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    437
    <form name='contact' action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
    ..
    Chris, Senior Developer,
    www.Chrisranjana.com

  5. #5
    Join Date
    Oct 2013
    Posts
    38
    Love you!

    I see I have to be careful with php syntax...

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,213
    you could have simply used the name of the form.php as well...

    HTML Code:
    <form name='contact' action='form.php' method='post'>
    and if your form was called index.php you could have

    HTML Code:
    <form name='contact' action='./' method='post'>
    Also...
    PHP Code:
    $error false;
    $sent false;
    if(isset(
    $_POST['submit'])) 
    is needed to avoid errors
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Oct 2013
    Posts
    38
    Guys,

    so I am dealing with another issue for quite some time now.
    I need to write out a message on the site stating if the submitting was succesfull or not (if some fields are not filled in for example). But I need to do it without the page refreshing. I tried different methods and tutorials, but none of them worked for me.

    Here is the current code:
    Code:
    <html>
    <head>
    	<script type='text/javascript' src='../jquery.min.js'></script>
    	<script type='text/javascript'>
    	$(function(){
    		$('input[type=submit]').click(function(){
    			$.ajax({
    				type: "POST",
    				url: "post.php",
    				data: $("#myform").serialize(),
    							
    				success: function(data){
    					$('#result').html(data);
    				}					
    				});
    			});
    		})
    	</script>
    </head>
    <body>
    	<form action="" method="post" onsubmit="return false;" id="myform">
    		Name: <input type='text' name='name' />
    		Email: <input type='email' name='email' />
    		Comments: <input type='text' name='comments' />
    		<input type='submit' name='submit' value='Post'>
    		<div id='result'></div>
    		</form>
    </body>
    </html>
    Code:
    <?php 
    
    	if($_POST['submit']) {
    	if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
    		echo "Error";
    		
    	} else {
    	
    		$to = "my mail";
    		$name = trim($_POST['name']);
    		$email = trim($_POST['email']);
    		$comments = trim($_POST['comments']);
    		
    		$subject = "Contact Form";
    		$headers = "From: $email";  
    		$messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
    		$mailsent = mail($to, $subject, $messages, $headers);
    		
    		if($mailsent) {
    			echo "Mail sent successfuly";
    		}
    	}
    }
    ?>
    I suppose this is a pretty common 'feature' on websites so I am sure many of you know how to do it.
    Thanks a lot

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles