Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    code works, needs to be reordered?

    I have this code that writes form data into a mysql database and used the same form data as the message of an email to let someone know an inquiry has been made. The problems is..... it is sending a blank email (with headers) every time someone opens the page.

    How do I get it to only send the email once the submit button has been selected?

    PHP Code:
    <?

    $email 
    '**********@***';
    $subject 'Inquiry from web';
    $message "First name: {$_POST['firstname']}
    Last name: {$_POST['lastname']}
    Email: {$_POST['emailAddress']}
    Phone: {$_POST['phoneNumber']}
    Address: {$_POST['stAddress']}
    {$_POST['aptNumber']}
    {$_POST['city']}
    {$_POST['fiftyStates']}
    {$_POST['zipCode']},
    At: {$_POST['time']}"
    ;
    $headers "MIME-Version: 1.0\r\n";
    $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers.= "X-Nyar-Nyar: Test header to test with\r\n";
    if (!
    preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"$email)) { echo "Invalid email address!";
    }
    elseif (
    $subject == "") {
    echo 
    "No subject!";
    }
    elseif (
    mail($email,$subject,$message,$headers)) {
    echo 
    "";
    }
    else {
    echo 
    "Hold on: email to $email has failed.";
    }

    ?>

    <?
    if (!function_exists("GetSQLValueString")) {
    function 
    GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      if (
    PHP_VERSION 6) {
        
    $theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }

      
    $theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch (
    $theType) {
        case 
    "text":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;    
        case 
    "long":
        case 
    "int":
          
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case 
    "double":
          
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case 
    "date":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;
        case 
    "defined":
          
    $theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
          break;
      }
      return 
    $theValue;
    }
    }

    $editFormAction $_SERVER['PHP_SELF'];
    if (isset(
    $_SERVER['QUERY_STRING'])) {
      
    $editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset(
    $_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      
    $insertSQL sprintf("INSERT INTO contact (firstname, lastname, emailAddress, phoneNumber, stAddress, aptNumber, city, fiftystates, zipCode, `time`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                           
    GetSQLValueString($_POST['firstname'], "text"),
                           
    GetSQLValueString($_POST['lastname'], "text"),
                           
    GetSQLValueString($_POST['emailAddress'], "text"),
                           
    GetSQLValueString($_POST['phoneNumber'], "text"),
                           
    GetSQLValueString($_POST['stAddress'], "text"),
                           
    GetSQLValueString($_POST['aptNumber'], "text"),
                           
    GetSQLValueString($_POST['city'], "text"),
                           
    GetSQLValueString($_POST['fiftyStates'], "text"),
                           
    GetSQLValueString($_POST['zipCode'], "text"),
                           
    GetSQLValueString($_POST['time'], "text"));

      
    mysql_select_db($database_************, $****************);
      
    $Result1 mysql_query($insertSQL, $**************) or die(mysql_error());
    }
    ?>
    Does the email text need to be in an if statement below the mysql part?

    For the curious- here is the form part... there is another bit of php at the end that works to echo the name back and say thanks.

    Code:
    <div class="contentContact">
     <div id="formResponse"> 
    
    </div> 
    <form method="POST" id="form1" name="form1" action="<?php echo $editFormAction; ?>"post">
    <table width="90%" border="0" cellpadding="1%">
      <tr>
        <td>First Name*</td>
        <td><span id="sprytextfirstname">
          <input type="text" name="firstname" id="firstname" />
          <span class="textfieldRequiredMsg">(Please, let me know who I am contacting)</span></span></td>
      </tr>
      <tr>
        <td>Last Name</td>
        <td><span id="sprylastname">
          <input type="text" name="lastname" id="lastname" />
    </span></td>
      </tr>
      <tr>
        <td>Email Address*</td>
        <td><span id="spryemail"><span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>      
          <input type="text" name="emailAddress" id="emailAddress" />
         </td>
      </tr>
      <tr>
        <td>Phone Number*</td>
        <td><span id="spryphonenumber">
        <input type="text" name="phoneNumber" id="phoneNumber" />
        <span class="textfieldInvalidFormatMsg">Example: (000) 000-0000</span></span></td>
      </tr>
      <tr>
        <td>Street Address</td>
        <td><span id="sprystaddress">
          <input type="text" name="stAddress" id="stAddress" />
    </span></td>
      </tr>
      <tr>
        <td>Suite | Apt | Unit</td>
        <td><span id="spryaptNumber">
          <input type="text" name="aptNumber" id="aptNumber" />
    </span></td>
      </tr>
      <tr>
        <td>City</td>
        <td><span id="sprycity">
          <input type="text" name="city" id="city" />
    </span></td>
      </tr>
      <tr>
        <td>State</td>
         <td><select name="fiftyStates" size="1">
      <option value="">Select a State</option>
      <option value="AL" selected="selected">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="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>
      </tr>
      <tr>
        <td>Zip Code*</td>
        <td><span id="spryzipcode">
          <input type="text" name="zipCode" id="zipCode" />
          <span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
      </tr>
      <tr>
        <td>When is the best time to reach you?</td>
        <td><span id="sprytime">
          <input name="time" id="time" type="text" />
    </span></td>
      </tr>
      
      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submit" id="submit" value="Submit" /></td>
      </tr>
    </table>
    <input type="hidden" name="MM_insert" value="form1" />
    </form>
      <!-- end .content -->
    <?php 
    if (isset($_POST['firstname']))
    {
    	echo "Thank you, ";
    	echo $_POST['firstname'] . " " .$_POST['lastname'] ;
    	echo ", I will get in touch with you as soon as possible!";
    	echo "<br/>";
    echo "<img src=../thanksimage.png />";
    
    
    	}
    
    
    ?></div>

  • #2
    New to the CF scene
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    nevermind....

    I reordered it and it worked correctly- just a little copy and paste work.

  • #3
    New to the CF scene
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It really was this easy.

    PHP Code:
    <?
    if (!function_exists("GetSQLValueString")) {
    function 
    GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      if (
    PHP_VERSION 6) {
        
    $theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }

      
    $theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch (
    $theType) {
        case 
    "text":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;    
        case 
    "long":
        case 
    "int":
          
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case 
    "double":
          
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case 
    "date":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;
        case 
    "defined":
          
    $theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
          break;
      }
      return 
    $theValue;
    }
    }

    $editFormAction $_SERVER['PHP_SELF'];
    if (isset(
    $_SERVER['QUERY_STRING'])) {
      
    $editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset(
    $_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      
    $insertSQL sprintf("INSERT INTO contact (firstname, lastname, emailAddress, phoneNumber, stAddress, aptNumber, city, fiftystates, zipCode, `time`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                           
    GetSQLValueString($_POST['firstname'], "text"),
                           
    GetSQLValueString($_POST['lastname'], "text"),
                           
    GetSQLValueString($_POST['emailAddress'], "text"),
                           
    GetSQLValueString($_POST['phoneNumber'], "text"),
                           
    GetSQLValueString($_POST['stAddress'], "text"),
                           
    GetSQLValueString($_POST['aptNumber'], "text"),
                           
    GetSQLValueString($_POST['city'], "text"),
                           
    GetSQLValueString($_POST['fiftyStates'], "text"),
                           
    GetSQLValueString($_POST['zipCode'], "text"),
                           
    GetSQLValueString($_POST['time'], "text"));

      
    mysql_select_db($database_************, $****************);
      
    $Result1 mysql_query($insertSQL, $**************) or die(mysql_error());
    $email '**********@***';
    $subject 'Inquiry from web';
    $message "First name: {$_POST['firstname']}
    Last name: {$_POST['lastname']}
    Email: {$_POST['emailAddress']}
    Phone: {$_POST['phoneNumber']}
    Address: {$_POST['stAddress']}
    {$_POST['aptNumber']}
    {$_POST['city']}
    {$_POST['fiftyStates']}
    {$_POST['zipCode']},
    At: {$_POST['time']}"
    ;
    $headers "MIME-Version: 1.0\r\n";
    $headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers.= "X-Nyar-Nyar: Test header to test with\r\n";
    if (!
    preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"$email)) { echo "Invalid email address!";
    }
    elseif (
    $subject == "") {
    echo 
    "No subject!";
    }
    elseif (
    mail($email,$subject,$message,$headers)) {
    echo 
    "";
    }
    else {
    echo 
    "Hold on: email to $email has failed.";
    }

    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •