Jump to content

Solved - Contact Form MAYHEM!


wizerm

Recommended Posts

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.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>

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.
Link to comment
Share on other sites

Do you have any code written for your [b]contact.php[/b] 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.com/tutorials/68/0.php

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

Good luck... ;)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

<?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.
Link to comment
Share on other sites

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

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

[code]
        <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>
[/code]

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

[code]
<?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

?>
[/code]

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:

[code]
<?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

?>
[/code]

You'll see that I didn't assign the [b]$_POST['send'][/b] and [b]$_POST['cc'][/b] 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 [b]$_POST['cc'][/b] value will only be set (passed to [b]contact.php[/b]) if the checkbox was checked, so we'll check this later
when we need it.

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

[code]
<?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
}

?>
[/code]

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

[code]
<?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";
}

?>
[/code]

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 [b]$_POST['cc'][/b] 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:

[code]
<?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!";
}

?>
[/code]

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

So, let's put the code all together:

[code]
<?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>
[/code]

[b]NOTE:[/b] This code does [b]NO[/b] error checking, which I did to make the example easier. [b]DO[/b] lots of error checking on the stuff the users submit to your forms. [b]DON'T[/b] 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.com/forums/index.php/topic,36973.0.html

Good luck... ;)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.