Page 1 of 1

PHP Script -- Return E-mail...

Posted: Mon May 23, 2011 6:45 pm
by fr0z3ng33k
Three Questions:
1) Where do I upload my .php script so that when you click the "Submit" button on my contact us page the script returns the data to my e-mail address?

2) Once "Submit" is selected how do I get it to direct you to this page "http://www.mysitename.com/index.php?page=thank-you" Basically with the code I supplied how do I write this in?
I see the sections on where I am to write this all the way at the bottom of the code, just no idea how to put it in there correctly.

3) I have code for a drop down box with many options to choose from, if I add that code into the html portion how do I add it to the php script so it's recognized?

Below is the code, it wouldn't let me upload the file. So I had to copy / paste it.

Also once this gets running perfectly I will supply everything html / php for those who need this same Submit Button feature.

Code: Select all

<?php

// get posted data into local variables
$EmailFrom = "info@mysitename.com";
$EmailTo = "me@mysitename.com";
$Subject = "Thank you for your interest";
$FirstName = Trim(stripslashes($_POST['FirstName'])); 
$LastName = Trim(stripslashes($_POST['LastName'])); 
$Title = Trim(stripslashes($_POST['Title'])); 
$Company = Trim(stripslashes($_POST['Company'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Phone = Trim(stripslashes($_POST['Phone'])); 
$Address = Trim(stripslashes($_POST['Address'])); 
$City = Trim(stripslashes($_POST['City'])); 
$State/Province = Trim(stripslashes($_POST['State/Province'])); 
$Zip = Trim(stripslashes($_POST['Zip'])); 
$Comments = Trim(stripslashes($_POST['Comments'])); 

// validation
$validationOK=true;
if (Trim($FirstName)=="") $validationOK=true;
if (Trim($LastName)=="") $validationOK=true;
if (Trim($Title)=="") $validationOK=true;
if (Trim($Company)=="") $validationOK=true;
if (Trim($Email)=="") $validationOK=false;
if (Trim($Phone)=="") $validationOK=true;
if (!is_numeric($Phone)) $validationOK=true;
if (Trim($Address)=="") $validationOK=true;
if (Trim($City)=="") $validationOK=true;
if (Trim($State/Province)=="") $validationOK=true;
if (Trim($Zip)=="") $validationOK=true;
if (!is_numeric($Zip)) $validationOK=true;
if (Trim($Comments)=="") $validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "FirstName: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "LastName: ";
$Body .= $LastName;
$Body .= "\n";
$Body .= "Title: ";
$Body .= $Title;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "City: ";
$Body .= $City;
$Body .= "\n";
$Body .= "State/Province: ";
$Body .= $State/Province;
$Body .= "\n";
$Body .= "Zip: ";
$Body .= $Zip;
$Body .= "\n";
$Body .= "Comments: ";
$Body .= $Comments;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Re: PHP Script -- Return E-mail...

Posted: Mon May 23, 2011 9:45 pm
by Nullig
Just use the FormBuilder module.

Nullig

Re: PHP Script -- Return E-mail...

Posted: Mon May 23, 2011 11:02 pm
by fr0z3ng33k
I was I just would rather code this on my own since I can edit the sizes of the text boxes and edit the coding myself for practice. Thanks though, plus I struggle with form builder on certain things.

Re: PHP Script -- Return E-mail...

Posted: Tue May 24, 2011 11:53 am
by Jos
You can create a UDT to build your own form. Note that you have to leave the php start and closing tags out of your code.

Here's some example code for a simple form with just a submit button:

Code: Select all

if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['MySubmit']))
{      
   // do your php stuff and mail function

   echo 'Form is submitted';

}
else
{
   echo '<form action="" method="post">';
   echo '<input type="submit" name="MySubmit" value="Submit" />';
   echo '</form>';
}

Re: PHP Script -- Return E-mail...

Posted: Tue May 24, 2011 12:33 pm
by fr0z3ng33k
Jos wrote:You can create a UDT to build your own form. Note that you have to leave the php start and closing tags out of your code.
I guess I should have mentioned that I am a newb and I had failed a creating a form using Formbuilder already (just didn't look like the one I already have that I'm replacing).

Really I just want to know where to put that code to take the person to the next page once Submit is clicked and where to attach the php file on the server. If someone knows the dropdown box answer in php even better.

Thank you though for the info, I'm sure if I wasn't so new to this I'd be better off with that information.

Re: PHP Script -- Return E-mail...

Posted: Tue May 24, 2011 12:53 pm
by Jos
UDT = User Defined Tag, check admin menu Extensions. A UDT in itself has nothing to do with Formbuilder.
fr0z3ng33k wrote:I was I just would rather code this on my own
I don't know if I understood you correctly, but if you want to code the form yourself, but you don't have any basic understanding of php, I don't think you will get any other answer than to use Formbuilder.

For the redirect maybe the {redirect_page} tag may be of use to you. Check Extensions -> Tags for the help on this tag.

Re: PHP Script -- Return E-mail...

Posted: Tue May 24, 2011 2:49 pm
by fr0z3ng33k
Jos wrote:UDT = User Defined Tag, check admin menu Extensions. A UDT in itself has nothing to do with Formbuilder.
fr0z3ng33k wrote:I was I just would rather code this on my own
I don't know if I understood you correctly, but if you want to code the form yourself, but you don't have any basic understanding of php, I don't think you will get any other answer than to use Formbuilder.

For the redirect maybe the {redirect_page} tag may be of use to you. Check Extensions -> Tags for the help on this tag.
Thanks I wasn't sure what the UDT mean't. I'm looking at the extensions / tags to get a better understanding of this stuff.

What I did was find a site that allows you to pick what you want in a form and it generates the html code for the form and the php script for your server. I am now just editing that code to add a drop down with 24 choices and getting it to redirect to another page. I apologize if this isn't clear, I am new to all of this and I learn / read every day to get better.

Formbuilder was awesome, don't get me wrong I like it. I just couldn't align other text on the left of the form easily, and the text boxes were all aligned but 2 and I couldn't figure out how to even those out and the submit button wouldn't work for me. Thus the reason for me trying to code this without the use of the wonderful CMS modules.