473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

mssql post state to email

Hey Everyone hope all ok. I am needing your expertise.

I have an html form and inside of it i have a drop down option to choose a state

Expand|Select|Wrap|Line Numbers
  1. <select name="State">
  2.     <option value="0" selected="selected">Select a State</option>
  3.     <option value="AL">Alabama</option>
  4.     <option value="AK">Alaska</option>
  5.     <option value="AZ">Arizona</option>
  6.     <option value="AR">Arkansas</option>
  7.        etc.....
  8. </select>
Any-time the customer selects a a state and submits the form it goes to my mssql database and pulls an ip address releavant to the staten they choose in the html form.

Expand|Select|Wrap|Line Numbers
  1. +-----------+-------+---------------+
  2. | stateip_id| state |   user_ip     |
  3. +-----------+-------+---------------+
  4. |      1    | AL    | 67.100.244.74 |
  5. |      2    | AK    | 68.20.131.135 |
  6. |      3    | AZ    | 64.134.225.33 |
  7. +-----------+-------+---------------+
So for example, lets say they choose Alabama (AL), when they submit the form i want the code to connect to the php file and then show the ip address releavant to the state, in this case (AL). I have some php code for this thanks to this forum and another


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // visit http://php.net/pdo for more details
  3. // start error handling
  4.  
  5. try 
  6. {
  7. $Server = "00.00.000.000,0000";
  8. $User = "username";
  9. $Pass = "password";
  10. $DB = "dbname";
  11.  
  12. //connection to the database
  13. $dbhandle = mssql_connect($Server, $User, $Pass)
  14.   or die("Couldn't connect to SQL Server on $Server"); 
  15.  
  16. //select a database to work with
  17. $selected = mssql_select_db($DB, $dbhandle)
  18.   or die("Couldn't open database $DB"); 
  19.  
  20. $state = $_POST['State'];
  21.  
  22. $query = "SELECT TOP 1 user_ip
  23.               FROM state_ip
  24.               WHERE state='$state'
  25.               ORDER BY newid()";
  26.  
  27. //execute the SQL query and return records
  28. $result = mssql_query($query);
  29.  
  30. $numRows = mssql_num_rows($result); 
  31. echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 
  32.  
  33. //display the results 
  34. while($row = mssql_fetch_array($result));
  35.  
  36. }
  37. catch (Exception $e)
  38. {
  39.   echo "sorry, there was an error.";
  40.   mail("email@gmail.com", "database error", $e->getMessage(), "From: email@gmail.com");
  41. }
  42.  
  43. if(isset($_POST['email'])) {
  44.  
  45.     // EDIT THE 2 LINES BELOW AS REQUIRED
  46.     $email_to = "email@gmail.com";
  47.     $email_subject = "This is a test";
  48.  
  49.  
  50.     function died($error) {
  51.         // your error code can go here
  52.         echo "We are very sorry, but there were error(s) found with the form you submitted. ";
  53.         echo "These errors appear below.<br /><br />";
  54.         echo $error."<br /><br />";
  55.         echo "Please go back and fix these errors.<br /><br />";
  56.         die();
  57.     }
  58.  
  59.     // validation expected data exists
  60.     if(!isset($_POST['first_name']) ||
  61.         !isset($_POST['last_name']) ||
  62.         !isset($_POST['email']) ||
  63.         !isset($_POST['what']) ||
  64.         !isset($_POST['State']) ||
  65.         !isset($_POST['comments'])) {
  66.         died('We are sorry, but there appears to be a problem with the form you submitted.');       
  67.     }
  68.  
  69.     $what = $_POST['what']; // required
  70.     $first_name = $_POST['first_name']; // required
  71.     $last_name = $_POST['last_name']; // required
  72.     $email_from = $_POST['email']; // required
  73.     $state = $_POST['State']; // not required
  74.     $comments = $_POST['comments']; // required
  75.  
  76.     $error_message = "";
  77.     $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  78.   if(!preg_match($email_exp,$email_from)) {
  79.     $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  80.   }
  81.     $string_exp = "/^[A-Za-z .'-]+$/";
  82.   if(!preg_match($string_exp,$first_name)) {
  83.     $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  84.   }
  85.   if(!preg_match($string_exp,$last_name)) {
  86.     $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  87.   }
  88.   if(strlen($comments) < 2) {
  89.     $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  90.   }
  91.   if(strlen($error_message) > 0) {
  92.     died($error_message);
  93.   }
  94.     $email_message = "Form details below.\n\n";
  95.  
  96.     function clean_string($string) {
  97.       $bad = array("content-type","bcc:","to:","cc:","href");
  98.       return str_replace($bad,"",$string);
  99.     }
  100.  
  101.     $email_message .= "First Name: ".clean_string($first_name)."\n";
  102.     $email_message .= "Last Name: ".clean_string($last_name)."\n";
  103.     $email_message .= "What: ".clean_string($what)."\n";
  104.     $email_message .= "Email: ".clean_string($email_from)."\n";
  105.     $email_message .= "State: ".clean_string($state)."\n";
  106.     $email_message .= "Comments: ".clean_string($comments)."\n";
  107. // create email headers
  108. $headers = 'From: '.$email_from."\r\n".
  109. 'Reply-To: '.$email_from."\r\n" .
  110. 'X-Mailer: PHP/' . phpversion();
  111. if (!mail($email_to, $email_subject, $email_message, $headers))
  112. {
  113.     echo "failed to send message";
  114. }  
  115.  
  116. ?>
  117.  
  118.  
  119. <!-- include your own success html here -->
  120.  
  121. Thank you for contacting us. We will be in touch with you very soon.
  122.  
  123. <?php
  124. }
  125. ?>
This above code works great, i collects the state randomly and sends it to me in an email with the rest of the form info. The problem i have is that in the email it is sending me the letters, for example it is sending me "AL or AZ or CA etc.." it is not sending me the ip address. I think it has something to do with this line of code


Expand|Select|Wrap|Line Numbers
  1. $email_message .= "State: ".clean_string($state)."\n";
mainly the $state part. I want it to select the user_ip part but can not find out how to do it. i have tryed this but it does not work

Expand|Select|Wrap|Line Numbers
  1. $email_message .= "State: ".clean_string('user_ip')."\n";
This is how it displays in an email

Expand|Select|Wrap|Line Numbers
  1. First Name: afdf
  2. Last Name: sfgsdf
  3. What: hellow
  4. Email: sd@fd.com
  5. State: AL
  6. Comments: ali alabama2
i want it to be this

Expand|Select|Wrap|Line Numbers
  1. First Name: afdf
  2. Last Name: sfgsdf
  3. What: hellow
  4. Email: sd@fd.com
  5. State: 123.456.1.21
  6. Comments: ali alabama2
Any help would be great thanks every one
Dec 1 '11 #1
1 1473
omerbutt
638 512MB
try adding echo before the $query
Expand|Select|Wrap|Line Numbers
  1. echo $query = "SELECT TOP 1 user_ip...........
  2.  
and from the browser copy the query and run it in the phpmyadmin to see if the selected ip is present there or not against that state or not
regards,
Omer Aslam
Dec 1 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John | last post by:
This does a little beyond PHP, but I'm looking for a script to use on a php website that will grab my current away message from either AIM, IRC, ICQ, Jabber, Yahoo, or MSN; as I use trillian pro...
8
by: aa | last post by:
If I have a customers database and wanted to automastically create and post an email from a local computer to each customer of the same content - can I do it in PHP? If affirmative which...
4
by: sf | last post by:
Would someome like to post their email filters code. Its so common that probably some standard library supports it or many would have written it already. If I have basic structure, I can take from...
21
by: Dino M. Buljubasic | last post by:
I'd like my application to be able to detect default email application (MS Outlook or whichever is set to be default) so that the user can enter the email body, address, subject line and send the...
7
by: Mamatha | last post by:
Hi i have VB.NET application and i don't know how to send emails throgh SMTP server by using server authentication.I visited some websites,but that websites shows add a reference SMTPsvg.dll ...
2
by: michele | last post by:
Hi, I want to preapare mail for outlook express throughout my vb.net application, can someone help me? thanls
3
by: dirk | last post by:
Hello, As beginner, I'm a bit confused by passing 2 variables using a html-form with method=POST. If passing one of them, it works ; if passing both together, nope. At least when I reload the...
1
by: riyadh81 | last post by:
can any one help me about the following issues. i m working in a web based ERP implementation team. some time php & mssql generates different types of errors. some time user reports errors to IT....
5
by: Kalaram | last post by:
Hello Sir/ Madam, I solve my problem. I can't end loop in this script. I receive two time mail. I wanna receive only one time mail. <?php $txtName=$_REQUEST; ...
0
by: hroarke | last post by:
I have implemented the excellent solution posted here, but am facing the requirement to attach a DOC or PDF to the email and post that email to the Drafts folder of an Exchange 2003 account. After...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.