By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,693 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,693 IT Pros & Developers. It's quick & easy.

Forms

P: 30
Hello I am having a problem with forms. I know they are easy for some people, but I am new. I am trying to make a simple feedback form. Hear is what I have so far...
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //process the email
  4. if (array_key_exists('send', $_POST)) {
  5.   $to = 'goldfishgraphics@gmail.com'; //use your own email
  6.   $subject = 'Project Idea';
  7.  
  8.   //process the $_POST variables
  9.   $name = $_POST['name'];
  10.   $email = $_POST['comments'];
  11.  
  12.   //build the message
  13.   $message = "Name: $name\n\n";
  14.   $message .= "Email: $email\n\n";
  15.   $message .= "Project Idea: $comments";
  16.  
  17.   //limit line length to 55 characters
  18.   $message = wordwrap($message, 55);
  19.  
  20.   //send it
  21.   $mailSent = mail($to, $subject, $message);
  22.   }
  23.  
  24.  ?>
  25.  
  26. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  27. "http://www.w3.org/TR/xhtmll/DTD/xhtml1-strict.dtd">
  28. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  29.  
  30. <head>
  31.  
  32. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  33.  
  34. <title>Goldfish Graphics</title>
  35.  
  36. <link type="text/css" rel="stylesheet" href="Goldfish.css" />
  37.  
  38. <style type="text/css">
  39. <!--
  40. A {text-decoration:none}
  41. -->
  42. </style>
  43.  
  44. </head>
  45. <body>
  46.  
  47. <div id="container">
  48.  
  49. <div id="banner">
  50. <img src="images/banner.jpg" alt= "Goldfish Graphics Banner Image" />
  51. </div>
  52.  
  53.  
  54. <div id="navigation">
  55. <div id="hvan">
  56. <a href="index.php">Home</a> &nbsp; &nbsp; &nbsp; &nbsp;  <a href="services.php">Services</a> &nbsp; &nbsp; &nbsp; &nbsp;  <a href="portfolio.php">Portfolio</a> &nbsp; &nbsp; &nbsp; &nbsp; <a href="contact.php">Contact</a>
  57. </div>
  58. </div>
  59.  
  60.  
  61. <div id="sidebar">
  62.  
  63. <div id="texto">
  64.  
  65. <h4 class="sidebar"> Testimonies</h4>
  66. <p>
  67. <?php include('testmonies.php'); ?>
  68.  
  69. </p>
  70.  
  71. </div>
  72. <h4>Links</h4>
  73.  
  74. <div id="links">
  75.  
  76. <div class="nav"> 
  77. <p>
  78. <a href="#">Pixel2Life</a>
  79. <br />
  80. <a href="#">Link</a>
  81. <br />
  82. <a href="#">Link</a>
  83. <br />
  84. <a href="#">Link</a>
  85. </p>
  86. </div>
  87.  
  88.  
  89. </div>
  90. </div>
  91.  
  92.  
  93.  
  94. <div id="content">
  95. <div id="text">
  96. <h2>Contact</h2>
  97. <p id="home">
  98. <form id="feedback" method="post" action="">
  99.   <p>
  100.     <label for="name">Name:</label>
  101.     <input name="security" id="security" type="text" class="name" />
  102.   </p>
  103.       <label for="email">Email:</label>
  104.     <input name="security" id="security" type="text" class="email" />
  105.   <p>
  106.     <label for="comments">Prodject Description:</label>
  107.     <textarea name="security" id="security" cols="54" rows="5">
  108.     </textarea>
  109.   </p>
  110.   <p>
  111.     <input name="send" id="send" type="submit" value="Submit" />
  112.   </p>
  113. </form>
  114.  
  115. <?php
  116. if ($_POST && !$mailSent) {
  117. ?>
  118.   <p class="warning">Sorry, there was a problem sending your message. Please check to make sure you filled out all the required entries.</p>
  119. <?php
  120.  }
  121. elseif ($_POST && $mailSent) {
  122. ?>
  123.   <p><strong>Your message has been sent. Thank you.</strong></p>
  124. <?php } ?>
  125.  
  126.  
  127.  
  128.  
  129. </p>
  130.  
  131.  
  132. <p id="news">
  133.  
  134. </p>
  135. </div>
  136. </div>
  137.  
  138. <div id="footer">
  139. </div>
  140.  
  141.  
  142.  
  143. </div>
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. </body>
  152. </html>
The problem is it's not actually sending anything to my email. This might be because I am doing it on a local host, the other thing is whenever I summit any kind of information this error message pops up at the top of my site...


Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:\wamp\www\Goldfish Graphics\contact.php on line 21

I want to know how to make that not happen, I want to know how to make the information actually go to my email, and I don't really understand how to make my forms safe from hackers. I basically went through a tutorial and found this.
Expand|Select|Wrap|Line Numbers
  1. <?
  2. function secured($val)
  3. {
  4.  
  5. if(empty($val) or strlen($val) > 40)
  6. {
  7. return false;
  8. } else {
  9. $val = strip_tags(
  10. trim(($val)
  11. )
  12. )
  13. ;
  14.  
  15. $val = escapeshellcmd($val);
  16. return stripslashes($val);
  17. }
  18. }
  19. ?>
I have that saved in a php file as security and in my code above you can see I have the name and id of the input fields as security. Does this work?
Jul 19 '07 #1
Share this Question
Share on Google+
4 Replies


pbmods
Expert 5K+
P: 5,821
Please use CODE tags when posting source code. Thanks!
Jul 19 '07 #2

pbmods
Expert 5K+
P: 5,821
Heya, Bouzy.

To get mail() to work on Windows, you have to futz around with your php.ini file.

Check out this article.
Jul 19 '07 #3

P: 22
Hey,
I found a slight problem you had with defining your variables. The way you did it, it would litterally look like this if you printed them. Name: $name
[PHP] $message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Project Idea: $comments";[/PHP]
Try doing it this way.
[PHP]$message = "Name:" . $name . "\n\n";
$message .= "Email:" . $email . "\n\n";
$message .= "Project Idea:" . $comments;[/PHP]

The way you did it would not show the contents of the variable but rather just show it as plain text. If I'm wrong, please correct me, I learnt PHP this way.
Jul 20 '07 #4

Atli
Expert 5K+
P: 5,058
This is actually not correct.

There is a difference between single quote marks '...' and double quote marks "...".
The single quote marks will always print precisely what you put into them, while the double quote marks will parse variable names into their respective values.

So basicly:
Expand|Select|Wrap|Line Numbers
  1. $var = "Hello";
  2. echo "var = $var"; // outputs: var = Hello
  3. echo 'var = $var'; // outputs: var = $var
  4.  
Jul 20 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.