TimFA
02-21-2008, 01:47 AM
I decided to make a php/ajax based site, it works by whenever the page first loads theres an include for one php file which holds the pages content, then you click a link and the ajax does a request to this effect:
"php/php.php?page=" + page
Page of course being defined as loadPage('home'); or such, anyways I've submitted forms through Ajax before and this one should be no different but the content is loaded through ajax which apparently makes a difference. I'm trying to make a Send a link page. Heres the PHP, the form, and the Ajax.
PHP:
elseif ($page=="sendalink" && $task=="send") {
$email=$_POST['email'];
$link=$_POST['link'];
mail($email, "someone sent you $link", $link);
print("Email sent to $email.");
}
elseif ($page=="sendalink") {
print("<span class=\"title\">
send link--:
</span>
<br>
<form name=\"sendalink\">
Email of the person you are sending it to:
<br>
<input type=\"text\" name=\"email\">
<br>
<br>
The link URL:
<br>
<input type=\"text\" name=\"link\">
<br>
<br>
<a href=\"#\" onClick=\"sendLink();\">Send!</a>
");
}
Ajax:
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
function RSchange() {
if (xmlhttp.readyState==1) {
document.getElementById('contentdiv').innerHTML="Loading..."
}
else if (xmlhttp.readyState==4) {
document.getElementById('contentdiv').innerHTML=xmlhttp.responseText
}
}
function loadPage(page) {
if (xmlhttp) {
d=document
xmlhttp.open("GET","php/php.php?page=" + page, true);
xmlhttp.onreadystatechange=RSchange
xmlhttp.send(null)
}
}
function sendLink() {
if (xmlhttp) {
var data="email=" + email + "&link=" + link;
var email=document.getElementsByName("email");
var link=document.getElementsByName("link");
d=document
xmlhttp.open("POST","php/php.php?page=sendalink&task=send", true);
xmlhttp.onreadystatechange=RSchange
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(data)
}
}
I know its not working because I get "Email sent to undefined" which I recently discovered any blank fields in Ajax are "undefined".
Thanks for any help guys.
Tim
"php/php.php?page=" + page
Page of course being defined as loadPage('home'); or such, anyways I've submitted forms through Ajax before and this one should be no different but the content is loaded through ajax which apparently makes a difference. I'm trying to make a Send a link page. Heres the PHP, the form, and the Ajax.
PHP:
elseif ($page=="sendalink" && $task=="send") {
$email=$_POST['email'];
$link=$_POST['link'];
mail($email, "someone sent you $link", $link);
print("Email sent to $email.");
}
elseif ($page=="sendalink") {
print("<span class=\"title\">
send link--:
</span>
<br>
<form name=\"sendalink\">
Email of the person you are sending it to:
<br>
<input type=\"text\" name=\"email\">
<br>
<br>
The link URL:
<br>
<input type=\"text\" name=\"link\">
<br>
<br>
<a href=\"#\" onClick=\"sendLink();\">Send!</a>
");
}
Ajax:
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
function RSchange() {
if (xmlhttp.readyState==1) {
document.getElementById('contentdiv').innerHTML="Loading..."
}
else if (xmlhttp.readyState==4) {
document.getElementById('contentdiv').innerHTML=xmlhttp.responseText
}
}
function loadPage(page) {
if (xmlhttp) {
d=document
xmlhttp.open("GET","php/php.php?page=" + page, true);
xmlhttp.onreadystatechange=RSchange
xmlhttp.send(null)
}
}
function sendLink() {
if (xmlhttp) {
var data="email=" + email + "&link=" + link;
var email=document.getElementsByName("email");
var link=document.getElementsByName("link");
d=document
xmlhttp.open("POST","php/php.php?page=sendalink&task=send", true);
xmlhttp.onreadystatechange=RSchange
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(data)
}
}
I know its not working because I get "Email sent to undefined" which I recently discovered any blank fields in Ajax are "undefined".
Thanks for any help guys.
Tim