Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    115
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How can I do php lookup without leaving my javascript page?

    I am a novice to PHP, so any advice would be most appreciated.

    I have a javascript page which includes the following logic:

    Code:
    var dirnames=new Array("photos1/","photos2/","photos3/");
    .
    <form>
    <select name="theme">
    <option value="1">theme 1</option>
    <option value="2">theme 2</option>
    <option value="3">theme 3</option>
    </select>
    </form>
    .
    themeval=document.forms[0].theme.value*1;
    .
    fullimgname=dirnames[themeval]+imgname+".jpg";
    .
    display the image
    I have added another field to the form, a text field called "tags", and the page checks whether anything has been entered here. If it has, I want to ignore the "theme" and instead do a PHP lookup to return an array full of image details from an external database based on the value entered in the "tags" field.

    Everything I read about PHP seems to say I need to leave the current page and pass my "tags" variable to a separate PHP page to do the lookups.

    My problem is that all the forms, display, processing code etc is in Javascript on the original page, including numerous variables which have been set to values I don't want to lose. I need to use this code (including previously set variables) to process the image details returned from the PHP lookup.

    I have previously used php "echo" statements to save php variables into javascript variables, but obviously once I have left my page to go into another this method will be useless as I need to get the values back into the old page.

    How can I do my PHP lookup and get the returned values back into my page without leaving the page? Any advice appreciated. Thanks.

  • #2
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use Ajax to call a serverside script using javascript
    Here is a basic example that can be changed to suit
    http://www.dynamicdrive.com/dynamici...jaxcontent.htm

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    115
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the link Degsy.

    It looks very useful, but although it loads a php page it doesn't load it completely. I have no idea why, as the page it loads works fine by itself.

    The calling page is at:
    http://www.moonmilk.org/worlds/testajax.php
    and also
    http://www.moonmilk.org/worlds/testajax.htm
    (identical, just the filename extension differs)
    Code is:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    
    <title>Ajax test page</title>
    
    
    <script type="text/javascript">
    
    
    
    /***********************************************
    
    * Dynamic Ajax Content- � Dynamic Drive DHTML code library (www.dynamicdrive.com)
    
    * This notice MUST stay intact for legal use
    
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    
    ***********************************************/
    
    
    
    var loadedobjects=""
    
    var rootdomain="http://"+window.location.hostname
    
    
    
    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)
    
    }
    
    page_request.open('GET', url, 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
    
    }
    
    }
    
    }
    
    //---------------------------------------
    //---------------------------------------
    
    function makeimg()
    {
    
    ajaxpage('test-php-to-js.php', 'phpbody');
    
    
    document.write("<p>now back in original page<br>the total found is " + flickrtotal + "<br>");
    
    } 
    
    //---------------------------------------
    
    </script>
    
    <style type="text/css">
    
    #phpbody        
    	{
    	background-color: transparent;
    	text-align: left;
    	position: absolute;
    	top: 60px;
    	left: 10px;
    	height: 100%;
    	width: 600px;
    	visibility: visible;
    	display: block;
    	border: 1px solid red;
    	}
    	
    </style>
    
    </head>
    
    <body>
    
    <form action="" name="imgform" method="post" onSubmit="makeimg();return false;">
    
    <input type=text name="flickrtags" size=20 maxlength=100 value="enter tags">
    
    <a href="#" onClick="makeimg();">go go go</a>
    
    </form>
    
    <div id="phpbody">
    here is the body
    </div>
    
    </body>
    </html>
    The page it calls is:
    http://www.moonmilk.org/worlds/test-php-to-js.php

    Code is:
    Code:
    <html>
    <head>
    <title>PHP flickr images test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="keywords" content="hey there">
    
    <script type="text/javascript">
    <!--
    
    var maxarray=new Array();
    var flickrtotal=0;
    
    //-->
    </script>
    
    </head>
    <body>
    
    <?php
    require_once("phpFlickr/phpFlickr.php");
    
    $maximgs = 11;
    $tag1="open";
    $tag2="blue";
    
    // Create new phpFlickr object
    $f = new phpFlickr("fbc2211a8778e91cd26d3de027eac206");
    
    echo "<p>photos tagged with \"$tag1 $tag2\" with total of $maximgs<br>\n";
    
    // Search by tags
    $photos = $f->photos_search(array("tags"=>"$tag1 $tag2", "sort"=>"relevance", "per_page"=>$maximgs));
    
    
    echo "per page is " . $photos['perpage'] . " total is " . $photos['total'] . "<br><br>";
    
    $flickrtotal = $photos['total'];
    
    foreach ($photos['photo'] as $photo) {
        // Build image and link tags for each photo
        $val1 = "'http://www.flickr.com/photos/$photo[owner]/$photo[id]'";
        echo "<a href=http://www.flickr.com/photos/$photo[owner]/$photo[id]>";
        $val2 = '"' . $f->buildPhotoURL($photo, "thumbnail") . '"';
        echo "<img border='0' alt='$photo[title]' " . "src=" . $f->buildPhotoURL($photo, "thumbnail") . ">";        
        echo "</a>";
        
        $i++;
        $maximgsarray[$i]=$val2;
        echo "<br>$val1<br>$maximgsarray[$i]<br>";
    
    }
    
    echo "</p>\n";
    echo "length of the array is " . count($maximgsarray) . "<br>";
    
    ?>
    
    <script type="text/javascript">
    <!--
    
    <?php
    
    
    echo "flickrtotal = $flickrtotal;\n";
    
    $length = count($maximgsarray);
    
    // assign the php variables to javascript variables
    for ($i = 1; $i <= $length; $i++)
      {
      echo "maxarray[$i] = $maximgsarray[$i];\n";
      }
    ?>
    
    document.write("<p>now in javascript<br>the total found is " + flickrtotal + "<br>");
    
    for (i=1; i < maxarray.length; i++) 
     {      
     document.write("maxarray[" + i + "] is "+maxarray[i]+"<br>");
     }
    document.write("</p>");
    
    htmlstring=""; 
    
    for (i=1; i < maxarray.length; i++) 
     {      
     htmlstring=htmlstring+'<img src="'+maxarray[i]+'" width="45" height="30">';
     }
    htmlstring=htmlstring+'<br><br>';
    document.write(htmlstring);
    
    //-->
    </script>
        
    </body>
    </html>
    The latter page basically uses a php interface to search for photos and then returns their details in php variables, after which I convert the php variables to javascript variables.

    When I call the page using Ajax, the php search works fine but the conversion to javascript doesn't occur. I also have the line
    Code:
    document.write("<p>now back in original page<br>the total found is " + flickrtotal + "<br>");
    in the calling program to test if the variables have been retrieved correctly, but this fails with flickrtotal as an undefined variable.

    As I said in the original post, I am a php novice, so can't see what is going wrong, and particularly why the page called should work by itself but not when called via Ajax.

    Obviously I am not yet at the point of passing the tags into the page I am calling, this will be the next hurdle once I get this current issue sorted.

    Any advice would be much appreciated.
    Last edited by pavinder; 05-08-2006 at 05:14 PM.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    When you use Ajax to call PHP you are not calling for a full web page but rather just for the content that Javascript will need to update the existing page.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    115
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks Felgall.

    I have moved all the php-javascript assignments into the page that is called (no longer done after returning back to the calling page), and that is now not a full web page but just a php routine. But I am still unable to access these variables from the original calling page after returning.

    So for example the called page has:
    echo "flickrtotal = $flickrtotal;\n";

    but trying to access the variable flickrtotal after returning to the calling program again results in an undefined variable.

    I thought I should perhaps make these variables global in the standard way by declaring them in the head of the calling page BEFORE calling the php:

    (i.e. var flickrtotal=0; var maxarray=new Array(); )

    but if I do so then for some reason it tells me that loadpages (one of the Ajax routines, which was working perfectly before and occurs earlier on the page) is now not defined! wtf?? I therefore removed these lines again, but still think that the problem could be that the variables may only exist as local within the called routine.

    So the current calling page is:
    http://www.moonmilk.org/worlds/testajax2.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    
    <title>Ajax test page</title>
    
    
    <script type="text/javascript">
    
    
    
    /***********************************************
    
    * Dynamic Ajax Content- � Dynamic Drive DHTML code library (www.dynamicdrive.com)
    
    * This notice MUST stay intact for legal use
    
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    
    ***********************************************/
    
    
    
    var loadedobjects=""
    
    var rootdomain="http://"+window.location.hostname
    
    
    
    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)
    
    }
    
    page_request.open('GET', url, 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
    
    }
    
    }
    
    }
    
    //---------------------------------------
    //---------------------------------------
    //---------------------------------------
    function makeimg()
    {
    
    ajaxpage('testajaxphp.php', 'phpbody');
    
    document.write("<p>now back in javascript<br>the total found is " + flickrtotal + "<br>");
    
    for (i=1; i < maxarray.length; i++) 
     {      
     document.write("maxarray[" + i + "] is "+maxarray[i]+"<br>");
     }
    document.write("</p>");
    
    htmlstring=""; 
    
    for (i=1; i < maxarray.length; i++) 
     {      
     htmlstring=htmlstring+'<img src="'+maxarray[i]+'" width="45" height="30">';
     }
    htmlstring=htmlstring+'<br><br>';
    document.write(htmlstring);
    
    } 
    
    
    //---------------------------------------
    -->
    
    </script>
    
    
    
    <style type="text/css">
    
    
    #phpbody        
    	{
    	background-color: transparent;
    	text-align: left;
    	position: absolute;
    	top: 60px;
    	left: 10px;
    	height: 100%;
    	width: 600px;
    	visibility: visible;
    	display: block;
    	border: 1px solid red;
    	}
    	
    
    </style>
    
    
    </head>
    
    
    
    <body>
    
    
    
    <form action="" name="imgform" method="post" onSubmit="makeimg();return false;">
    
    <input type=text name="flickrtags" size=20 maxlength=100 value="enter tags">
    
    
    <a href="#" onClick="makeimg();">go go go</a>
    
    
    </form>
    
    <div id="phpbody">
    here is the body
    </div>
    
    
    
    </body>
    
    
    
    </html>
    The current called page is:
    http://www.moonmilk.org/worlds/testajaxphp.php
    Code:
    <?php
    require_once("phpFlickr/phpFlickr.php");
    
    $maximgs = 11;
    $tag1="open";
    $tag2="blue";
    
    // Create new phpFlickr object
    $f = new phpFlickr("fbc2211a8778e91cd26d3de027eac206");
    
    echo "<p>photos tagged with \"$tag1 $tag2\" with total of $maximgs<br>\n";
    
    // Search by tags
    $photos = $f->photos_search(array("tags"=>"$tag1 $tag2", "sort"=>"relevance", "per_page"=>$maximgs));
    
    
    echo "per page is " . $photos['perpage'] . " total is " . $photos['total'] . "<br><br>";
    
    $flickrtotal = $photos['total'];
    
    foreach ($photos['photo'] as $photo) {
        // Build image and link tags for each photo
        $val1 = "'http://www.flickr.com/photos/$photo[owner]/$photo[id]'";
        echo "<a href=http://www.flickr.com/photos/$photo[owner]/$photo[id]>";
        $val2 = '"' . $f->buildPhotoURL($photo, "thumbnail") . '"';
        echo "<img border='0' alt='$photo[title]' " . "src=" . $f->buildPhotoURL($photo, "thumbnail") . ">";        
        echo "</a>";
        
        $i++;
        $maximgsarray[$i]=$val2;
        echo "<br>$val1<br>$maximgsarray[$i]<br>";
    
    }
    
    echo "</p>\n";
    echo "length of the array is " . count($maximgsarray) . "<br>";
    
    echo "var flickrtotal=0;";
    echo "var maxarray=new Array();";
    
    echo "flickrtotal = $flickrtotal;\n";
    
    $length = count($maximgsarray);
    
    // assign the php variables to javascript variables
    for ($i = 1; $i <= $length; $i++)
      {
      echo "maxarray[$i] = $maximgsarray[$i]" . "<br>";
      }
      
    ?>
    Have I again just missed something simple here? Just something to do with the scope of the variables?

    Again, thanks in advance for any advice.
    Last edited by pavinder; 05-09-2006 at 04:07 AM.

  • #6
    Regular Coder
    Join Date
    Mar 2005
    Posts
    115
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm now trying to find alternatives to Ajax, which is causing headaches - seems so close to a solution but no idea how to proceed.

    Just a new thought - is it "straightforward" (i.e. simple for a novice php-er) to save different variables in a file and then read them back from another page?
    For example I'll need to save the values of various flags, user-selected fields, image data (of variable length) from the php search etc, in a format that's easily retrievable.

    Would this be an easier solution than trying to get Ajax to work?

    Thanks....

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Have you tried a simple PHP method? You can store values (flags, etc) from Javascript in hidden form elements, and submit the form with an action of

    action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post"

    which will reload the same page, with all those hidden form elements (and visible form elements too) now loaded to the $_POST variable as an associative array. You can then reload the form values needed to keep the page "persistent", and do any processing of those variables in PHP or Javascript, whatever is necessary.

  • #8
    Regular Coder
    Join Date
    Mar 2005
    Posts
    115
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I cannot get this method to work either...I am trying to make a very simple example of what you suggest, but the following code tells me :
    "makeimg is not defined":
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>hidden test page</title>
    
    
    <script type="text/javascript">
    
    function makeimg()
    {
    
    var newvar1="thenew1";
    var newvar2="thenew2";
    var newvar3=789;
    
    document.imgform.submit();
    
    <?php
    
    echo "<br>in php after the submit<br>";
    echo "<br>the post variable value is: $_post<br>";
    echo "<br>thats all<br>";
    
    ?>
    
    document.write("<p>now back in javascript in original page</p>");
    
    } 
    
    </script>
    
    <style type="text/css">
    
    #phpbody        
    {
    background-color: transparent;
    text-align: left;
    position: absolute;
    top: 100px;
    left: 10px;
    height: 100%;
    width: 600px;
    visibility: visible;
    display: block;
    border: 1px solid red;
    }
    
    </style>
    
    </head>
    
    <body>
    
    <form name="imgform" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
    
    <input type=text name="tags1" size="20" maxlength="100" value="enter tags1">
    <input type=text name="tags2" size="20" maxlength="100" value="enter tags2">
    <input type=text name="tags3" size="20" maxlength="100" value="enter tags3">
    <input type=hidden name="tags3" size="20" maxlength="100" value="hidden tags3">
    
    <a href="#" onClick="makeimg();">go go go</a>
    
    </form>
    
    <div id="phpbody">
    body area
    </div>
    
    </body>
    
    </html>
    and if I put javascript comment tags inside the javascript tags:
    <script type="text/javascript">
    <!--
    .
    .
    // -->
    </script>

    I get an "unexpected end of XML source" error when it hits the "// -->"! wtf?

    I am also concerned about using this method in the first place. All my "post-php" processing code is in the same page as the php, so when I first load the page will the php be generated in a way that creates errors (as the values that it needs have not yet been entered by the user)? This of course was the whole reason I was trying to get ajax to work in the first place and keep the php seperate. Is this concern valid?

    Back on the original method of using ajax, I have found a way to pass the entered tags into php now, and been trying yet another method of getting my php variables back to javascript, by using echo statements to write them in the form:
    ['file1.jpg','file2.jpg','file3.jpg'.....] in the php, and then using:
    var img_array = eval(page_request.responseText); back in the javascript calling routine.
    Code:
    echo "[";
    for ($i = 1; $i <= $length; $i++)
    if ($i > 1) {
       echo ",'" . $maximgsarray[$i] . "'";
    }
    else {
       echo "'" . $maximgsarray[$i] . "'";
    }
    echo "]";
    But this doesn't work either - it ends up writing the string "['file1.jpg','file2.jpg','file3.jpg'.....]" to screen rather than in a form that the page_request can retrieve them.

    These pages are at:
    http://www.moonmilk.org/worlds/testajax4.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    
    <title>Ajax test page</title>
    
    
    <script type="text/javascript">
    
    
    /***********************************************
    
    * Dynamic Ajax Content- � Dynamic Drive DHTML code library (www.dynamicdrive.com)
    
    * This notice MUST stay intact for legal use
    
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
    
    ***********************************************/
    
    
    
    var loadedobjects=""
    
    var rootdomain="http://"+window.location.hostname
    
    
    
    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)
    
    }
    
    page_request.open('GET', url, 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
    
    }
    
    }
    
    }
    
    //---------------------------------------
    //---------------------------------------
    //---------------------------------------
    function makeimg()
    {
    
    var tags = document.forms['imgform'].flickrtags.value;
    var page_request = false;
    
    ajaxpage('testajaxphp4.php?tags=' + tags, 'phpbody');
    
    //var max_array = page_request.responseText;
    
    // document.write("<p>now back in javascript<br>the total found is " + flickrtotal + "<br>");
    
    for (i=1; i < max_array.length; i++) 
    {      
    document.write("max_array[" + i + "] is "+max_array[i]+"<br>");
    }
    document.write("</p>");
    
    htmlstring=""; 
    
    for (i=1; i < max_array.length; i++) 
    {      
    htmlstring=htmlstring+'<img src="'+max_array[i]+'" width="45" height="30">';
    }
    htmlstring=htmlstring+'<br><br>';
    document.write(htmlstring);
    
    } 
    
    
    //---------------------------------------
    -->
    
    </script>
    
    <style type="text/css">
    
    #phpbody        
    {
    background-color: transparent;
    text-align: left;
    position: absolute;
    top: 60px;
    left: 10px;
    height: 100%;
    width: 600px;
    visibility: visible;
    display: block;
    border: 1px solid red;
    }
    </style>
    </head>
    
    <body>
    
    <form action="" name="imgform" method="post" onSubmit="makeimg();return false;">
    
    <input type=text name="flickrtags" size="20" maxlength="100" value="enter tags">
    
    <a href="#" onClick="makeimg();">go go go</a>
    
    </form>
    
    <div id="phpbody">
    here is the body
    </div>
    
    </body>
    
    </html>
    and
    http://www.moonmilk.org/worlds/testajaxphp4.php
    Code:
    <?php
    require_once("phpFlickr/phpFlickr.php");
    
    $maximgs = 11;
    
    $tags = $_GET['tags'];
    echo "<p>the tags = \"$tags\" \n";
    // Create new phpFlickr object
    $f = new phpFlickr("fbc2211a8778e91cd26d3de027eac206");
    
    echo "<p>photos tagged with \"$tags\" with total of $maximgs<br>\n";
    
    // Search by tags
    $photos = $f->photos_search(array("tags"=>"$tags", "sort"=>"relevance", "per_page"=>$maximgs));
    
    
    echo "per page is " . $photos['perpage'] . " total is " . $photos['total'] . "<br><br>";
    
    $flickrtotal = $photos['total'];
    
    foreach ($photos['photo'] as $photo) {
        // Build image and link tags for each photo
        $val1 = "'http://www.flickr.com/photos/$photo[owner]/$photo[id]'";
        echo "<a href=http://www.flickr.com/photos/$photo[owner]/$photo[id]>";
    //    $val2 = '"' . $f->buildPhotoURL($photo, "thumbnail") . '"';
        $val2 = $f->buildPhotoURL($photo, "thumbnail");
        echo "<img border='0' alt='$photo[title]' " . "src=" . $f->buildPhotoURL($photo, "thumbnail") . ">";        
        echo "</a>";
        
        $i++;
        $maximgsarray[$i]=$val2;
        echo "<br>$val1<br>$maximgsarray[$i]<br>";
    
    }
    
    echo "</p>\n";
    echo "in php length of the array is " . count($maximgsarray) . "<br>";
    
    
    echo "var flickrtotal = $flickrtotal;\n";
    
    $length = count($maximgsarray);
    
    echo "[";
    for ($i = 1; $i <= $length; $i++)
    if ($i > 1) {
       echo ",'" . $maximgsarray[$i] . "'";
    }
    else {
       echo "'" . $maximgsarray[$i] . "'";
    }
    echo "]";
    
    ?>
    Any help on this one as well - I don't care HOW I get these variables back, but I have tried 4 different methods so far and still nothing is working....any specific code example comments would be most appreciated.

    Thanks
    Last edited by pavinder; 05-15-2006 at 10:48 AM.

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    On using a server-side method, there is no other way to send data to server withouth changing the session. If you want to exchange information with the server in the same session, you may use AJAX
    http://en.wikipedia.org/wiki/AJAX
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Regular Coder
    Join Date
    Mar 2005
    Posts
    115
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes, I AM using Ajax!

    The example above is my Ajax code, but it isn't working - I cannot access the values of the dynamic content loaded using Ajax.

    I am asking if anyone can see the problem with my code, and suggest how I may be able to fix it.

    I'd appreciate any help on this one.

  • #11
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #12
    Regular Coder
    Join Date
    Mar 2005
    Posts
    115
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you Kor....enough to keep me busy for a while!

  • #13
    Regular Coder
    Join Date
    Mar 2005
    Posts
    115
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Almost there.

    I am now using a REQUEST within the same page:

    http://www.moonmilk.org/flick/k03.php
    and a copy at
    http://www.moonmilk.org/flick/k03.htm (so you can see the source)

    Looks like just a small delay issue now.

    Thanks for all the help....
    Last edited by pavinder; 05-30-2006 at 04:07 AM.


  •  

    Posting Permissions

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