Jump to content

Recommended Posts

I have a form to collect some information, then pass it to a php file, then php collects the data and sends an email. Also, I need to redirect it to a "thank you" page.

 

Right now, the php is not sending an email and rather than going to a thank you page, it goes to the php file that collects the data (post below). (I also have a checkform javascript function to check if all fields have been filled)

 

Can anybody tell me what I am doing wrong and how to make the following work:

- the php to collect the data

- send to an email

- redirect and go to a thank you page

So new at php and any help would be greatly appreciated. Thanks!  :(

 

Here is my form:

 

<form id="InformationRequest" name="InformationRequest" method="post" action="scripts/informationprocess.php" onSubmit="return checkform(this)">
                <table width="340" border="0" cellspacing="0" cellpadding="2" align="center">
                <tr>
                  <td class="info">First Name*:</td>
                  <td class="infoInput"><input type="text" name="FirstName" id="FirstName" tabindex="0" /></td>
                </tr>
                <tr>
                  <td class="info">Last Name*:</td>
                  <td class="infoInput"><input type="text" name="LastName" id="LastName" tabindex="1" /></td>
                </tr>
                <tr>
                  <td class="info">Company*:</td>
                  <td class="infoInput"><input type="text" name="Company" id="Company" tabindex="2" /></td>
                </tr>
                <tr>
                  <td class="info">Address*:</td>
                  <td class="infoInput"><input type="text" name="Street1" id="Street1" tabindex="3" /></td>
                </tr>
                <tr>
                  <td class="info">Address 2:</td>
                  <td class="infoInput"><input type="text" name="Street2" id="Street2" tabindex="4" /></td>
                </tr>
                <tr>
                  <td class="info">City*:</td>
                  <td class="infoInput"><input type="text" name="City" id="City" tabindex="5" /></td>
                </tr>
                <tr>
                  <td class="info">State*:</td>
                  <td class="infoInput"><select name="State" id="State">
                <option value="">Pick your state</option>
                <option value="AL">Alabama</option>
                <option value="AK">Alaska</option>
                <option value="AZ">Arizona</option>
                <option value="AR">Arkansas</option>
                <option value="CA">California</option>
                <option value="CO">Colorado</option>
                <option value="CT">Connecticut</option>
                <option value="DE">Delaware</option>
                <option value="DC">District of Columbia</option>
                <option value="FL">Florida</option>
                <option value="GA">Georgia</option>
                <option value="HI">Hawaii</option>
                <option value="ID">Idaho</option>
                <option value="IL">Illinois</option>
                <option value="IN">Indiana</option>
                <option value="IA">Iowa</option>
                <option value="KS">Kansas</option>
                <option value="KY">Kentucky</option>
                <option value="LA">Louisiana</option>
                <option value="ME">Maine</option>
                <option value="MD">Maryland</option>
                <option value="MA">Massachusetts</option>
                <option value="MI">Michigan</option>
                <option value="MN">Minnesota</option>
                <option value="MS">Mississippi</option>
                <option value="MO">Missouri</option>
                <option value="MT">Montana</option>
                <option value="NE">Nebraska</option>
                <option value="NV">Nevada</option>
                <option value="NH">New Hampshire</option>
                <option value="NJ">New Jersey</option>
                <option value="NM">New Mexico</option>
                <option value="NY">New York</option>
                <option value="NC">North Carolina</option>
                <option value="ND">North Dakota</option>
                <option value="OH">Ohio</option>
                <option value="OK">Oklahoma</option>
                <option value="OR">Oregon</option>
                <option value="PA">Pennsylvania</option>
                <option value="RI">Rhode Island</option>
                <option value="SC">South Carolina</option>
                <option value="SD">South Dakota</option>
                <option value="TN">Tennessee</option>
                <option value="TX">Texas</option>
                <option value="UT">Utah</option>
                <option value="VT">Vermont</option>
                <option value="VA">Virginia</option>
                <option value="WA">Washington</option>
                <option value="WV">West Virginia</option>
                <option value="WI">Wisconsin</option>
                <option value="WY">Wyoming</option>
                </select>      
                </td>
                </tr>
                <tr>
                  <td class="info">Zip*:</td>
                  <td class="infoInput"><input type="text" name="PostCode" id="PostCode" tabindex="6" /></td>
                </tr>
                <tr>
                  <td class="info">Email*:</td>
                  <td class="infoInput"><input type="text" name="email" id="email" tabindex="7" /></td>
                </tr>
                <tr>
                  <td class="info">Phone*:</td>
                  <td class="infoInput"><input type="text" name="Tel" id="Tel" tabindex="8" /></td>
                </tr>
                <tr>
                  <td class="info"><input type="reset" name="reset" id="reset" value="Reset" /></td>
                  <td class="infoInput"><input type="submit" name="submit" id="submit" value="Submit" /></td>
                </tr>
              </table>
            </form>

Here's the php file:

<?php

// get posted data into local variables
$EmailFrom = "myemail@gmail.com";
$EmailTo = "myemail@gmail.com";
$Subject = "My Subject";
$FirstName = Trim(stripslashes($_POST['FirstName'])); 
$LastName = Trim(stripslashes($_POST['LastName'])); 
$Company = Trim(stripslashes($_POST['Company'])); 
$Street1 = Trim(stripslashes($_POST['Street1'])); 
$Street2 = Trim(stripslashes($_POST['Street2'])); 
$City = Trim(stripslashes($_POST['City'])); 
$State = Trim(stripslashes($_POST['State'])); 
$PostCode = Trim(stripslashes($_POST['PostCode'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$email = Trim(stripslashes($_POST['email'])); 
$update = Trim(stripslashes($_POST['update'])); 

if (!($FirstName && $LastName && $Company && $Street1 && $City && $State&& $PostCode && $Tel && $email)) {echo "All fields are required, please press back on your browser and complete the form."; } else {


// prepare email body text
$Body = "";
$Body .= "FirstName: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "LastName: ";
$Body .= $LastName;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Street1: ";
$Body .= $Street1;
$Body .= "\n";
$Body .= "Street2: ";
$Body .= $Street2;
$Body .= "\n";
$Body .= "City: ";
$Body .= $City;
$Body .= "\n";
$Body .= "State: ";
$Body .= $State;
$Body .= "\n";
$Body .= "PostCode: ";
$Body .= $PostCode;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "update: ";
$Body .= $update;
$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=thankyou.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=header.php\">";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/170787-form-pass-to-php-redirect/
Share on other sites

I quickly tested out your script and it does send the email just fine which means I would double check your hosting account to make sure they allow the use of the PHP mail() function, I do know that some providers disable it (Reasons of which are unknown).

 

As for the PHP data collection, that also seems to be working fine, I got all of the post fields without changing anything.

 

As for the meta redirect I would try using the full url so http://mydomain.com/thankyou.html instead of just thankyou.html

 

@DrTrans - Yes true the header function should be used over the HTML redirect but only use the header function if you are not sending out any data to the screen.

 

Eg of working header

-----

header('Location: http://blabla.com');

 

some HTML

------

 

Eg of non working header

-------

some HTML

 

header('Location: http://blabla.com');

------

 

Hope that helps a little bit.

Here is a better structure. have th emain page check if form was submitted, is so check for errors.

 

If the form was not submitted or there were post errors, display the form (with the previously submitted values)

 

If there were no errors send the email and then redirect (with header() to the appropriate page)

 

Point the user to contact.php, form.php is just an include page

 

contact.php

<?php

if (isset($_POST))
{
    // get posted data into local variables
    $EmailFrom = "myemail@gmail.com";
    $EmailTo = "myemail@gmail.com";
    $Subject = "My Subject";
    $FirstName = Trim(stripslashes($_POST['FirstName']));
    $LastName = Trim(stripslashes($_POST['LastName']));
    $Company = Trim(stripslashes($_POST['Company']));
    $Street1 = Trim(stripslashes($_POST['Street1']));
    $Street2 = Trim(stripslashes($_POST['Street2']));
    $City = Trim(stripslashes($_POST['City']));
    $State = Trim(stripslashes($_POST['State']));
    $PostCode = Trim(stripslashes($_POST['PostCode']));
    $Tel = Trim(stripslashes($_POST['Tel']));
    $email = Trim(stripslashes($_POST['email']));
    $update = Trim(stripslashes($_POST['update']));

    if (empty($FirstName) || empty($LastName) || empty($Company) ||
        empty($Street1) || empty($City) || empty($State) ||
        empty($PostCode) || empty($Tel) || empty($email))
    {
        $error = "All fields are required, please try again.";
    }
    else
    {
        // prepare email body text
        $Body  = "FirstName: $FirstName\n";
        $Body .= "LastName: $LastName\n";
        $Body .= "Company: $Company\n";
        $Body .= "Street1: $Street1\n";
        $Body .= "Street2: $Street2\n";
        $Body .= "City: $City\n";
        $Body .= "State: $State\n";
        $Body .= "PostCode: $PostCode\n";
        $Body .= "Tel: $Tel\n";
        $Body .= "email: $email\n";
        $Body .= "update: $update\n";

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

        // redirect to success page
        if ($success){
            header("Location: thankyou.html");
        }
        else{
            header("Location: header.php");
        }
    }
}

//If data is not posted or if there was
//an error is submisstion, display the form
include("form.php");

?>

 

form.php

<?php
$stateAry = array(
    'AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA' => 'California',
    'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware', 'DC' => 'District of Columbia',
    'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois',
    'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana',
    'ME' => 'Maine', 'MD' => 'Maryland', 'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota',
    'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada',
    'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York',
    'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon',
    'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota',
    'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont', 'VA' => 'Virginia',
    'WA' => 'Washington', 'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming',
);

$stateOptions = '';
foreach($stateAry as $stateAbbrv => $stateName)
{
    $selected = ($stateAbbrv==$_POST['State']) ? ' selected="selected"' : '' ;
    $stateOptions .= "      <option value=\"{$stateAbbrv}\"{$selected}>{$stateName}</option>";
}

?>
<html>
<body>
<div style="color:red;"><?php echo $error; ?></div>
<form id="InformationRequest" name="InformationRequest" method="post" action="<?php echo $_SERVER[php_SELF]; ?> " onSubmit="return checkform(this)">
                <table width="340" border="0" cellspacing="0" cellpadding="2" align="center">
                <tr>
                  <td class="info">First Name*:</td>
                  <td class="infoInput"><input type="text" name="FirstName" id="FirstName" value="<?php echo $_POST['FirstName']; ?>" tabindex="0" /></td>
                </tr>
                <tr>
                  <td class="info">Last Name*:</td>
                  <td class="infoInput"><input type="text" name="LastName" id="LastName" value="<?php echo $_POST['LastName']; ?>" tabindex="1" /></td>
                </tr>
                <tr>
                  <td class="info">Company*:</td>
                  <td class="infoInput"><input type="text" name="Company" id="Company" value="<?php echo $_POST['Company']; ?>" tabindex="2" /></td>
                </tr>
                <tr>
                  <td class="info">Address*:</td>
                  <td class="infoInput"><input type="text" name="Street1" id="Street1" value="<?php echo $_POST['Street1']; ?>" tabindex="3" /></td>
                </tr>
                <tr>
                  <td class="info">Address 2:</td>
                  <td class="infoInput"><input type="text" name="Street2" id="Street2" value="<?php echo $_POST['Street2']; ?>" tabindex="4" /></td>
                </tr>
                <tr>
                  <td class="info">City*:</td>
                  <td class="infoInput"><input type="text" name="City" id="City" value="<?php echo $_POST['City']; ?>" tabindex="5" /></td>
                </tr>
                <tr>
                  <td class="info">State*:</td>
                  <td class="infoInput"><select name="State" id="State">
                <option value="">Pick your state</option>
                <?php echo $stateOptions; ?>
                </select>
                </td>
                </tr>
                <tr>
                  <td class="info">Zip*:</td>
                  <td class="infoInput"><input type="text" name="PostCode" id="PostCode" value="<?php echo $_POST['PostCode']; ?>" tabindex="6" /></td>
                </tr>
                <tr>
                  <td class="info">Email*:</td>
                  <td class="infoInput"><input type="text" name="email" id="email" value="<?php echo $_POST['email']; ?>" tabindex="7" /></td>
                </tr>
                <tr>
                  <td class="info">Phone*:</td>
                  <td class="infoInput"><input type="text" name="Tel" id="Tel" value="<?php echo $_POST['Tel']; ?>" tabindex="8" /></td>
                </tr>
                <tr>
                  <td class="info"><input type="reset" name="reset" id="reset" value="Reset" /></td>
                  <td class="infoInput"><input type="submit" name="submit" id="submit" value="Submit" /></td>
                </tr>
              </table>
            </form>
</body>
</html>

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.