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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How do you use PHP to connect to a Microsoft SQL Express database?

    Ok, I have a PHP form but when the user submits it doesn't do anything. I have finally got the base code all working. I have PHP validation working just the way I want it to.

    After a bit of learning I came up with an idea. When the user submits the PHP form it could add these particular records into a MS SQL 2008 Express database (it cannot be MySQL as I am already using MS SQL Express for my members area) and then if this was successful it could send an email to the email address the user entered
    --------------------------------
    Contact Page (contactus.php) (all required fields): user fills in all details

    Firstname: TEXTBOX
    Surname: TEXTBOX
    Email: TEXBOX
    Number of employees: DROP DOWN
    Consulting type: DROP DOWN
    Message: TEXT AREA
    Confirmation: CHECKBOX

    Submit: BUTTON (when user clicks it adds this data to a table in a Microsoft SQL 2008 Express Database and sends an email to the address the user entered into the email textbox )
    ------------------------------------------
    In database table (MS SQL 2008 Express)

    ID (primary key) : Randomly generated number (created by PHP)
    Firstname: Data user entered
    Surname: Data user entered
    Email: Data user entered
    Number of employees: Data user entered
    Consulting type: Data user entered
    Message: Data user entered
    Confirmation: checked

    ----------------
    Confirmation Email

    Thanks for contacting us please click this go here to complete contacting us:

    http://www.mysite.com/contactus.aspx?RANDOMNUMBER

    (I think asp.net would be easier in this case because it would be more flexible connecting to the MS SQL 2008 express database.)

    ------------------------------------

    Contact Us (contactus.aspx?ID=RANDOMNUMBER)

    Gets the data from the ID RANDOMNUMBER

    Use DetailView Control or FormView Control (make these read-only so user cannot edit the values) to display current all records relating to that particular RANDNUMBER ID value. Then have a submit button at the end where the user can send sends all the data off to a fixed email address:

    myemail@myemail.com

    If the email was successfully send, asp.net removes RANDOMNUMBER's row in the database browser redirects to static page- successForm.html just saying email successfully sent

    If the email failed to send, asp.net keeps RANDOMNUMBER's row in the database and browser redirects to static page- failureForm.html just saying email failed to send. Please try the confirmation link again later.

    ------------

    To clarify things further, I'll show you what contactus.php looks like so far so you see what I need to do in order to successfully achieve the above:


    Code:
    <?php
     
    if (!isset($_POST['submit'])) {
     
    } else {
    $Firstname = $_POST['Firstname'];
     
    $Surname = $_POST['Surname'];
     
    $Email = $_POST['Email'];
     
    $Company = $_POST['Company'];
     
    $phone = ereg_replace('[^0-9]', '', $_POST['Telephone']);
     
    $employee = ($_POST['employee'] or $osrows);
     
    $consultingtype = ($_POST['consultingtype'] or $consultingrows);
     
    $message = $_POST['Message'];
     
    if (!empty($_POST['Firstname'])) {
    } else {
    $Firstname = FALSE;
    echo "<p><font color='red'>Please enter your first name.</font></p>";
    }
    if (!empty($_POST['Surname'])) {
    } else {
    $Surname = FALSE;
    echo "<p><font color='red'>Please enter your surname.</font></p>";
    }
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)){
     } else { echo "<p><font color='red'>Please enter a valid email address.</font></p>"; 
    }  
     if (!empty($_POST['Company'])) {
    } else {
    $Company = FALSE;
    echo "<p><font color='red'>Please enter your company name.</font></p>";
    }
     
    if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {
    echo "<p><font color='red'>Please enter a valid UK phone number.</font></p>";
    } 
    if ($employee == "1-10" || $employee == "10-20" || $employee == "20-40" || $employee == "40-80" || $employee == "80-160" || $employee == "160-320" || $employee == "320-640" || $employee == "1000+"){
     
    }else {
    echo "<p><font color='red'>Please select the number of employees</font></p>";
    }
     
       if ($consultingtype == "Finance Support" || $consultingtype == "Marketing Support") { 
     
    }else {
    echo "<p><font color='red'>Please select the a consulting option</font></p>";
    }
    if (!empty($_POST['Message'])) {
    } else {
    $message = FALSE;
    echo "<p><font color='red'>Please enter a message.</font></p>";
    }
     
    if(isset($_POST['checkbox'])) {
    } else {
    echo "<p><font color='red'>You must agree to the Terms and Conditions.</font></p>";
    }
    }
    $osrows = array(
     array('idInvalid' => 0, 'label' => 'Please select'),
     array('id' => 1, 'label' => '1-10'),
     array('id' => 2, 'label' => '20-40'),
     array('id' => 3, 'label' => '40-80'),
     array('id' => 4, 'label' => '80-160'),
     array('id' => 5, 'label' => '160-320'),
     array('id' => 6, 'label' => '320-640'),
     array('id' => 7, 'label' => '1000+'),
    );
    $consultingrows = array(
     array('id2Invalid' => 0, 'label2' => 'Please select'),
     array('id2' => 1, 'label2' => 'Finance Support'),
     array('id2' => 2, 'label2' => 'Marketing Support'),
     
    ); 
    ?>
    </p>
      </div>
      <table width="508" border="0">
      <tr>
        <td><p>First name</p></td>
        <td><label>
        <input type="text" name="Firstname" id="Firstname" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Firstname=$_POST['Firstname']; echo "$Firstname"; }?>" >
       </label> </td> 
      </tr>
      <tr>
        <td width="164"><p>Surname</p></td>
        <td width="334"><label>
        <input type="text" name="Surname" id="Surname" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Surname=$_POST['Surname']; echo "$Surname"; }?>" >
        </label></td>
      </tr>
      <tr>
        <td><p>Email Address</p></td>
        <td><label>
          <input type="text" name="Email" id="Email" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Email=$_POST['Email']; echo "$Email"; }?>">
        </label></td>
      </tr>
      <tr>
        <td><p>Company name</p></td>
        <td><label>
          <input type="text" name="Company" id="Company" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Company=$_POST['Company']; echo "$Company"; }?>">
        </label></td>
      </tr>
      <tr>
        <td><p>Telephone Number</p></td>
        <td><input type="text" name="Telephone" id="Telephone" value="<?php if (!isset($_POST['submit'])) {} else { 
        $phone=$_POST['Telephone']; echo "$phone"; }?>"></td>
      </tr>
      <tr>
        <td><p>Number of employees</p></td>
     
        <td><select name="employee" id="employee">
         <?php foreach ($osrows as $osrow): ?>
          <option value="<?php echo $osrow['id'] ?>"
     
       <?php echo ($osrow['id'] == $_POST['employee'] ? "selected='selected'" : ""); ?> 
      ><?php echo $osrow['label'] ?></option>
          <?php endforeach; ?>
        </select>
          </td>
      </tr>
      <tr>
        <td><p>Consulting type</p></td>
        <td>
          <select name="consultingtype" id="consultingtype">
            <?php foreach ($consultingrows as $consultingrow): ?>
          <option value="<?php echo $consultingrow['id2'] ?>"
     
       <?php echo ($consultingrow['id2'] == $_POST['consultingtype'] ? "selected='selected'" : ""); ?> 
      ><?php echo $consultingrow['label2'] ?></option>
          <?php endforeach; ?>
        </select></td>
      </tr>
      <tr>
        <td height="133"><p>Message</p></td>
        <td valign="top">
          <textarea name="Message" cols="45" rows="10" id="Message"><?php $message=$_POST['Message']; echo "$message"; ?></textarea></td>
      </tr>
      <tr>
        <td valign="top"><p> I agree to LMGR <a href="../Policies/policies.html" target="_blank">Terms and Conditons </a></p>      </td>
        <td valign="top">
        <input name="checkbox" type="checkbox" id="checkbox" value="checked"></td>
      </tr>
    </table>
     
      <input name="submit" type="submit" id="submit" value="Submit" ></form>

    I hope I have given all the info needed. Please bear in mind it must connect to a Microsoft SQL 2008 Express database named ASPNETDB.MDF
    I cannot use MySQL because I have already set up all the user accounts in this particular database. I have heard it is possible to do this in PHP despite the fact it is normally used to connect to MySQL.

    Thanks for your time guys

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    So, you haven't used Google and you haven't attempted any code on your own yet. You just want someone here to do it for you?
    Are you a Help Vampire?

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Absolutely not, I am simply asking for help amongst others as this is what you normally do on web forums and yes I did use Google and yes I have made the entire contactus.php myself. I have made a mail.php myself as well which is below:

    PHP Code:
    <title>Sending Email...</title><?php
    echo "<HTML>Please wait while loading...
    This may take a moment on Internet Explorer browsers or other older browsers. 
    <head>"
    ;
          
    $to 'myemail@myemail.com';
    if (
    $_SERVER['REQUEST_METHOD'] != "POST"){exit;}
    while(list(
    $key,$value) = each($_POST)){if(!(empty($value))){$set=1;}$message $message "$key: $value\n\n";} if($set!==1){header("location: $_SERVER[HTTP_REFERER]");exit;}
    $message stripslashes($message);
    $subject "Website Enquiry";
    $headers "From: "$_POST['email'] . "\n";
    $success mail($to,$subject,$message,$headers);
                if (
    $success){echo '<meta http-equiv="refresh" content="1;URL=successForm.html">';}
                else{echo 
    '<meta http-equiv="refresh" content="1;URL=failureForm.html">';}
    echo 
    '</head><body></body></html>';
    ?>
    If anybody else has any suggestions please feel free to add,

    Thanks

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You haven't written any db connection code. This was the first result for me from Google (php mssql db connection). The second result takes me to php.net where I can then see all of the mssql functions.

    Changing the search query used on Google brought me back to the mssql page on php.net for the first result and this verbose instruction set for the second.
    Are you a Help Vampire?

  • #5
    New Coder
    Join Date
    Mar 2010
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This is my page. It is set up so if the user does not add certain required criteria into the fields it brings up an error (validation). However when everything is valid it does nothing. Where about in my code should I include the connection to the MS SQL database? Or if you like the code which runs when the validation is correct and ok and I cannot Google this issue; I am a "New Coder."

    Code:
    <?php
     
    if (!isset($_POST['submit'])) {
     //I tried putting the code in here but it displays when the page displays //
    } else {
    $Firstname = $_POST['Firstname'];
     
    $Surname = $_POST['Surname'];
     
    $Email = $_POST['Email'];
     
    $Company = $_POST['Company'];
     
    $phone = ereg_replace('[^0-9]', '', $_POST['Telephone']);
     
    $employee = ($_POST['employee'] or $osrows);
     
    $consultingtype = ($_POST['consultingtype'] or $consultingrows);
     
    $message = $_POST['Message'];
     
    if (!empty($_POST['Firstname'])) {
    } else {
    $Firstname = FALSE;
    echo "<p><font color='red'>Please enter your first name.</font></p>";
    }
    if (!empty($_POST['Surname'])) {
    } else {
    $Surname = FALSE;
    echo "<p><font color='red'>Please enter your surname.</font></p>";
    }
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)){
     } else { echo "<p><font color='red'>Please enter a valid email address.</font></p>"; 
    }  
     if (!empty($_POST['Company'])) {
    } else {
    $Company = FALSE;
    echo "<p><font color='red'>Please enter your company name.</font></p>";
    }
     
    if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {
    echo "<p><font color='red'>Please enter a valid UK phone number.</font></p>";
    } 
    if ($employee == "1-10" || $employee == "10-20" || $employee == "20-40" || $employee == "40-80" || $employee == "80-160" || $employee == "160-320" || $employee == "320-640" || $employee == "1000+"){
     
    }else {
    echo "<p><font color='red'>Please select the number of employees</font></p>";
    }
     
       if ($consultingtype == "Finance Support" || $consultingtype == "Marketing Support") { 
     
    }else {
    echo "<p><font color='red'>Please select the a consulting option</font></p>";
    }
    if (!empty($_POST['Message'])) {
    } else {
    $message = FALSE;
    echo "<p><font color='red'>Please enter a message.</font></p>";
    }
     
    if(isset($_POST['checkbox'])) {
    } else {
    echo "<p><font color='red'>You must agree to the Terms and Conditions.</font></p>";
    }
    }
    $osrows = array(
     array('idInvalid' => 0, 'label' => 'Please select'),
     array('id' => 1, 'label' => '1-10'),
     array('id' => 2, 'label' => '20-40'),
     array('id' => 3, 'label' => '40-80'),
     array('id' => 4, 'label' => '80-160'),
     array('id' => 5, 'label' => '160-320'),
     array('id' => 6, 'label' => '320-640'),
     array('id' => 7, 'label' => '1000+'),
    );
    $consultingrows = array(
     array('id2Invalid' => 0, 'label2' => 'Please select'),
     array('id2' => 1, 'label2' => 'Finance Support'),
     array('id2' => 2, 'label2' => 'Marketing Support'),
     
    ); 
    ?>
    </p>
      </div>
      <table width="508" border="0">
      <tr>
        <td><p>First name</p></td>
        <td><label>
        <input type="text" name="Firstname" id="Firstname" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Firstname=$_POST['Firstname']; echo "$Firstname"; }?>" >
       </label> </td> 
      </tr>
      <tr>
        <td width="164"><p>Surname</p></td>
        <td width="334"><label>
        <input type="text" name="Surname" id="Surname" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Surname=$_POST['Surname']; echo "$Surname"; }?>" >
        </label></td>
      </tr>
      <tr>
        <td><p>Email Address</p></td>
        <td><label>
          <input type="text" name="Email" id="Email" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Email=$_POST['Email']; echo "$Email"; }?>">
        </label></td>
      </tr>
      <tr>
        <td><p>Company name</p></td>
        <td><label>
          <input type="text" name="Company" id="Company" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Company=$_POST['Company']; echo "$Company"; }?>">
        </label></td>
      </tr>
      <tr>
        <td><p>Telephone Number</p></td>
        <td><input type="text" name="Telephone" id="Telephone" value="<?php if (!isset($_POST['submit'])) {} else { 
        $phone=$_POST['Telephone']; echo "$phone"; }?>"></td>
      </tr>
      <tr>
        <td><p>Number of employees</p></td>
     
        <td><select name="employee" id="employee">
         <?php foreach ($osrows as $osrow): ?>
          <option value="<?php echo $osrow['id'] ?>"
     
       <?php echo ($osrow['id'] == $_POST['employee'] ? "selected='selected'" : ""); ?> 
      ><?php echo $osrow['label'] ?></option>
          <?php endforeach; ?>
        </select>
          </td>
      </tr>
      <tr>
        <td><p>Consulting type</p></td>
        <td>
          <select name="consultingtype" id="consultingtype">
            <?php foreach ($consultingrows as $consultingrow): ?>
          <option value="<?php echo $consultingrow['id2'] ?>"
     
       <?php echo ($consultingrow['id2'] == $_POST['consultingtype'] ? "selected='selected'" : ""); ?> 
      ><?php echo $consultingrow['label2'] ?></option>
          <?php endforeach; ?>
        </select></td>
      </tr>
      <tr>
        <td height="133"><p>Message</p></td>
        <td valign="top">
          <textarea name="Message" cols="45" rows="10" id="Message"><?php $message=$_POST['Message']; echo "$message"; ?></textarea></td>
      </tr>
      <tr>
        <td valign="top"><p> I agree to LMGR <a href="../Policies/policies.html" target="_blank">Terms and Conditons </a></p>      </td>
        <td valign="top">
        <input name="checkbox" type="checkbox" id="checkbox" value="checked"></td>
      </tr>
    </table>
     
      <input name="submit" type="submit" id="submit" value="Submit" ></form>
    Thanks
    Last edited by njccbeach9; 03-23-2010 at 10:59 PM.

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    What have you searched for? Did you read through any of the links provided? Have you read through any tutorials?
    Are you a Help Vampire?

  • #7
    New Coder
    Join Date
    Mar 2010
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have read through a lot of tutorials and links including yours but I don't know where it should go in relation to my code and I know that Google can't do that.

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    I don't think you're paying attention to what you're reading. I'm finding tutorials and FAQs everywhere.

    Using PHP with mssql is almost the same as using with mysql. The connection details will be slightly different and the function names will be different. The actual logic of using php with a database, however, will be the same.
    Are you a Help Vampire?

  • #9
    New Coder
    Join Date
    Mar 2010
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I understand very much what I have read and the resources you have shown me. I am not anymore asking what code I need, I am asking where about the code should go in relation to the code I have given you .

    Come on, I am not asking you to write the code for me. I am just asking where should the code go. I am asking people in a forum, when you ask a teacher a question you don't expect them to come back to you and say research it and look in the book.

    There is one skill knowing the code but it is another skill knowing where it goes in relation to my code, no matter how hard you try and search on Google it will not tell you how to do in relation to my code.

    Please also bear in mind I am a New Coder and so getting code and inserting into a webpage is not second-nature.

    I hope you understand the point I am raising,

    Thank you

  • #10
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    It does tell you how to do it if you pay attention what it's doing instead of looking for something to copy and paste. Almost the same pattern exists throughout all php/database interactions:

    1) connect to the database
    2) run query
    3) handle results

    That's it. That's all there is. Look at whatever tutorials you claim you've read. That's the pattern. The variability comes in with what kind of query you want, which affects whether you need to prepare data, and what kind of results you want, which affects the query itself. Here's yet another example (this one using mysql) that shows that same pattern.
    Are you a Help Vampire?

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by njccbeach9 View Post
    I understand very much what I have read and the resources you have shown me. I am not anymore asking what code I need, I am asking where about the code should go in relation to the code I have given you .
    The basic layout of your form should be something along the lines of:


    {
    Process, sanitise and validate your post vars.
    }
    {
    Do duplicate record checks and commit the results to the database.
    }
    {
    If there were any errors in processing, or the form hasn't been submitted, display the form.
    }

  • #12
    New Coder
    Join Date
    Mar 2010
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry for the late reply, I'll work on it.

    Thanks for your help

  • #13
    New Coder
    Join Date
    Mar 2010
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    OK, I've worked on this page and I have come up with a possible solution which does not work. The problem I have is that I have set the Form action to PHP Self and therefore if all fields are valid the page does not actually do anything despite the fact that all the validation controls work extactly the way I want them to. I have set up a solution so that once the page loads there, a variable named number ($number) is set to zero. However once each field is correctly filled in I have the number variable to increment by one (using ++ in PHP). However the problem is that with the code below if I enter two valid fields, $number increments by one for the first field (giving a value of 1) but the second field does not increment the first field's value (1 to 2) etc all the way up to 9. I have also set it so that if the value is greater than or equal to 9 then succeed and if not don't do anything.

    Here is the code:

    Code:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="form" id="form"><h1 class="divcontent"> By Email </h1>
    <p class="divcontent">Please fill in the following information in order to Contact us about our consulting programmes or any other queries you many have.</p>
    
    <?php $_SERVER['PHP_SELF']; ?>
    <div class="divRightContact"><p>
     
    <?php
     $number=0;
    if (!isset($_POST['submit'])) {
     
    } else {
    $Firstname = $_POST['Firstname'];
     
    $Surname = $_POST['Surname'];
     
    $Email = $_POST['Email'];
     
    $Company = $_POST['Company'];
     
    $phone = ereg_replace('[^0-9]', '', $_POST['Telephone']);
     
    $employee = ($_POST['employee'] or $osrows);
     
    $consultingtype = ($_POST['consultingtype'] or $consultingrows);
     
    $message = $_POST['Message'];
     
    if (!empty($_POST['Firstname'])) {
    $number=$number++;
    } else {
    $Firstname = FALSE;
    echo "<p><font color='red'>Please enter your first name.</font></p>";
    }
    if (!empty($_POST['Surname'])) {
    $number=$number++;
    } else {
    $Surname = FALSE;
    echo "<p><font color='red'>Please enter your surname.</font></p>";
    }
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)){
    $number=$number++;
     } else { echo "<p><font color='red'>Please enter a valid email address.</font></p>"; 
    }  
     if (!empty($_POST['Company'])) {
     $number=$number++;
    } else {
    $Company = FALSE;
    echo "<p><font color='red'>Please enter your company name.</font></p>";
    }
     
    if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {
    $number=$number++;
    echo "<p><font color='red'>Please enter a valid UK phone number.</font></p>";
    } 
    if ($employee == "1-10" || $employee == "10-20" || $employee == "20-40" || $employee == "40-80" || $employee == "80-160" || $employee == "160-320" || $employee == "320-640" || $employee == "1000+"){
    $number=$number++;
    }else {
    echo "<p><font color='red'>Please select the number of employees</font></p>";
    }
     
       if ($consultingtype == "Finance Support" || $consultingtype == "Marketing Support") { 
     $number=$number++;
    }else {
    echo "<p><font color='red'>Please select the a consulting option</font></p>";
    }
    if (!empty($_POST['Message'])) {
    $number=$number++;
    } else {
    $message = FALSE;
    echo "<p><font color='red'>Please enter a message.</font></p>";
    }
     
    if(isset($_POST['checkbox'])) {
    $number=$number++;
    } else {
    echo "<p><font color='red'>You must agree to the Terms and Conditions.</font></p>";
    }
    
    }
    $osrows = array(
     array('idInvalid' => 0, 'label' => 'Please select'),
     array('id' => 1, 'label' => '1-10'),
     array('id' => 2, 'label' => '20-40'),
     array('id' => 3, 'label' => '40-80'),
     array('id' => 4, 'label' => '80-160'),
     array('id' => 5, 'label' => '160-320'),
     array('id' => 6, 'label' => '320-640'),
     array('id' => 7, 'label' => '1000+'),
    );
    $consultingrows = array(
     array('id2Invalid' => 0, 'label2' => 'Please select'),
     array('id2' => 1, 'label2' => 'Finance Support'),
     array('id2' => 2, 'label2' => 'Marketing Support'),
     
    ); 
    
    if ($number >=9) {
    echo "<HTML><p>Please wait while we send your email...</p>
     
    <head>";
          $to = 'myemail@email.com';
    
    if ($_SERVER['REQUEST_METHOD'] != "POST"){exit;}
    while(list($key,$value) = each($_POST)){if(!(empty($value))){$set=1;}$message = $message . "$key: $value\n\n";} if($set!==1){header("location: $_SERVER[HTTP_REFERER]");exit;}
    $SpamErrorMessage ="No Websites URLs permitted";
    if (preg_match("/http/i","$Firstname")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$Surname")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$Email")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$Company")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$Telephone")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$employee")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$consultingtype")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$message")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$checkbox")) {echo "$SpamErrorMessage"; exit ();}
    $message = stripslashes($message);
    $subject = "Website Enquiry";
    $headers = "From: LMGR Customer". $_POST['email'] . "\n";
    $success = mail($to,$subject,$message,$headers); 
                if ($success){echo '<meta http-equiv="refresh" content="1;URL=successForm.html">';}
                else{echo '<meta http-equiv="refresh" content="1;URL=failureForm.html">';}
    echo '</head><body></body></html>';
    }else {
    
    }
    ?>
    </p>
      </div>
      <table width="508" border="0">
      <tr>
        <td><p>First name</p></td>
        <td><label>
        <input type="text" name="Firstname" id="Firstname" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Firstname=$_POST['Firstname']; echo "$Firstname"; }?>" >
       </label> </td> 
      </tr>
      <tr>
        <td width="164"><p>Surname</p></td>
        <td width="334"><label>
        <input type="text" name="Surname" id="Surname" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Surname=$_POST['Surname']; echo "$Surname"; }?>" >
        </label></td>
      </tr>
      <tr>
        <td><p>Email Address</p></td>
        <td><label>
          <input type="text" name="Email" id="Email" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Email=$_POST['Email']; echo "$Email"; }?>">
        </label></td>
      </tr>
      <tr>
        <td><p>Company name</p></td>
        <td><label>
          <input type="text" name="Company" id="Company" value="<?php if (!isset($_POST['submit'])) {} else { 
        $Company=$_POST['Company']; echo "$Company"; }?>">
        </label></td>
      </tr>
      <tr>
        <td><p>Telephone Number</p></td>
        <td><input type="text" name="Telephone" id="Telephone" value="<?php if (!isset($_POST['submit'])) {} else { 
        $phone=$_POST['Telephone']; echo "$phone"; }?>"></td>
      </tr>
      <tr>
        <td><p>Number of employees</p></td>
     
        <td><select name="employee" id="employee">
         <?php foreach ($osrows as $osrow): ?>
          <option value="<?php echo $osrow['id'] ?>"
     
       <?php echo ($osrow['id'] == $_POST['employee'] ? "selected='selected'" : ""); ?> 
      ><?php echo $osrow['label'] ?></option>
          <?php endforeach; ?>
        </select>
          </td>
      </tr>
      <tr>
        <td><p>Consulting type</p></td>
        <td>
          <select name="consultingtype" id="consultingtype">
            <?php foreach ($consultingrows as $consultingrow): ?>
          <option value="<?php echo $consultingrow['id2'] ?>"
     
       <?php echo ($consultingrow['id2'] == $_POST['consultingtype'] ? "selected='selected'" : ""); ?> 
      ><?php echo $consultingrow['label2'] ?></option>
          <?php endforeach; ?>
        </select></td>
      </tr>
      <tr>
        <td height="133"><p>Message</p></td>
        <td valign="top">
          <textarea name="Message" cols="45" rows="10" id="Message"><?php $message=$_POST['Message']; echo "$message"; ?></textarea></td>
      </tr>
      <tr>
        <td valign="top"><p> I agree to LMGR <a href="../Policies/policies.html" target="_blank">Terms and Conditons </a></p>      </td>
        <td valign="top">
        <input name="checkbox" type="checkbox" id="checkbox" value="checked"></td>
      </tr>
    </table>
     
      <input name="submit" type="submit" id="submit" value="Submit" ></form>
    I know it is a little tricky to get your head around this but I have tried to make it as clear as possible to understand.

    Any suggestions or tips would be greatly appreciated,

    Thanks
    Last edited by njccbeach9; 03-27-2010 at 02:06 PM.

  • #14
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You haven't coded it to do anything on validation besides send an email.

    Edit: By the way, your increment lines won't work, so it won't even send an email. To increment a variable, just do this:
    Code:
    $number++;
    Last edited by tomws; 03-27-2010 at 12:03 AM.
    Are you a Help Vampire?

  • #15
    New Coder
    Join Date
    Mar 2010
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    OK thank you very much for your help. I understand what you are saying about the code only sending an email; at this stage I just want to make sure the foundation is there, connecting PHP to the database will come later.


    I have tried using the ++ increment but it does not work. Here is my code:

    Code:
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="form" id="form"><h1 class="divcontent"> By Email </h1>
    <p class="divcontent">Please fill in the following information in order to Contact us about our consulting programmes or any other queries you many have.</p>
    
    <div class="divRightContact"><p>
     
    <?php
    
    if (!isset($_POST['submit'])) {
     $number=0;
    } else {
    $Firstname = $_POST['Firstname'];
     
    $Surname = $_POST['Surname'];
     
    $Email = $_POST['Email'];
     
    $Company = $_POST['Company'];
     
    $phone = ereg_replace('[^0-9]', '', $_POST['Telephone']);
     
    $employee = ($_POST['employee'] or $osrows);
     
    $consultingtype = ($_POST['consultingtype'] or $consultingrows);
     
    $message = $_POST['Message'];
     
    if (!empty($_POST['Firstname'])) {
    $number++;
    } else {
    $Firstname = FALSE;
    echo "<p><font color='red'>Please enter your first name.</font></p>";
    }
    if (!empty($_POST['Surname'])) {
    $number++;
    } else {
    $Surname = FALSE;
    echo "<p><font color='red'>Please enter your surname.</font></p>";
    }
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)){
    $number++;
     } else { echo "<p><font color='red'>Please enter a valid email address.</font></p>"; 
    }  
     if (!empty($_POST['Company'])) {
     $number++;
    } else {
    $Company = FALSE;
    echo "<p><font color='red'>Please enter your company name.</font></p>";
    }
     
    if (!filter_var($phone, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^0[1-9][0-9]{8,9}$/")))) {
    $number++;
    echo "<p><font color='red'>Please enter a valid UK phone number.</font></p>";
    } 
    if ($employee == "1-10" || $employee == "10-20" || $employee == "20-40" || $employee == "40-80" || $employee == "80-160" || $employee == "160-320" || $employee == "320-640" || $employee == "1000+"){
    $number++;
    }else {
    echo "<p><font color='red'>Please select the number of employees</font></p>";
    }
     
       if ($consultingtype == "Finance Support" || $consultingtype == "Marketing Support") { 
     $number++;
    }else {
    echo "<p><font color='red'>Please select the a consulting option</font></p>";
    }
    if (!empty($_POST['Message'])) {
    $number++;
    } else {
    $message = FALSE;
    echo "<p><font color='red'>Please enter a message.</font></p>";
    }
     
    if(isset($_POST['checkbox'])) {
    $number++;
    } else {
    echo "<p><font color='red'>You must agree to the Terms and Conditions.</font></p>";
    }
    
    }
    $osrows = array(
     array('idInvalid' => 0, 'label' => 'Please select'),
     array('id' => 1, 'label' => '1-10'),
     array('id' => 2, 'label' => '20-40'),
     array('id' => 3, 'label' => '40-80'),
     array('id' => 4, 'label' => '80-160'),
     array('id' => 5, 'label' => '160-320'),
     array('id' => 6, 'label' => '320-640'),
     array('id' => 7, 'label' => '1000+'),
    );
    $consultingrows = array(
     array('id2Invalid' => 0, 'label2' => 'Please select'),
     array('id2' => 1, 'label2' => 'Finance Support'),
     array('id2' => 2, 'label2' => 'Marketing Support'),
     
    ); 
    
    if ($number >= 9) {
    echo "<HTML><p>Please wait while we send your email...</p>
     
    <head>";
          $to = 'myemail@myemail.com';
    
    if ($_SERVER['REQUEST_METHOD'] != "POST"){exit;}
    while(list($key,$value) = each($_POST)){if(!(empty($value))){$set=1;}$message = $message . "$key: $value\n\n";} if($set!==1){header("location: $_SERVER[HTTP_REFERER]");exit;}
    $SpamErrorMessage ="No Websites URLs permitted";
    if (preg_match("/http/i","$Firstname")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$Surname")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$Email")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$Company")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$Telephone")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$employee")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$consultingtype")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$message")) {echo "$SpamErrorMessage"; exit ();}
    if (preg_match("/http/i","$checkbox")) {echo "$SpamErrorMessage"; exit ();}
    $message = stripslashes($message);
    $subject = "Website Enquiry";
    $headers = "From: ". $_POST['email'] . "\n";
    $success = mail($to,$subject,$message,$headers); 
                if ($success){echo '<meta http-equiv="refresh" content="1;URL=successForm.html">';}
                else{echo '<meta http-equiv="refresh" content="1;URL=failureForm.html">';}
    echo '</head><body></body></html>';
    }else {
    
    }
    ?>
    I have set it so once each field is correctly filled it adds $number variable by one each time. If the value equals 9 or greater then just execute some PHP code. However the value of $number does not go over 1 for some reason. This is proven when I change the first stage of this IF statement:

    PHP Code:
    if ($number >= 9) { 
    to
    PHP Code:
    if ($number >= 1) { 
    I have also tried using another increment in each of the fields changing:

    PHP Code:
    $number++ 
    to
    PHP Code:
    $number=+
    and this does exactly the same thing because it is exactly the same.

    Please can somebody help me,

    Thanks


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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