tmyonline Posted August 8, 2007 Share Posted August 8, 2007 Guys: I built a simple contact form to receive users' comments. It does not work. Any ideas. Below is the code. I'll greatly appreciate any help. Thanks. <?php include('includes/corefuncs.php'); // nukeMagicQuotes(); if (array_key_exists('send', $_POST)) { $to = '[email protected]'; $subject = 'test'; // process the $_POST variables $name = $_POST['name']; $email = $_POST['email']; $comment = $_POST['comment']; // build message $message = "Name: $name\n\n"; $message .= "Email: $email\n\n"; $message .= "Comment: $comment"; // send it $mailSent = mail($to, $subject, $message); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form id="form1" name="form1" method="post" action=""> <label for="name">Name:</label> <input type="text" name="name" id="name" /> <br /> <br /> <label for="email">Email:</label> <input type="text" name="email" id="email" /> <br /> <br /> <label for="comments">Message:</label> <textarea name="comments" id="comments" cols="45" rows="5"></textarea> <br /> <br /> <input type="submit" name="send" id="send" value="Send message" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/ Share on other sites More sharing options...
climbjm Posted August 8, 2007 Share Posted August 8, 2007 <form id="form1" name="form1" method="post" action=""> Your not sending your form anywhere. Action needs to be set. Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/#findComment-318733 Share on other sites More sharing options...
tmyonline Posted August 8, 2007 Author Share Posted August 8, 2007 I just followed my PHP book example. I was surprised too. What should "action" be then ? Normally, "action" should take me to where I want the message to be processed. In this case, everything is on the same page. Is it the reason why the author left it blank ? Hymn... Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/#findComment-318736 Share on other sites More sharing options...
climbjm Posted August 8, 2007 Share Posted August 8, 2007 No, but that is a good question. Say your PHP script is in the file "form.php". You would enter the value "form.php" for the attribute action. example: <form id="form1" name="form1" method="post" action="form.php"> OR... There is a PHP variable that echos the name of the file that it is being executed in. So instead you could use: <form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF;?>"> This way, if you rename the file from form.php to anything else, it will always point back at itself. Here is a great tutorial. Check it out. It also gives explanations. http://www.tizag.com/phpT/examples/formex.php Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/#findComment-318739 Share on other sites More sharing options...
almightyegg Posted August 8, 2007 Share Posted August 8, 2007 You should set it to another page, or the same page but a different action eg. mypagewithform.php -> mypagewithform.php?action=processform Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/#findComment-318741 Share on other sites More sharing options...
climbjm Posted August 8, 2007 Share Posted August 8, 2007 Unlike the tutorial, I actually use: action="<?php echo $_SERVER['PHP_SELF']; ?>" Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/#findComment-318742 Share on other sites More sharing options...
tmyonline Posted August 8, 2007 Author Share Posted August 8, 2007 I tried the recommended actions but not successful! I'm concerned if anything else could be the problem beside the "action" attribute. Is my code correct? Thanks guys. Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/#findComment-318750 Share on other sites More sharing options...
m1a2x3x7 Posted August 8, 2007 Share Posted August 8, 2007 a couple of things 1. lose the array_key_exists and use if (isset ($_POST['submit'])) { 2. lose the $mailSent = just use mail($to, $subject, $message); 3. you have $comment =$_POST['comment']; but your textarea id is comments 4. use action="<?php echo $_SERVER['PHP_SELF']; ?>" for your form should work Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/#findComment-318790 Share on other sites More sharing options...
tmyonline Posted August 8, 2007 Author Share Posted August 8, 2007 Thanks everyone and particularly to "m1a2x3x7". It works now. Many thanks & appreciations !!! Link to comment https://forums.phpfreaks.com/topic/63944-solved-problem-with-contact-form/#findComment-318828 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.