PDA

View Full Version : Sending a link using PHP/Ajax



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

thetestingsite
02-21-2008, 07:26 PM
This section of code:



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)
}
}


change to this:



function sendLink() {
if (xmlhttp) {
var email=document.getElementsByName("email");
var link=document.getElementsByName("link");

var data="email=" + email + "&link=" + 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)
}
}


Basically, you are setting the var data with the information from the variables email and link (which have not been set yet. If you move them to before the variable data is set, it should work.

Hope this helps.

TimFA
02-21-2008, 11:46 PM
Thanks for your input testingsite, I'll try it soon. I would've tried it but I have a form that does the same thing, with it setup like that and it works fine.

Tim

thetestingsite
02-22-2008, 12:16 AM
Thinking about it some more, you may want to add the following to the code:



var email=document.getElementsByName("email").value;
var link=document.getElementsByName("link").value;


being that you want to get the value of those fields.

Hope this helps.

TimFA
02-23-2008, 06:17 AM
Sorry for the late reply. I tried having the links above var data with the .value, one time I got [objectHTML] or something like that, I'm having trouble reproducing it now. Anyways, then I tried it below data with and without and still get undefined. Thanks,

Tim