[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

Using xmlhttprequest post in PHP and Facebox

Asked by matttoca in Asynchronous Javascript and XML (AJAX), PHP and Databases, PHP Scripting Language

Tags: AJAX, PHP, xmlhttprequest

I'm trying to use Facebox in my website. I'm been successful in getting it to display the page i want in the modal window that Facebox creates. The problem is I can't submit the form, that the window loads, with the desired results. I looked online to figure out how I could send the form. Some one suggested using AJAX, so I looked into that. I think I got most of the code right I just don't know what's wrong with it. I'll post the code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
//link that starts it all  
<a href="email.php?user=doug" rel="facebox">EMAIL ME</a>   

//form that calls to the fucntion  
<? $user = $_GET['user']; ?>
<form method="post" onclick="emailForm();">
<input type="hidden" name="user" value="<? echo $user; ?>">
<input name="name" type="text"></td>
<input name="company" type="text"></td>
<input name="email" type="text"></td>
<textarea name="message" rows="5" wrap></textarea></td>
<input type="submit" name="submit" value="Send"></td>
</form>   

//ajax script to connect the pages  
<script type="text/javascript">
var time_variable; 
function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
} 
var xmlhttp = new getXMLObject(); 
function emailForm() {
	var getdate = new Date();  //Used to prevent caching during ajax call
	
	if (xmlhttp) {
		var user = document.getElementByName("user");
		var name = document.getElementByName("name");
		var company = document.getElementByName("company");
		var email = document.getElementByName("email");
		var message = document.getElementByName("message");
		
		http.open("POST", "send.php", true);
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send("user=" + user.value + "&name=" + name.value + "&company=" + company.value + "&email=" + email.value + "&message=" + message.value); 
	} else {
		alert ("Browser does not support HTTP Request");
		return;
	}
} 
function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element 
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}
</script>   

//the send.php simplified
// Connects to your Database
include("dbinfo.inc.php");
mysql_connect("$server", $username, $password);
@mysql_select_db($database) or die("Unable to select database"); 
//get posted items from form
$user = $_POST['user'];
$name = $_POST['name'];
$company = $_POST['company'];
$theiremail = $_POST['email'];
$message = nl2br($_POST['message']); 
//look up email for user
$query = "SELECT email FROM board WHERE name = '$user'";
$results = mysql_query($query);
$youremail = mysql_fetch_array($results); 
//send email out
//code here uses the mail() fucntion
[+][-]11/05/09 07:46 AM, ID: 25750697Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/05/09 08:28 AM, ID: 25751104Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/05/09 10:27 AM, ID: 25752352Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625