Jump to content


Photo

Solved - Contact Form MAYHEM!


  • Please log in to reply
11 replies to this topic

#1 wizerm

wizerm
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 August 2006 - 06:40 PM

If someone could please help me with this I would greatly appreciate it. I have been trying to figure this out for lord knows how long and haven't yet. I am new to .php for the most part and can't get this form working.

Here is a the code of the page it resides on "www.robsaudio.com/contact/index.html"

<div id="page">
        <form method="post" action="http://www.robsaudio...il/contact.php" id="contact">
          <p id="result"></p>
          <p>
            <label for="name" id="lab-name">Name:</label>
            <input name="name" type="text" class="required" id="name" tabindex="1" size="30" value="" />
          </p>
          <p>
            <label for="email" id="lab-email">Email:</label>
            <input name="email" type="text" class="validate-email required" id="email" tabindex="2" size="30" value="" />
          </p>
          <p>
            <label for="cc" id="cc-author">CC:</label>
            <label><input type="checkbox" name="cc" id="cc" /> yes, send a copy to me.</label>
          </p>
          <p>
            <label for="subject" id="lab-subj">Subject:</label>
            <input name="subject" type="text" id="subject" tabindex="3" size="30" />
          </p>
          <p>
            <label for="message" id="lab-msg">Message:</label>
            <br />
            <textarea tabindex="4" id="message" name="message" rows="10" cols="30" class="required"></textarea>
          </p>
          <p id="contol">
            <input value="Send Message" name="send" type="submit" class="btn" />
          </p>
        </form>
      </div>

I just need the form to work properly, and with a slim to non knowledge of php I am lost and need to finish this page. Thanks for any help anyone can give.

#2 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 25 August 2006 - 06:51 PM

Do you have any code written for your contact.php page?

If so, please post it here.

If not, check out the PHP Freak tutorial here for contact forms (you can leave out any MySQL stuff, as you probably don't want it):

http://www.phpfreaks...orials/68/0.php

That will get you started on learning about html form handling in PHP.

Good luck... ;)

#3 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 25 August 2006 - 06:52 PM

I don't see any code and your link gives a 404.  If you're going to use PHP, you need to start by changing the extension of the contact form to ".php".

Info: PHP Manual


#4 wizerm

wizerm
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 August 2006 - 06:56 PM

I don't have anything other than the html page that the form sits on. I messed up the contact.php so much trying to get it to work it was unuseable. Basically I need whatever will make this form work....I feel like an idiot right now....lol.

#5 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 25 August 2006 - 06:57 PM

Let us see what code you have so far for contact.php.

Otherwise, try that tutorial that I linked for you. It will give you some good concepts to work from.

#6 wizerm

wizerm
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 August 2006 - 07:01 PM

<?php
// Change the 4 variables below
$yourName = 'Kyle Stearnes';
$yourEmail = 'test@robsaudio.com';
$yourSubject = 'testJax';
$referringPage = 'http://www.robsaudio.com/contact/index.html';
// No need to edit below unless you really want to. It's using a simple php mail() function. Use your own if you want
function cleanPosUrl ($str) {
return stripslashes($str);
}
if ( isset($_POST['sendContactEmail']) )
{
$to = $yourEmail;
$subject = $yourSubject.': '.$_POST['posRegard'];
$message = cleanPosUrl($_POST['posText']);
$headers = "From: ".cleanPosUrl($_POST['posName'])." <".$_POST['posEmail'].">\r\n";
$headers .= 'To: '.$yourName.' <'.$yourEmail.'>'."\r\n";
$mailit = mail($to,$subject,$message,$headers);
if ( @$mailit ) {
header('Location: '.$referringPage.'?success=true');
}
else {
header('Location: '.$referringPage.'?error=true');
}
}
?>

I am sure there are things wrong with it know doubt. Some of the variables are wrong I know, but I messed it up so bad I don't know what to change it to...lol. I wish I knew more.

#7 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 25 August 2006 - 08:01 PM

For ease, I would recommend consolidating your contact form and the PHP into one file, which we'll call contact.php.

Let's look at the main meat of your contact form:

        <form method="post" action="http://www.robsaudio.com/workspace/mail/contact.php" id="contact">
          <p id="result"></p>
          <p>
            <label for="name" id="lab-name">Name:</label>
            <input name="name" type="text" class="required" id="name" tabindex="1" size="30" value="" />
          </p>
          <p>
            <label for="email" id="lab-email">Email:</label>
            <input name="email" type="text" class="validate-email required" id="email" tabindex="2" size="30" value="" />
          </p>
          <p>
            <label for="cc" id="cc-author">CC:</label>
            <label><input type="checkbox" name="cc" id="cc" /> yes, send a copy to me.</label>
          </p>
          <p>
            <label for="subject" id="lab-subj">Subject:</label>
            <input name="subject" type="text" id="subject" tabindex="3" size="30" />
          </p>
          <p>
            <label for="message" id="lab-msg">Message:</label>
            <br />
            <textarea tabindex="4" id="message" name="message" rows="10" cols="30" class="required"></textarea>
          </p>
          <p id="contol">
            <input value="Send Message" name="send" type="submit" class="btn" />
          </p>
        </form>

Your form is set to submit by way of POST, so you form values can be accesed on your contact.php page like so:

<?php

$_POST['name'];		// The name from the form
$_POST['email'];	// The email address from the form
$_POST['cc'];		// The cc checkbox from the form
$_POST['subject'];	// The subject from the form
$_POST['message'];	// The message from the form
$_POST['send'];		// The form send button

?>

The above code won't actually do anything, it's just a reference to the values submitted to the form. I would suggest assigning these values
to easier to remember variables, like so:

<?php

// Assign the form values to other variable names
$formName = $_POST['name'];		// The name from the form
$formEmail = $_POST['email'];		// The email address from the form
$formSubject = $_POST['subject'];	// The subject from the form
$formMessage = $_POST['message'];	// The message from the form

?>

You'll see that I didn't assign the $_POST['send'] and $_POST['cc'] to other variables because, in order to be useful to use,
these values are handled need to be handled a little differently than the others.

The $_POST['cc'] value will only be set (passed to contact.php) if the checkbox was checked, so we'll check this later
when we need it.

We'll use $_POST['send'] to determine if the html form was submitted (someone clicked the "Send Message" button), like so:

<?php

// this line will return a "TRUE" if the form was submitted
// and execute the code in the if() block
if($_POST['send']){

	// Set form variable stuff here

	// Code to build the email message goes here
	
	// Message headers here
	
	// Send the mail
}

?>

Now we can start building the email message, like so:

<?php

// Your name, email address, etc...
$yourName = 'Kyle Stearnes';
$yourEmail = 'test@robsaudio.com';

// Message headers here
$headers .= 'To: '.$yourName.' <'.$yourEmail.'>' . "\r\n";
$headers .= 'From: '.$formName.' <'.$formEmail.'>' . "\r\n";

if( isset($_POST['cc']){
	$headers .= 'Cc: '.$formEmail.'' . "\r\n";
}

?>

We set the email address that the contact form stuff will be sent to, and we set the message headers. You can see that we checked if $_POST['cc'] was set, so we'll only include the "CC" line if the user checked the "CC" checkbox on the form.

Finally, you can send your email:

<?php

// Send the mail
if( mail($yourEmail, $formSubject, $formMessage, $headers) );
	$msg = "Your email was sent!";
} else {
	$msg = "There was a problem sending your email, please try again!";
}

?>

You can see that I added an if() statement that will set a message variable ($msg) depending on if the mail was
sent successfully.

So, let's put the code all together:

<?php

// this line will return a "TRUE" if the form was submitted
// and execute the code in the if() block
if($_POST['send']){

	// Set form variable stuff here
	
	// Assign the form values to other variable names
	$formName = $_POST['name'];		// The name from the form
	$formEmail = $_POST['email'];		// The email address from the form
	$formSubject = $_POST['subject'];	// The subject from the form
	$formMessage = $_POST['message'];	// The message from the form

	// Code to build the email message goes here
	
	// Your name, email address, etc...
	$yourName = 'Kyle Stearnes';
	$yourEmail = 'test@robsaudio.com';
	
	// Message headers here
	$headers .= 'To: '.$yourName.' <'.$yourEmail.'>' . "\r\n";
	$headers .= 'From: '.$formName.' <'.$formEmail.'>' . "\r\n";
	
	if( isset($_POST['cc']){
		$headers .= 'Cc: '.$formEmail.'' . "\r\n";
	}	
	
	// Send the mail
	if( mail($yourEmail, $formSubject, $formMessage, $headers) );
		$msg = "Your email was sent!";
	} else {
		$msg = "There was a problem sending your email, please try again!";
	}
} 

// Start the contact page
?>

<html>
<body>

<?php

// Show the mail send message, if it exists
if( $msg ){
	echo $msg;
}

// Email form here
?>

   <div id="wrap">
      <h2>
        <span>Contact Rob's Audio Solutions</span>
      </h2>
      <div id="page">
        <form method="post" action="http://www.robsaudio.com/workspace/mail/contact.php" id="contact">
          <p id="result"></p>
          <p>
            <label for="name" id="lab-name">Name:</label>
            <input name="name" type="text" class="required" id="name" tabindex="1" size="30" value="" />
          </p>
          <p>
            <label for="email" id="lab-email">Email:</label>
            <input name="email" type="text" class="validate-email required" id="email" tabindex="2" size="30" value="" />
          </p>
          <p>
            <label for="cc" id="cc-author">CC:</label>
            <label><input type="checkbox" name="cc" id="cc" /> yes, send a copy to me.</label>
          </p>
          <p>
            <label for="subject" id="lab-subj">Subject:</label>
            <input name="subject" type="text" id="subject" tabindex="3" size="30" />
          </p>
          <p>
            <label for="message" id="lab-msg">Message:</label>
            <br />
            <textarea tabindex="4" id="message" name="message" rows="10" cols="30" class="required"></textarea>
          </p>
          <p id="contol">
            <input value="Send Message" name="send" type="submit" class="btn" />
          </p>
        </form>
      </div>
    </div>

  </body>
</html>

NOTE: This code does NO error checking, which I did to make the example easier. DO lots of error checking on the stuff the users submit to your forms. DON'T trust what users submit.

There is a great error checking thread stickied at the top of the "PHP Help" forum. I highly recommend you read it.

http://www.phpfreaks...ic,36973.0.html

Good luck... ;)

#8 wizerm

wizerm
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 August 2006 - 08:16 PM

Thank you so much, I really apreciate it. I have had people try to help me in the past with PHP, but they never really explained it well. You explained it and made the lightbulb in my head go off, although dim at times, it still went off and I understand the PHP behind what you said. Thanks again.

#9 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 25 August 2006 - 08:33 PM

Glad to help! ;)

You in the right place to ask questions, we're all here to help and learn!

#10 wizerm

wizerm
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 August 2006 - 09:07 PM

Here is my new contact page "www.robsaudio.com/contact/index-3.html"which load great but the form doesn't work. I made a file called contact2.php as well. As you can see the post go to contact2.php, but gives an parsing error on line 25. What did I do wrong? Thanks again.

#11 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 25 August 2006 - 09:23 PM

Here is a contact form that works...I've used it before

http://www.ibdhost.com/contact/
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#12 wizerm

wizerm
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 25 August 2006 - 09:29 PM

Thanks AdRock, I actually figured it out by going about it a different way. Changed the code around a bit. I just have to get it to give the messages about the emailing being recieved or error, to show on a correctly formatted page with the right css and images. Thanks though for the help. Eberyone on this site seems to be very helpful and hopefully I will learn enough PHP that I can be of some help waaaayyyyy down the road...lol.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users