PHP Script and Forms

~ Technical Help, Hardware, Programming, Computer Games etc

Moderator: Staff

PHP Script and Forms

Postby Bootsy Collins » Fri Jul 06, 2012 10:27 am

Thought I throw this out there and see what happens.

I'm in the midst of redesigning my wife's website and have hit a few snags. Mostly because I'm not a website designer and don't understand code very well.

I'd like to think I'm a pretty smart guy and can learn my way through most things but this has me stumped. It may be an easy fix but I just don't get it.

Here's the deal.

We have a contact form on my wife's site. Now I understand that to make this form go to our email I need a PHP script to direct it from 'form to email' and that I need to direct something server side as well.

So the form is written, it's live on the site, my server has PHP capabilities but I don't understand the rest.

Where does the PHP script go? Do I need to put some of it with the form and then some goes on the server?

Help please....any really cuz google and other tech forums aren't helping.

Thanks.
Feline Freedom Alliance
Former Pyro of MUM, DOW, SA
User avatar
Bootsy Collins
Lost Soul
 
Posts: 40
Joined: Wed May 16, 2012 1:42 am

Re: PHP Script and Forms

Postby Kaz » Fri Jul 06, 2012 11:22 am

You would submit the form to the PHP script. It could be done as two pages, form and php script, or just use the one page.

I'll write you a basic example
Programming is 10% science, 25% ingenuity and 65% getting the ingenuity to work with the science
Wars of Tonan (Monarchy Inspired Miniatures)
Armatures for Sculpting Miniatures
User avatar
Kaz
Administrator
 
Posts: 1916
Images: 45
Joined: Wed Sep 23, 2009 8:27 pm
Location: The Vinyards of Fernabergia

Re: PHP Script and Forms

Postby Bootsy Collins » Fri Jul 06, 2012 11:24 am

I'm using Dreamweaver if that helps at all Kaz.
Feline Freedom Alliance
Former Pyro of MUM, DOW, SA
User avatar
Bootsy Collins
Lost Soul
 
Posts: 40
Joined: Wed May 16, 2012 1:42 am

Re: PHP Script and Forms

Postby Kaz » Fri Jul 06, 2012 11:49 am

Dreamweaver usage should be irrelevent
Programming is 10% science, 25% ingenuity and 65% getting the ingenuity to work with the science
Wars of Tonan (Monarchy Inspired Miniatures)
Armatures for Sculpting Miniatures
User avatar
Kaz
Administrator
 
Posts: 1916
Images: 45
Joined: Wed Sep 23, 2009 8:27 pm
Location: The Vinyards of Fernabergia

Re: PHP Script and Forms

Postby Kaz » Fri Jul 06, 2012 12:07 pm

Feel free to test this out at http://monarchygame.com/contact_bootsy.php

I've tested it quickly with a different email set for $website_email and both sender and receiver arrived ok.

If you test, it might possibly flag the email as spam depending on your mail server, because obviously 'this' server is not mac.com

Code: Select all
<?php

# contactus.php

$website_name = "Bootsy Contact Form";
$website_email = "anibus@mac.com";

if(isset($_POST['contact_us']))
{
   $name = $_POST['name'];
   $email = $_POST['email'];
   $subject = $_POST['subject'];
   $message = $_POST['message'];
   
   # additional content to be added the the front of the message being sent
   # for some reason if you include tabs (for spacing purposes the email wont look right
   $addtomessage = "Message received from contact form
-------------------------------------
Name: ". $name . "
Email: ". $email . "
-------------------------------------
";
   
   # add the extra part of the message
   $newmessage = $addtomessage . $message;
   
   # basic email check
   # is there an @ sign
   $has_one_at = explode('@', $email);
   if(count($has_one_at) == 2)
   {
      # is there at least 1 dot
      $has_dot = explode('.', $has_one_at[1]);
      if(!count($has_dot) > 0)
      {
         # no '.' = spam
         $spam = true;
      }
   }
   else
   {
      #no '@' (or more than 1) = spam
      $spam = true;
   }
   
   # could do some other spam checks here
   
   if(!isset($spam))
   {   
      #Configure headers 1
      #This is going to be sent to you
      #Set from address to be the website sending the email (dont spoof the users email)
      #Set reply to to be the user who's sent the email   
      $headers = 'From: '.$website_name.' <'.$website_email.'>' . "\r\n";
      $headers .= 'Reply-To: '.$name.' <'.$email.'>' . "\r\n";
      $headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
      
      mail($website_email, $subject, $newmessage, $headers);
      
      #Configure headers 2
      #This is going to be sent to the user
      $headers = 'From: '.$website_name.' <'.$website_email.'>' . "\r\n";
      $headers .= 'Reply-To: '.$website_name.' <'.$website_email.'>' . "\r\n";
      $headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
      
      mail($email, $subject, $newmessage, $headers);
   }
}


?>


<!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=iso-8859-1" />
<title>Bootsy Contact Form</title>
</head>

<body>


<? if(isset($_POST['contact_us']) && !isset($spam)): ?>


   <p>Thanks for your email <?=$name?></p>
   
   <p>Blah Blah</p>
   
   <p>We'll be in touch soon on email <?=$email?>.</p>


<? else: ?>

   <? if(isset($spam)): ?>
   
   <div>Spammer Detected!</div>
   
   <? endif ?>

   <form method="post" action="contact_bootsy.php">
   
      <div>Name: <input type="text" name="name" value="<?=$name?>" /></div>
      <div>Email: <input type="text" name="email" value="<?=$email?>" /></div>
      <div>Subject: <input type="text" name="subject" value="<?=$subject?>" /></div>
      <div>Message:<br /><textarea name="message"><?=$message?></textarea></div>
      <div><input type="submit" name="contact_us" value="Send Message" /></div>
   
   </form>

<? endif ?>

</body>
</html>
Programming is 10% science, 25% ingenuity and 65% getting the ingenuity to work with the science
Wars of Tonan (Monarchy Inspired Miniatures)
Armatures for Sculpting Miniatures
User avatar
Kaz
Administrator
 
Posts: 1916
Images: 45
Joined: Wed Sep 23, 2009 8:27 pm
Location: The Vinyards of Fernabergia

Re: PHP Script and Forms

Postby Bootsy Collins » Fri Jul 06, 2012 12:15 pm

Thanks Kaz..appreciated.
Feline Freedom Alliance
Former Pyro of MUM, DOW, SA
User avatar
Bootsy Collins
Lost Soul
 
Posts: 40
Joined: Wed May 16, 2012 1:42 am

Re: PHP Script and Forms

Postby Kaz » Fri Jul 06, 2012 12:16 pm

does it all make sense?
Programming is 10% science, 25% ingenuity and 65% getting the ingenuity to work with the science
Wars of Tonan (Monarchy Inspired Miniatures)
Armatures for Sculpting Miniatures
User avatar
Kaz
Administrator
 
Posts: 1916
Images: 45
Joined: Wed Sep 23, 2009 8:27 pm
Location: The Vinyards of Fernabergia

Re: PHP Script and Forms

Postby Kaz » Fri Jul 06, 2012 12:22 pm

btw, i'd do more spam checks that those otherwise you'll start getting a lot of automated crap.

things to check for in the message window are blank messages, web addresses etc

could also check the email address (after @) to be sure it's a valid domain name etc

I'd not bother going overboard on checking the format of the email. There are loads of expressions out there that 'claim' to do it, but to be honest they all reject certain valid email addresses.

I find the basic check for a n '@' and a '.' is good enough, and a dns check doesn't hurt http://php.net/manual/en/function.checkdnsrr.php
Programming is 10% science, 25% ingenuity and 65% getting the ingenuity to work with the science
Wars of Tonan (Monarchy Inspired Miniatures)
Armatures for Sculpting Miniatures
User avatar
Kaz
Administrator
 
Posts: 1916
Images: 45
Joined: Wed Sep 23, 2009 8:27 pm
Location: The Vinyards of Fernabergia

Re: PHP Script and Forms

Postby Bootsy Collins » Fri Jul 06, 2012 12:43 pm

PHP is something I have just started to learn so it makes a little sense.
I tend to learn most coding by ripping it apart and putting it back together again.

It has helped out a lot so thank you for that.

I'll experiment with the code and see what I need to change.

I'll be needing to put at least 3 different kinds of forms on my wife's site so this is a good start.

Thanks,
Feline Freedom Alliance
Former Pyro of MUM, DOW, SA
User avatar
Bootsy Collins
Lost Soul
 
Posts: 40
Joined: Wed May 16, 2012 1:42 am

Re: PHP Script and Forms

Postby Bootsy Collins » Sat Jul 07, 2012 12:25 pm

Ok, so I changed the code a bit to reflect a 'select' field in the form. When I preview it in Dreamweaver, it works perfect but when I look at it on the site, the select box comes up empty.

Think I'm missing something.

Code: Select all
<?php

# contactus.php

$website_name = "BootsyContactForm";
$website_email = "info@simcoeparents.com";

if(isset($_POST['contact_us']))
{
   $name = $_POST['name'];
   $email = $_POST['email'];
   $subject = $_POST['subject'];
   $message = $_POST['message'];
   
   # additional content to be added the the front of the message being sent
   # for some reason if you include tabs (for spacing purposes the email wont look right
   $addtomessage = "Message received from contact form
-------------------------------------
Name: ". $name . "
Email: ". $email . "
-------------------------------------
";
   
   # add the extra part of the message
   $newmessage = $addtomessage . $message;
   
   # basic email check
   # is there an @ sign
   $has_one_at = explode('@', $email);
   if(count($has_one_at) == 2)
   {
      # is there at least 1 dot
      $has_dot = explode('.', $has_one_at[1]);
      if(!count($has_dot) > 0)
      {
         # no '.' = spam
         $spam = true;
      }
   }
   else
   {
      #no '@' (or more than 1) = spam
      $spam = true;
   }
   
   # could do some other spam checks here
   
   if(!isset($spam))
   {   
      #Configure headers 1
      #This is going to be sent to you
      #Set from address to be the website sending the email (dont spoof the users email)
      #Set reply to to be the user who's sent the email   
      $headers = 'From: '.$website_name.' <'.$website_email.'>' . "\r\n";
      $headers .= 'Reply-To: '.$name.' <'.$email.'>' . "\r\n";
      $headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
     
      mail($website_email, $subject, $newmessage, $headers);
     
      #Configure headers 2
      #This is going to be sent to the user
      $headers = 'From: '.$website_name.' <'.$website_email.'>' . "\r\n";
      $headers .= 'Reply-To: '.$website_name.' <'.$website_email.'>' . "\r\n";
      $headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
     
      mail($email, $subject, $newmessage, $headers);
   }
}


?>

</body>
</html>
<!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>BootsyContactForm</title>
</head>

<body>

<? if(isset($_POST['contact_us']) && !isset($spam)): ?>


   <p>Thanks for your email <?=$name?></p>
   
   <p>Blah Blah</p>
   
   <p>We'll be in touch soon on email <?=$email?>.</p>


<? else: ?>

   <? if(isset($spam)): ?>
   
<div>Spammer Detected!</div>
   
   <? endif ?>
   <center>
<table width="450" border="2" cellspacing="2" cellpadding="2">
  <tr>
    <td>Name:</td>
    <td><input type="text" name="name" value="<?=$name?>" /></td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><input type="text" name="email" value="<?=$email?>" /></td>
  </tr>
  <tr>
    <td>Subject: </td>
    <td><select type="select" name="subject" value="<?=$subject?>" />
         <select>
               <option value="General">General</option>
                <option value="Auctions">Auctions</option>
                <option value="Advertising">Advertising</option>
                <option value="Charity">Charity</option>
            </select>         
    </td>
  </tr>
  <tr>
    <td>Message:</td>
    <td><textarea name="message" cols="40" rows="4"><?=$message?>
    </textarea></td>
  </tr>
</table>
<p>
  <input type="submit" name="contact_us" value="Send Message" />
</p>
</center>
<p>
  <? endif ?>
</p>
</body>
</html>
Feline Freedom Alliance
Former Pyro of MUM, DOW, SA
User avatar
Bootsy Collins
Lost Soul
 
Posts: 40
Joined: Wed May 16, 2012 1:42 am

Re: PHP Script and Forms

Postby Sikil » Sat Jul 07, 2012 12:35 pm

Code: Select all
<select type="select" name="subject" value="<?=$subject?>" />
         <select>
               <option value="General">General</option>
                <option value="Auctions">Auctions</option>
                <option value="Advertising">Advertising</option>
                <option value="Charity">Charity</option>
            </select>   


should be

Code: Select all
<select name="subject" />
               <option value="General">General</option>
                <option value="Auctions">Auctions</option>
                <option value="Advertising">Advertising</option>
                <option value="Charity">Charity</option>
            </select>   
"The church is near but the road is icy; the bar is far away but I'll walk carefully."
Stolen Sun Studios
User avatar
Sikil
Traveler
 
Posts: 238
Joined: Wed Jun 29, 2011 6:41 am


Return to Technical Corner

Who is online

Users browsing this forum: No registered users and 1 guest