The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Basic PHP FormMail help

LiquidatorLiquidator Registered User regular
edited September 2010 in Help / Advice Forum
Hey guys,

I don't have a hell of a lot of experience with web design, and even less with PHP, but recently I've gotten a new position working from home and I'm now in charge of managing a website that has already been built. I've gotten the ownership of the domain transferred over to myself as well as getting a new hosting account through GoDaddy (whether or not that's a good host is neither here nor there). The website is up and running, but I'm having a problem with our existing contact form on the site. It's basic contact form/FormMail where potential clients enter their contact information and when they click the submit button, it e-mails the contents of the form to one of our e-mail addresses.

When I first got the existing site code uploaded to GoDaddy and I tested out our existing form on the live website, I got some PHP errors when I clicked on the submit button to submit the form. I got that resolved by changing the form action to point at the formmail script that GoDaddy provides, gdform.php, and that got the form working and I'm receiving the e-mails when visitors to the site.

Now, getting to my question... before I took over the website and transferred it over to a new host, whenever a user pushed the submit button the form to send their information, the form would disappear and it would echo back a message saying that an e-mail has been sent to us and we will contact you shortly. However, once I made the change to the form action to get the form working again, when the user submits their information, it e-mails us, but when they click the button, the form is simply cleared and it doesn't echo back the message that their information has been successfully submitted. I'm just trying to figure out how to get it to do that again.

In the code section here, behind the spoiler tag, you can see what we've got as the code for our form I would REALLY appreciate any insight to how I can get this part of the code working again. I'm a complete and total newbie when it comes to PHP, so this is a trial by fire for me.
<? if ($_POST['Submit']!= "Submit")
            {?>
    			
                <form action="/gdform.php" method="POST">
                
                <div class="row">
						<h2>Check one or more of the following services which best describes your family law matter:</h2>
						</div>	<br />						
						<div class="div_checkbox">
							<div class="wrapper">
							<ol
							><li><input type="checkbox" name="SpecificService" value="custodyvisitation" /> Custody/Visitation</li
                            ><li><input type="checkbox" name="SpecificService" value="childsupport" /> Child Support</li
                            ><li><input type="checkbox" name="SpecificService" value="paternity" /> Paternity</li
                            ><li><input type="checkbox" name="SpecificService" value="divorce" /> Divorce</li
                            ><li><input type="checkbox" name="SpecificService" value="other" /> Other</li
                            ><!-- <li><input type="checkbox" name="SpecificService" value="divorce" /> Divorce</li 
							> --> </ol> 
                            <!-- <br /> -->
							</div>
						</div>
                       <br /><hr align="left" width="45%"><p><br />
                <table width="440" cellpadding="0" cellspacing="0" border="0" class="form-table">
                
                <tr><td width="150"><img style="display:none;" id="firstnamealert" src="images/alert.gif" alt="Alert" align="left">Name</td><td><input name="firstname" type="text" class="form-text" size="25"></td></tr>
                <!--<tr><td width="150"><img style="display:none;" id="lastnamealert" src="images/alert.gif" alt="Alert" align="left">Last name</td><td><input name="lastname" type="text" class="form-text" size="30"></td></tr> -->
              <!--  <tr><td width="150"><img style="display:none;" id="cityalert" src="images/alert.gif" alt="Alert" align="left">City</td><td><input name="city" type="text" class="form-text" size="30"></td></tr> -->
                <tr><td width="150"><img style="display:none;" id="statealert" src="images/alert.gif" alt="Alert" align="left">County in California<input name="state" type="hidden" size="30"></td>
				<td><select id="state" name="state">
                  <option value="">- Select One -</option> 
                    <option value="Alameda">Alameda County</option>	
                    <option value="Alpine">Alpine County</option>	
                    <option value="Amador">Amador County</option>	
                    <option value="Butte">Butte County</option>
                    <option value="Calaveras">Calaveras County</option>	
                    <option value="Colusa">Colusa County</option>	
                    <option value="ContraCosta">Contra Costa County</option>	
                    <option value="DelNorte">Del Norte County</option>
                    <option value="ElDorado">El Dorado County</option>	
                    <option value="Fresno">Fresno County</option> 	
                    <option value="Glenn">Glenn County</option>	
                    <option value="Humboldt">Humboldt County</option>
                    <option value="Imperial">Imperial County</option> 	
                    <option value="Inyo">Inyo County</option> 	
                    <option value="Kern">Kern County</option>
                    <option value="Kings">Kings County</option>
                    <option value="Lake">Lake County</option> 	
                    <option value="Lassen">Lassen County</option> 	
                    <option value="Los Angeles">Los Angeles County</option>	
                    <option value="Madera">Madera County</option>
                    <option value="Marin">Marin County</option>	
                    <option value="Mariposa">Mariposa County</option> 	
                    <option value="Mendocino">Mendocino County</option> 	
                    <option value="Merced">Merced County</option>
                    <option value="Modoc">Modoc County</option> 	
                    <option value="Mono">Mono County</option> 	
                    <option value="Monterey">Monterey County</option>	
                    <option value="Napa">Napa County</option>
                    <option value="Nevada">Nevada County</option>	
                    <option value="Orange">Orange County</option>	
                    <option value="Placer">Placer County</option> 	
                    <option value="Plumas">Plumas County</option>
                    <option value="Riverside">Riverside County</option>	
                    <option value="Sacramento">Sacramento County</option>
                    <option value="SanBenito">San Benito County</option>
                    <option value="SanBernardino">San Bernardino County</option>
                    <option value="SanDiego">San Diego County</option>
                    <option value="SanFrancisco">San Francisco County</option>	
                    <option value="SanJoaquin">San Joaquin County</option>
                    <option value="SanLuisObispo">San Luis Obispo County</option>
                    <option value="SanMateo">San Mateo County</option>
                    <option value="SantaBarbara">Santa Barbara County</option>	
                    <option value="SantaClara">Santa Clara County</option>	
                    <option value="SantaCruz">Santa Cruz County</option>
                    <option value="Shasta">Shasta County</option>	
                    <option value="Sierra">Sierra County</option>	
                    <option value="Siskiyou">Siskiyou County</option> 
                    <option value="Solano">Solano County</option>
                    <option value="Sonoma">Sonoma County</option>
                    <option value="Stanislaus">Stanislaus County</option>
                    <option value="Sutter">Sutter County</option>
                    <option value="Tehama">Tehama County</option>
                    <option value="Trinity">Trinity County</option>
                    <option value="Tulare">Tulare County</option>
                    <option value="Tuolumne">Tuolumne County</option>
                    <option value="Ventura">Ventura County</option>
                    <option value="Yolo">Yolo County</option>
                    <option value="Yuba">Yuba County</option>
                  </select></td>
				</tr>
                <tr><td><img style="display:none;" id="emailalert" src="images/alert.gif" alt="Alert" align="left">Email</td><td><input name="email" type="text" class="form-text" size="25"></td></tr>
                <tr><td>Phone</td><td><input name="phone" type="text" class="form-text" size="25"></td></tr>
                <tr><td>Alternate Phone</td><td><input name="altphone" type="text" class="form-text" size="25"></td></tr>
				<tr><td width="150"><img style="display:none;" id="besttimealert" src="images/alert.gif" alt="Alert" align="left">Best Time to Contact<input name="besttime" type="hidden" size="30"></td>
                 <td><select id="besttime" name="besttime">
                  <option value="">- Select One -</option> 
                    <option value="Mornings">Mornings</option>	
                    <option value="Afternoons">Afternoons</option>	
                    <option value="Evenings">Evenings</option>	
                    <option value="Anytime">Anytime</option>
                  </select></td>
                  <!-- <tr><td>Number of Children Involved</td><td><input name="children" type="text" class="form-text" size="4"></td></tr> -->
<!-- <tr><td>Number of Children Involved&nbsp;&nbsp;</td><td><input name="children" type="text" class="form-text" size="5"></td></tr> -->
                
                <tr><td></td><td><input type="Submit" name="Submit" class="form-btn" value="Submit"></td></tr>
                
            </table>
                
                </form>
         
            <?
            } else {
            
            $request_method = $_SERVER["REQUEST_METHOD"];
            if ($request_method == "GET") {$query_vars = $_GET;} 
            elseif ($request_method == "POST") {$query_vars = $_POST;}
            
            reset($query_vars);
            
            $Datetime = date("YmdHis", time() + $timeoffset);
            
            //$file = $_SERVER['DOCUMENT_ROOT'] . "/../data/gdform_" . $t;
            $file = $_SERVER['DOCUMENT_ROOT'] . "/contactform.txt";
            if (!$fp = fopen($file, 'a')) {echo "Cannot open file error code 223"; exit;}
            
            fwrite($fp,"-----------------------------------\n");
            
            while (list ($key, $val) = each ($query_vars)) {
                if ($key == "Subject") {fwrite($fp,"<FORM NAME=$val START  ".$Datetime.">\n"); $formname=$val;}
                elseif ($key == "Redirect") {$landing_page = $val;}
                else {fwrite($fp,"$key				$val\n"); $Emailbody.=$key."				".$val."
                ";}
            }
            fclose($fp);
            //echo $landing_page;
            
            
            $formname='Home - Evaluation';
          	$frommail = "XXXXXXXXXXXXXXXXXXXXXXXXX";  //email address of recipient.
            $fromname = "XXXXXXXXXXXXXXXXXXXXXXXXXX: ".$formname;  //name of sender of email.
            $Subject= $fromname;
            $replymail = $frommail;  //address for reply mail.
            $replyname = "name";  //name for reply mail.
            
            $replyheader = "MIME-Version: 1.0\r \n";
            //$replyheaders .= "Content-type: text/plain; charset=iso-8859-1\r \n";
            $replyheaders .= "From: \"$fromname\" <$frommail>\r \n";
            $replyheader .= "To: $orderer_First_Name <$orderer_email>\r \n";
            $replyheaders .= "Reply-To: \"$replyname\" <$replymail>\r \n";
            $replyheaders .= "Return-Path: $replymail\r \n";
            /* additional headers 
            #$replyheaders .= "X-Priority: 3\r \n";
            #$replyheaders .= "X-MSMail-Priority: High\r \n";
            $headers .= "X-Mailer: Just My Server";
            $headers .= "Cc: birthdayarchive@example.com\r\n";
            $headers .= "Bcc: birthdaycheck@example.com\r\n";
            */
            
            mail( "$frommail", "$Subject",
            "$Emailbody", "$replyheaders");
            
            echo '<p>Thank you for your inquiry.<br>Your evaluation has been received. We will contact you shortly.</p>';
            }
            ?>
            
            </div>

Oh, and one more question, but it's not entirely that important to get it working right yet. We have a text file on our hosting account called contactform.txt, and whenever someone USED to submit their information, it would be appended to the end of the text file. The code is up there, but it doesn't seem to do anything anymore since I switched the code to a new hosting account. Any ideas about that?

I appreciate any help you guys can give. Thank you soooo much!

Liquidator on

Posts

  • DisrupterDisrupter Registered User regular
    edited September 2010
    is the script we are looking at gdform.php? I would assume not. What does gdform.php to? Probably just redirect you back from where you came after sending the email?

    Disrupter on
    616610-1.png
  • LiquidatorLiquidator Registered User regular
    edited September 2010
    Disrupter wrote: »
    is the script we are looking at gdform.php? I would assume not. What does gdform.php to? Probably just redirect you back from where you came after sending the email?

    Sorry, the script you're looking at is within our index.php main page. gdform.php is the formmail script that GoDaddy provides. Here's what it looks like.
    <?php
    	$request_method = $_SERVER["REQUEST_METHOD"];
    	if($request_method == "GET")
    	{
    		$query_vars = $_GET;
    	} 
    	elseif ($request_method == "POST")
    	{
    		$query_vars = $_POST;
    	}
    
    	reset($query_vars);
    	$t = date("U");
    	$file = $_SERVER['DOCUMENT_ROOT'] . "\ssfm\gdform_" . $t;
    	$fp = fopen($file,"w");
    
    	while (list ($key, $val) = each ($query_vars)) 
    	{
    		fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\r\n"); 
    		fputs($fp,"$val\r\n");
    		fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\r\n");
    		if ($key == "redirect") 
    		{ 
    			$landing_page = $val;
    		}
    	}
    
    	fclose($fp);
    	
    	if ($landing_page != "")
    	{
    		header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");
    	} 
    	else 
    	{
    		header("Location: http://".$_SERVER["HTTP_HOST"]."/");
    	}
    ?>
    

    Liquidator on
  • DisrupterDisrupter Registered User regular
    edited September 2010
    Ok well, the simple answer to why whats happening is happening:

    Your form currently submits to gdform.php which writes your post variables to a text file, then returns you from whence you came.

    When it returns you, the submit button is no longer posted, so you are getting the empty form rather then the submit message.

    Now, I dont know how godaddy works, but I ASSUME they parse this text file to determine who to send emails to. If you notice, the code in your old form was very similar to gdform.php.
    $request_method = $_SERVER["REQUEST_METHOD"];
    if ($request_method == "GET") {$query_vars = $_GET;}
    elseif ($request_method == "POST") {$query_vars = $_POST;}

    reset($query_vars);

    $Datetime = date("YmdHis", time() + $timeoffset);

    //$file = $_SERVER . "/../data/gdform_" . $t;
    $file = $_SERVER . "/contactform.txt";
    if (!$fp = fopen($file, 'a')) {echo "Cannot open file error code 223"; exit;}

    fwrite($fp,"
    \n");

    while (list ($key, $val) = each ($query_vars)) {
    if ($key == "Subject") {fwrite($fp,"<FORM NAME=$val START ".$Datetime.">\n"); $formname=$val;}
    elseif ($key == "Redirect") {$landing_page = $val;}
    else {fwrite($fp,"$key $val\n"); $Emailbody.=$key." ".$val."
    ";}
    }

    It actually seemed to use godaddy's method of writing out to a file, but instead of writing out to gdform_" . $t; it wrote it to contactform.txt.

    Quick fix would be just to comment out the header redirects in the gdform.php and instead echo out your submission message. The better fix would be to take godaddy's gdform.php code and put it in your else statement, above the submission method and have your form post back to itself instead of gdform.php

    The code below might work, again, this is under the assumption godaddy writes out to the txt file then parses it to send mail. If it does not, then this code will not work (backup your code first before trying, incase im way off and i ruin your life)
    <? if ($_POST!= "Submit")
    {?>

    <form action="<?php echo($ SERVER);?>" method="POST">

    <div class="row">
    <h2>Check one or more of the following services which best describes your family law matter:</h2>
    </div> <br />
    <div class="div_checkbox">
    <div class="wrapper">
    <ol
    ><li><input type="checkbox" name="SpecificService" value="custodyvisitation" /> Custody/Visitation</li
    ><li><input type="checkbox" name="SpecificService" value="childsupport" /> Child Support</li
    ><li><input type="checkbox" name="SpecificService" value="paternity" /> Paternity</li
    ><li><input type="checkbox" name="SpecificService" value="divorce" /> Divorce</li
    ><li><input type="checkbox" name="SpecificService" value="other" /> Other</li
    ><!-- <li><input type="checkbox" name="SpecificService" value="divorce" /> Divorce</li
    > --> </ol>
    <!-- <br /> -->
    </div>
    </div>
    <br /><hr align="left" width="45%"><p><br />
    <table width="440" cellpadding="0" cellspacing="0" border="0" class="form-table">

    <tr><td width="150"><img style="display:none;" id="firstnamealert" src="images/alert.gif" alt="Alert" align="left">Name</td><td><input name="firstname" type="text" class="form-text" size="25"></td></tr>
    <!--<tr><td width="150"><img style="display:none;" id="lastnamealert" src="images/alert.gif" alt="Alert" align="left">Last name</td><td><input name="lastname" type="text" class="form-text" size="30"></td></tr> -->
    <!-- <tr><td width="150"><img style="display:none;" id="cityalert" src="images/alert.gif" alt="Alert" align="left">City</td><td><input name="city" type="text" class="form-text" size="30"></td></tr> -->
    <tr><td width="150"><img style="display:none;" id="statealert" src="images/alert.gif" alt="Alert" align="left">County in California<input name="state" type="hidden" size="30"></td>
    <td><select id="state" name="state">
    <option value="">- Select One -</option>
    <option value="Alameda">Alameda County</option>
    <option value="Alpine">Alpine County</option>
    <option value="Amador">Amador County</option>
    <option value="Butte">Butte County</option>
    <option value="Calaveras">Calaveras County</option>
    <option value="Colusa">Colusa County</option>
    <option value="ContraCosta">Contra Costa County</option>
    <option value="DelNorte">Del Norte County</option>
    <option value="ElDorado">El Dorado County</option>
    <option value="Fresno">Fresno County</option>
    <option value="Glenn">Glenn County</option>
    <option value="Humboldt">Humboldt County</option>
    <option value="Imperial">Imperial County</option>
    <option value="Inyo">Inyo County</option>
    <option value="Kern">Kern County</option>
    <option value="Kings">Kings County</option>
    <option value="Lake">Lake County</option>
    <option value="Lassen">Lassen County</option>
    <option value="Los Angeles">Los Angeles County</option>
    <option value="Madera">Madera County</option>
    <option value="Marin">Marin County</option>
    <option value="Mariposa">Mariposa County</option>
    <option value="Mendocino">Mendocino County</option>
    <option value="Merced">Merced County</option>
    <option value="Modoc">Modoc County</option>
    <option value="Mono">Mono County</option>
    <option value="Monterey">Monterey County</option>
    <option value="Napa">Napa County</option>
    <option value="Nevada">Nevada County</option>
    <option value="Orange">Orange County</option>
    <option value="Placer">Placer County</option>
    <option value="Plumas">Plumas County</option>
    <option value="Riverside">Riverside County</option>
    <option value="Sacramento">Sacramento County</option>
    <option value="SanBenito">San Benito County</option>
    <option value="SanBernardino">San Bernardino County</option>
    <option value="SanDiego">San Diego County</option>
    <option value="SanFrancisco">San Francisco County</option>
    <option value="SanJoaquin">San Joaquin County</option>
    <option value="SanLuisObispo">San Luis Obispo County</option>
    <option value="SanMateo">San Mateo County</option>
    <option value="SantaBarbara">Santa Barbara County</option>
    <option value="SantaClara">Santa Clara County</option>
    <option value="SantaCruz">Santa Cruz County</option>
    <option value="Shasta">Shasta County</option>
    <option value="Sierra">Sierra County</option>
    <option value="Siskiyou">Siskiyou County</option>
    <option value="Solano">Solano County</option>
    <option value="Sonoma">Sonoma County</option>
    <option value="Stanislaus">Stanislaus County</option>
    <option value="Sutter">Sutter County</option>
    <option value="Tehama">Tehama County</option>
    <option value="Trinity">Trinity County</option>
    <option value="Tulare">Tulare County</option>
    <option value="Tuolumne">Tuolumne County</option>
    <option value="Ventura">Ventura County</option>
    <option value="Yolo">Yolo County</option>
    <option value="Yuba">Yuba County</option>
    </select></td>
    </tr>
    <tr><td><img style="display:none;" id="emailalert" src="images/alert.gif" alt="Alert" align="left">Email</td><td><input name="email" type="text" class="form-text" size="25"></td></tr>
    <tr><td>Phone</td><td><input name="phone" type="text" class="form-text" size="25"></td></tr>
    <tr><td>Alternate Phone</td><td><input name="altphone" type="text" class="form-text" size="25"></td></tr>
    <tr><td width="150"><img style="display:none;" id="besttimealert" src="images/alert.gif" alt="Alert" align="left">Best Time to Contact<input name="besttime" type="hidden" size="30"></td>
    <td><select id="besttime" name="besttime">
    <option value="">- Select One -</option>
    <option value="Mornings">Mornings</option>
    <option value="Afternoons">Afternoons</option>
    <option value="Evenings">Evenings</option>
    <option value="Anytime">Anytime</option>
    </select></td>
    <!-- <tr><td>Number of Children Involved</td><td><input name="children" type="text" class="form-text" size="4"></td></tr> -->
    <!-- <tr><td>Number of Children Involved  </td><td><input name="children" type="text" class="form-text" size="5"></td></tr> -->

    <tr><td></td><td><input type="Submit" name="Submit" class="form-btn" value="Submit"></td></tr>

    </table>

    </form>

    <?
    } else {


    $request_method = $_SERVER["REQUEST_METHOD"];
    if($request_method == "GET")
    {
    $query_vars = $_GET;
    }
    elseif ($request_method == "POST")
    {
    $query_vars = $_POST;
    }

    reset($query_vars);
    $t = date("U");
    $file = $_SERVER . "\ssfm\gdform_" . $t;
    $file2 = $_SERVER . "/contactform.txt";
    $fp = fopen($file,"w");
    $fp2 = fopen($file2,"w");
    while (list ($key, $val) = each ($query_vars))
    {
    fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\r\n");
    fputs($fp,"$val\r\n");
    fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\r\n");
    fputs($fp2,"<GDFORM_VARIABLE NAME=$key START>\r\n");
    fputs($fp2,"$val\r\n");
    fputs($fp2,"<GDFORM_VARIABLE NAME=$key END>\r\n");
    }

    fclose($fp);
    fclose($fp2);

    echo '<p>Thank you for your inquiry.<br>Your evaluation has been received. We will contact you shortly.</p>';
    }
    ?>

    </div>

    Disrupter on
    616610-1.png
Sign In or Register to comment.