...

View Full Version : How do you use PHP to connect to a Microsoft SQL Express database?



njccbeach9
03-22-2010, 11:05 PM
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:



<?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

tomws
03-23-2010, 01:29 AM
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?

njccbeach9
03-23-2010, 08:35 PM
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:


<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

tomws
03-23-2010, 09:15 PM
You haven't written any db connection code. This (http://www.webcheatsheet.com/PHP/connect_mssql_database.php) was the first result for me from Google (php mssql db connection (http://www.google.com/search?q=php+mssql+db+connection)). The second result (http://php.net/manual/en/function.mssql-connect.php) takes me to php.net where I can then see all of the mssql functions (http://www.php.net/manual/en/ref.mssql.php).

Changing the search query (http://www.google.com/search?hl=en&client=firefox-a&hs=wz&rls=org.mozilla%3Aen-US%3Aofficial&q=php+ms+sql+server+express+connection) used on Google brought me back to the mssql page (http://php.net/manual/en/book.mssql.php) on php.net for the first result and this verbose instruction set (http://www.easysoft.com/developer/languages/php/sql_server_unix_tutorial.html) for the second.

njccbeach9
03-23-2010, 09:56 PM
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."


<?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

tomws
03-23-2010, 10:00 PM
What have you searched for? Did you read through any of the links provided? Have you read through any tutorials?

njccbeach9
03-23-2010, 10:06 PM
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.

tomws
03-23-2010, 10:17 PM
I don't think you're paying attention to what you're reading. I'm finding tutorials (http://www.rochester.edu/IT/web/WebHelp/mssql/index.html) and FAQs (http://dba.fyicenter.com/faq/sql_server_2/PHP_MSSQL_Connection_and_Query_Execution.html) 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.

njccbeach9
03-23-2010, 10:32 PM
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

tomws
03-23-2010, 10:40 PM
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 (http://www.freewebmasterhelp.com/tutorials/phpmysql/) (this one using mysql) that shows that same pattern.

MattF
03-23-2010, 11:20 PM
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.
}

njccbeach9
03-26-2010, 06:58 PM
Sorry for the late reply, I'll work on it.

Thanks for your help

njccbeach9
03-26-2010, 10:40 PM
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:


<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

tomws
03-26-2010, 11:01 PM
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:

$number++;

njccbeach9
03-27-2010, 01:05 PM
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:


<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:


if ($number >= 9) { to
if ($number >= 1) {

I have also tried using another increment in each of the fields changing:


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

Please can somebody help me,

Thanks

tomws
03-28-2010, 04:08 AM
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:


if ($number >= 9) { to
if ($number >= 1) {

That's a good attempt at debugging: test whether a variable has the expected value at a given location. Congratulations!



I have also tried using another increment in each of the fields changing:


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

No, those are not the same thing. The second is assigning a value. The "other" short form of incrementing is

$number += 1;
Flipped characters mean completely different operations.

All that being said, it's not the incrementing that's the problem. The root problem is variable scope and it's hard to see because of sloppy coding. When I pasted the whole thing into notepad++ and cleaned it up, I finally saw what's happening. Here's a severely trimmed version:

if (!isset($_POST['submit']))
{
$number=0;
}
else
{
// do some incrementing on $number
}
if ($number >= 9)
{
// do stuff
}

Do you know anything about variable scope? A variable defined within a block can only be modified within that same block and its value is only available within that same block. (No, it's not really that simple, but it will work here.) Where is $number first defined? Inside that first if block. Where are you attempting to modify it? Inside that first else block. Those are two different variables in memory because they are from two different blocks! Again, where are you testing it? Outside of both blocks. That test doesn't know anything about the other two because it's looking in its own scope, and $number isn't previously defined there so the value is zero (which is not >= 9, of course).

How do you fix it? Move the $number = 0; before the first if statement. It doesn't even make any sense to have it inside anyway. If you do that, the increment operations in else will increment that variable because the else is inside of the same block (is a "child" block) where $number is defined. And the test at the end will compare the against the same variable for the same reason.

Move that variable declaration and see what happens. There may be some other things to fix after that.

njccbeach9
03-28-2010, 02:52 PM
Thanks for outlining this problem, I moved the $number=0 variable outside the if (!isset($_POST['submit'])) statement and changed the following to this which all works perfectly:


if ($number >= 8) {
echo "<HTML><p>Please wait while we send your email...</p>

<head>";
$to = 'njccbeach9@hotmail.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 {

}

So my final code looks like this:


<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
$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++;
} 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 >= 8) {
echo "<HTML><p>Please wait while we send your email...</p>

<head>";
$to = 'njccbeach9@hotmail.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>

Now the next step, I going to try and use some PHP to connect to MS SQL express. I'll get back to you if I need help.

Thanks for your input anyway it really is appreciated!!

njccbeach9
03-29-2010, 09:45 PM
Hi,

I have found some code for a mysql database and I have changed it so that it can connect to a ms sql database. I have tried doing this:


if ($number >= 8) {

$server = 'Nicholas-PC\ASPNETDB.MDF';

// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');

if (!$link) {
die('Something went wrong while connecting to our database');
}


// table name
$tbl_name=contact_us_data;

// Random confirmation code
$ticketID=md5(uniqid(rand()));

// values sent from form
$Firstname;
$Surname;
$Email;
$Company;
$phone;
$employee;
$consultingtype ;
$message;
$checkbox;

// Insert data into database
$sql="INSERT INTO $tbl_name(ticketID, firstname, surname, email, company, telephone, numberemployees, consultingtype, message, confirmation)VALUES('$Firstname', '$Surname', '$Email', '$Company', '$phone', '$employee', '$consultingtype', '$message', '$checkbox')";
$result=mssql_query($sql);

// if suceesfully inserted data into database, send confirmation link to email
if($result){

// send e-mail to ...
$to=$Email;

// Your subject
$subject="Your confirmation link here";

// From
$header="from: your name <your email>";

// Your message
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="confirmation.php?passkey=$ticketID";

// send email
$sentmail = mail($to,$subject,$message,$header);

}

// if not found
else {
echo "We have not found your email in our database";
}

// if your email succesfully sent
if($sentmail){
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send Confirmation link to your e-mail address";
}
}
?>

I worried there might be a few mistakes in here but I have scanned and I'm not sure what's going on. When I run the page on XAMPP server I get the following error message:


Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\Contact Us\contactus.php on line 398

Am I missing an add-on? Please help somebody.

Thanks

tomws
03-29-2010, 09:50 PM
The 'M' in XAMPP is for MySQL. It's most likely not configured for MSSQL by default, so you may need to enable to extension. Here's a page (http://mschat.net/blog/index.php?topic=7) that looks like it may help.

Dormilich
03-29-2010, 09:58 PM
you can also try PHP’s PDO to connect to MSSQL.

njccbeach9
03-29-2010, 10:24 PM
Hi,

I have followed the link and it is fantastic. I know know that XAMPP can connect to a MS SQL database but I have figured out that my code is not up for the job. I get a new error:

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: Nicholas-PC\ASPNETDB.MDF in C:\xampp\htdocs\Contact Us\contactus.php on line 398
Something went wrong while connecting to our database

My code is the following:


if ($number >= 8) {

$server = 'Nicholas-PC\ASPNETDB.MDF';

// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');

if (!$link) {
die('Something went wrong while connecting to our database');
}


// table name
$tbl_name=contact_us_data;

// Random confirmation code
$ticketID=md5(uniqid(rand()));

// values sent from form
$Firstname;
$Surname;
$Email;
$Company;
$phone;
$employee;
$consultingtype ;
$message;
$checkbox;

// Insert data into database
$sql="INSERT INTO $tbl_name(ticketID, firstname, surname, email, company, telephone, numberemployees, consultingtype, message, confirmation)VALUES('$Firstname', '$Surname', '$Email', '$Company', '$phone', '$employee', '$consultingtype', '$message', '$checkbox')";
$result=mssql_query($sql);

// if suceesfully inserted data into database, send confirmation link to email
if($result){

// send e-mail to ...
$to=$Email;

// Your subject
$subject="Your confirmation link here";

// From
$header="from: your name <your email>";

// Your message
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="confirmation.php?passkey=$ticketID";

// send email
$sentmail = mail($to,$subject,$message,$header);

}

// if not found
else {
echo "We have not found your email in our database";
}

// if your email succesfully sent
if($sentmail){
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send Confirmation link to your e-mail address";
}
}

I'm going to be working on the problem, but if anybody knows exactly what the problem is and has an immediate solution, it would be of great benefit to me.

Thanks for all your help guys,

njccbeach9
04-01-2010, 11:10 AM
Hi,

I have done a lot of work and have searching and have finally cracked the problem but not quite. You see when I submit the form, it goes ahead as I asked it to and inserts a row into my SQL Express database table.

However, when it gives the results of the number of employees and consulting type it always comes back with a value of 1. I have tried to changed my values but because so much relies of something else it just messes everything up.


<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="form" id="form"><h1> By Email </h1>
<p>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
$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'];

$checkbox = $_POST['checkbox'];

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}$/")))) {
echo "<p><font color='red'>Please enter a valid UK phone number.</font></p>";
} else {
$number++;
}
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) {

if ($db = @mssql_connect('.\SQLEXPRESS', '',
'')) {
mssql_select_db('ASPNETDB');
echo 'Connected to the database.';


$tbl_name=contactUs_data;

// Random confirmation code
$ticketID=md5(uniqid(rand()));

// values sent from form
$Firstname;
$Surname;
$Email;
$Company;
$phone;
$employee;
$consultingtype ;
$message;
$checkbox;

// Insert data into database
$sql="INSERT INTO $tbl_name(ticketID, firstname, surname, email, company, telephone,employeenumber, consultingtype, message, confirmation)VALUES('$ticketID','$Firstname', '$Surname', '$Email', '$Company', '$phone', '$employee', '$consultingtype', '$message', '$checkbox')";
$result=mssql_query($sql);


if($result){

// send e-mail to ...
$to=$Email;

// Your subject
$subject="Your confirmation link here";

// From
$header="from: your name <your email>";

// Your message
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="confirmation.php?passkey=$ticketID";

// send email
$sentmail = mail($to,$subject,$message,$header);

}

// if your email succesfully sent
if($sentmail){
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send Confirmation link to your e-mail address";
}




mssql_close($db);
} else {
echo 'Connection failed.';
}
}
?>
</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>


Don't get me wrong. I am not asking anybody to do the code for me. I am just asking which parts need to be changed and to what in order for my form to work correctly.

Thank you very much

tomws
04-01-2010, 02:37 PM
It's coming along nicely. A little work tends to clear up a lot of confusion, eh?

When you say that the value for each is 1, does that mean the select box options for those are being re-displayed with selected="selected" on the value="1" option? Or is it something else? Are those fields inserted correctly into the database?

Here's something I'm confused about:

$employee = ($_POST['employee'] or $osrows);
$consultingtype = ($_POST['consultingtype'] or $consultingrows);

Both $osrows and $consultingrows are undefined at that point in the code. So $employee and $consultingtype will either be set to the posted value or false. Then, later on:

$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'),

);

Those should probably be defined earlier in the code before they're used in the variable assignments above.

Something else I've just noticed is that value="0" should never appear, according to this code, and you should be triggering a couple of "undefined index" warnings in your logs. Why? Let's use the employee select as an example (modified code structure a little):

<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>
$osrows[0], the first sub-array in $osrows, doesn't have an 'id' index. Same for $constultingrows. They both have that 'Invalid' text added. A better choice would be to keep the same id/id2 key name and up in your validation section, test that the value != 0. I'm not sure that would affect the default value on the form re-display, though.

Another unrelated item, you're "over-building" those arrays. Not necessary to change them, but you could easily get by without nested arrays since PHP array are numerically indexed by default. I wouldn't mess with it until after debugging the current issue, but they could be as simple as:

$osrows = array(
'Please select',
'1-10',
'20-40',
'40-80',
'80-160',
'160-320',
'320-640',
'1000+'
);

So, the string '20-40' is accessible by $osrows[2]. That change would necessitate modifying the foreach loops down in the form, too.

As for the current problem, have you tried debugging by dumping the posted values up at the top? What values are actually inserted into the db for those?

njccbeach9
04-01-2010, 03:40 PM
Thanks for your reply,

I am sorry I know it sounds really confusing but I'll try my best to explain.

There are no build errors or any problems submitting the form. It is not the user that sees the problem, it is me the receiver. You know when the user submits a form and it contains a drop-down, it sends the value of the selection the user made not the selection. Say for example:

How hours sleep do you get:

Please select
1 hour Value=1
2 hours Value=2
3 hours Value=3
etc

If the user selects "2 hours" it won't publish "2 hours" in the database once the user has submitted because it only publishes the value so it would appear as 2.

Relating to my situation, whether the user selects 1 hour or 2 hours or even 3hours it still returns the value as 1.

So, after a lot of thinking I thought it must be do with the:

array('id' => 1, 'label' => '1-10'),

id in the array or the id2 in the second array. (By the way I did try and break it down like you suggested but it didn't return with 1-10 etc it just returned with characters like P, 1, 2, 3, 2, 1 something like that, so I decided it was best tot keep it how it was.)

Because the only thing about the array was that was consistantly the same was the id and in the second array id2. I have tried changing them to something like this to see if I would get a different number appearing in my database:


$osrows = array(
array('idInvalid' => 2, 'label' => 'Please select'),
array('id' =>3, 'label' => '1-10'),
array('id' => 4, 'label' => '20-40'),
array('id' => 5, 'label' => '40-80'),
array('id' => 6, 'label' => '80-160'),
array('id' => 7, 'label' => '160-320'),
array('id' => 8, 'label' => '320-640'),
array('id' => 9, 'label' => '1000+'),
);
$consultingrows = array(
array('id2Invalid' => 2, 'label2' => 'Please select'),
array('id2' => 3, 'label2' => 'Finance Support'),
array('id2' => 4, 'label2' => 'Marketing Support'),

);

I have just thought, could you change the id and id2 to something different after every number like this:


<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', 'id3'] ?>"

<?php echo ($consultingrow['id2', 'id3', 'id4'] == $_POST['consultingtype'] ? "selected='selected'" : ""); ?>
><?php echo $consultingrow['label2' , 'label3'] ?></option>


I haven't put all the corrections but it just the principle.

I'll just show you what it looks like in my database:

I created a basic webpage and uploaded a screeshot on an old site of my database file so you can see the exact problem:

http://cakesrus.110mb.com/DatabaseImage.html

Thanks for you suggestions anyway,

Good to hear from you,

tomws
04-01-2010, 03:55 PM
How hours sleep do you get:

Please select
1 hour Value=1
2 hours Value=2
3 hours Value=3
etc

If the user selects "2 hours" it won't publish "2 hours" in the database once the user has submitted because it only publishes the value so it would appear as 2.

Relating to my situation, whether the user selects 1 hour or 2 hours or even 3hours it still returns the value as 1.


"still returns the value as 1" means "I always see '1' in the database field for both `employeenumber` and `consultingtype`", yes? If that's the case, we're overlooking something simple in the setting of the form values or the handling of those values.

What is the actual rendered HTML code on form load? Not PHP. Go to the form and view source. Check those two select boxes. Are the values set appropriately or are they all '1'? If correct, have you dumped that posted value at the top to confirm its value is not '1' coming into the form handler?

njccbeach9
04-01-2010, 04:13 PM
Oddly it does does have different values for different selections see my HTML code at run time:


<select name="employee" id="employee">
<option value=""

selected='selected'
>Please select</option>

<option value="3"


>1-10</option>
<option value="4"


>20-40</option>
<option value="5"


>40-80</option>
<option value="6"


>80-160</option>
<option value="7"


>160-320</option>
<option value="8"


>320-640</option>

<option value="9"


>1000+</option>
</select></td>
</tr>
<tr>
<td><p>Consulting type</p></td>
<td>
<select name="consultingtype" id="consultingtype">
<option value=""

selected='selected'
>Please select</option>

<option value="3"


>Finance Support</option>
<option value="4"


>Marketing Support</option>
</select>

Maybe it is something do with the database? The data type. I have set it to:
nvarchar(50). Do you think this will make any difference? You have to remember that not all the data types are supported by PHP.

Thanks

tomws
04-01-2010, 05:26 PM
Are the option values the same after a form submission?

Still need answers to previous questions, too:
- Do you always see the value of the first option in the database no matter what option was selected?
- Have you dumped the posted values to confirm they are what's expected?

njccbeach9
04-01-2010, 06:20 PM
Are the option values the same after a form submission?


Yes, I think so.


Do you always see the value of the first option in the database no matter what option was selected?

This is exactly the problem I have. Please see this link, it will only take a few seconds because I have the screenshot and you can see what the exact is.

http://cakesrus.110mb.com/DatabaseImage.html


Have you dumped the posted values to confirm they are what's expected?

I don't know what you mean by dumped the posted values but what I do know is when I preview the source of the page the options have different values however, when I submit the form to the database it only gives me one value which is 1. See the link above.

---
I am 99% sure that if you spare a moment looking at the link above you will understand exactly what happening or what's gone wrong.

What I don't understand is that the options are given different values and this is evident in the HTML code but the only value that appears under employeenumber and consultingtype is 1.

Thanks for your comments anyway,

njccbeach9
04-01-2010, 06:30 PM
One more thing. I tried changing the values to something a little different:


$osrows = array(
array('idInvalid' => 2, 'label' => 'Please select'),
array('id' =>"1-10", 'label' => '1-10'),
array('id' => "20-40", 'label' => '20-40'),
array('id' => "40-80", 'label' => '40-80'),
array('id' => "80-160", 'label' => '80-160'),
array('id' => "160-320", 'label' => '160-320'),
array('id' => "320-640", 'label' => '320-640'),
array('id' => "1000+", 'label' => '1000+'),
);
$consultingrows = array(
array('id2Invalid' => 2, 'label2' => 'Please select'),
array('id2' => "Financial Support", 'label2' => 'Finance Support'),
array('id2' => "Marketing Support", 'label2' => 'Marketing Support'),

);

All of this made no difference at all, so I thinking it must be the database. What do you think?

tomws
04-01-2010, 06:50 PM
All of this made no difference at all, so I thinking it must be the database. What do you think?

And it still shows '1'? Definitely need to know what's being posted. When I say "dump" the variable, I mean echo/print/die/whatever so you can see the value. So, up near the top, something like this would work:

die('employee: '.$_POST['employee']);
If that shows the wrong value, something weird is happening with the POST data. If it shows the correct value, dump the query string and see what values you have there. If it's right in the query, your db is doing something odd.

A quick search suggested that the nvarchar field is the same as a varchar but allows for unicode data, so I don't know what could be happening there.

njccbeach9
04-01-2010, 07:10 PM
Sorry, I didn't understand what you meant by "dumping posting values."

Anyway thanks for explaining I echoed out $employee and $consultingtype did this and got a result which surprised me:


if($sentmail){
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send Confirmation link to your e-mail address";
}
echo "<br />";
echo "$employee <br/>";

echo "$consultingtype";



mssql_close($db);
} else {
echo 'Connection failed.';
}
}
?>

It came back with the value 1 for both regardless of the choice.

Just a thought, what about those two variables $osrows and $consultingrows.

Thank you very much, quite a clever technique, I'll have to use it in the future.

tomws
04-01-2010, 07:18 PM
Um... wait a sec. Are these lines still in the code?

$employee = ($_POST['employee'] or $osrows);

$consultingtype = ($_POST['consultingtype'] or $consultingrows);

Still exactly like that? If so, try changing them to this and adding in a couple of debug lines for fun:

$employee = $_POST['employee'];
$consultingtype = $_POST['consultingtype'];
echo "<br />";
echo ($_POST['employee'] or $osrows)."<br/>";
echo ($_POST['consultingtype'] or $consultingrows);

What I expect: on first page load, you'll see two zeros. If you fake a form and submit (maybe leave it incomplete... "forget" a name or something), I think you'll see the actual values.

njccbeach9
04-01-2010, 09:07 PM
Yes sorry, I should have said it was a snippet from my code. My whole code looks like this:


<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="form" id="form"><h1> By Email </h1>
<p>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
$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'];

$checkbox = $_POST['checkbox'];

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}$/")))) {
echo "<p><font color='red'>Please enter a valid UK phone number.</font></p>";
} else {
$number++;
}
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' => 2, 'label' => 'Please select'),
array('id' =>"1-10", 'label' => '1-10'),
array('id' => "20-40", 'label' => '20-40'),
array('id' => "40-80", 'label' => '40-80'),
array('id' => "80-160", 'label' => '80-160'),
array('id' => "160-320", 'label' => '160-320'),
array('id' => "320-640", 'label' => '320-640'),
array('id' => "1000+", 'label' => '1000+'),
);
$consultingrows = array(
array('id2Invalid' => 2, 'label2' => 'Please select'),
array('id2' => "Financial Support", 'label2' => 'Finance Support'),
array('id2' => "Marketing Support", 'label2' => 'Marketing Support'),

);

if ($number >= 9) {

if ($db = @mssql_connect('.\SQLEXPRESS', '',
'')) {
mssql_select_db('ASPNETDB');
echo 'Connected to the database.';


$tbl_name=contactUs_data;

// Random confirmation code
$ticketID=md5(uniqid(rand()));

// values sent from form
$Firstname;
$Surname;
$Email;
$Company;
$phone;
$employee or $osrows;
$consultingtype or $consultingrows ;
$message;
$checkbox;

// Insert data into database
$sql="INSERT INTO $tbl_name(ticketID, firstname, surname, email, company, telephone,employeenumber, consultingtype, message, confirmation)VALUES('$ticketID','$Firstname', '$Surname', '$Email', '$Company', '$phone', '$employee', '$consultingtype', '$message', '$checkbox')";
$result=mssql_query($sql);


if($result){

// send e-mail to ...
$to=$Email;

// Your subject
$subject="Your confirmation link here";

// From
$header="from: your name <your email>";

// Your message
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="confirmation.php?passkey=$ticketID";

// send email
$sentmail = mail($to,$subject,$message,$header);

}

// if your email succesfully sent
if($sentmail){
echo "Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
echo "Cannot send Confirmation link to your e-mail address";
}
$employee = $_POST['employee'];
$consultingtype = $_POST['consultingtype'];
echo "<br />";
echo ($_POST['employee'] or $osrows)."<br/>";
echo ($_POST['consultingtype'] or $consultingrows);


mssql_close($db);
} else {
echo 'Connection failed.';
}
}
?>
</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>

Because I have validation controls in it is a little more difficult to test. However, I tried commenting out the code for both the number of employees and consulting type and when I submitted it no number came up, not even zero, just a blank. I don't know why the selected values are not being displayed because revealing the source shows it. This must have something to do with the problem:


<option value="<?php echo $consultingrow['id2'] ?>"

AND


<option value="<?php echo $osrow['id'] ?>"

I am thinking

Could you change it to something like this if you went back and edited the ids accordingly:


<option value="<?php echo $consultingrow['id2', 'id3'] ?>"

etc and the same principle with $osrows. Of course you would have to go back to where $osrows was declared and change it to something like this:


$consultingrows = array(
array('id2Invalid' => 2, 'label2' => 'Please select'),
array('id2' => "Financial Support", 'label2' => 'Finance Support'),
array('id3' => "Marketing Support", 'label2' => 'Marketing Support'),

I am going to try this but what do you think?

Thanks

njccbeach9
04-01-2010, 09:15 PM
My idea didn't work after playing around to make sure an error didn't load by putting [id2][id3] it came up with a blank selection and with the capital letter F. Where it got that from I don't know.

So I reverted back.

tomws
04-01-2010, 09:47 PM
Unless I miss my guess, you're chasing after the wrong thing there. Try that suggestion from my last post and report the results.

njccbeach9
04-02-2010, 08:38 AM
I have your code incorporated into mine and it still makes no difference. I'm going to have to do more playing around

njccbeach9
04-02-2010, 09:20 AM
Do you know how to solve this problem so that when the php drop down returns the value of the selection the user made instead of a constant number: 1?


Thanks,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum