Results 1 to 5 of 5

Thread: Sending a link using PHP/Ajax

  1. #1
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Sending a link using PHP/Ajax

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

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    This section of code:

    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:

    Code:
    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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. The Following User Says Thank You to thetestingsite For This Useful Post:

    TimFA (02-21-2008)

  4. #3
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    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

  5. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Thinking about it some more, you may want to add the following to the code:

    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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #5
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •