SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple AJAX/PHP Problem

    I'm trying to learn ajax here and I'm having a very simple problem. I was able to create a form and have ajax pickup the info and then php pull the variable back down all on the same page.

    My problem lies in trying to do the same thing but without a form. For example:

    Code:
    ...
    
    <td class='ladder' id='username'>";?><a href="ajaxpage('memberspage.php?username=<?php echo $query2['username']; ?>', 'maincontentarea');" class="info"><?php echo $query2['Username']."
    
    ...
    (partial code)

    Code:
    var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
    var loadedobjects=""
    var rootdomain="http://"+window.location.hostname
    var bustcacheparameter=""
    
    
    
    function ajaxpage(url, containerid){
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
    page_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
    try {
    page_request = new ActiveXObject("Msxml2.XMLHTTP")
    } 
    catch (e){
    try{
    page_request = new ActiveXObject("Microsoft.XMLHTTP")
    }
    catch (e){}
    }
    }
    else
    return false
    page_request.onreadystatechange=function(){
    loadpage(page_request, containerid)
    }
    if (bustcachevar) //if bust caching of external page
    bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
    page_request.open('GET', url+bustcacheparameter, true)
    page_request.send(null)
    }
    
    function loadpage(page_request, containerid){
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    document.getElementById(containerid).innerHTML=page_request.responseText
    }
    
    function loadobjs(){
    if (!document.getElementById)
    return
    for (i=0; i<arguments.length; i++){
    var file=arguments[i]
    var fileref=""
    if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
    if (file.indexOf(".js")!=-1){ //If object is a js file
    fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", file);
    }
    else if (file.indexOf(".css")!=-1){ //If object is a css file
    fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", file);
    }
    }
    if (fileref!=""){
    document.getElementsByTagName("head").item(0).appendChild(fileref)
    loadedobjects+=file+" " //Remember this object as being already added to page
    }
    }
    }
    Ok so what I'm trying to do is when you click on the link in the first code example, it loads a page dynamically but I need that link to carry specific php variables with it.

    I know how to use GET and POST as well, so thats not the issue, the issue is where/how can I add "?username={$query2['username'];}" or something of that nature to the end of my link so that I can then GET that variable when the page loads dynamically?

    I hope I explained that well enough.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    http://bulletproofajax.com/ is where you should be learning ajax from.

    For example, on the code page the Chapter 5 part about People, seems to be fairly close to what you're doing, even if it goes a bit further by using php to fetch html data from a separate page, the structure is all there for you in a best practice manner that's easy to build on.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually have the same problem doing something like that.

    I'm making a website for a friend, but he has just sober html hosting without a php server.
    If I would have php, there was no need for making it in ajax.

    Thus, I made the site with ajax so the content can load in specific places on the site.
    For the contactform, I normally use a php script that sends data to an email adres. But now, without the php on his webhosting, I added the php file on my own hosting (extern from his site).

    <form action="http://www.externsite.com/mailfunction.php" method="post">

    The only problem is that the result is getting in a blank window, and not loaded back in that specific place on the site.

    I tried with some ajax kinda thing, but now he isn't doing anything at all:

    <form action="javascript:ajaxpage('http://www.externsite.com/mailfunction.php', 'content');" method="post">

    How should I do this?


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
  •