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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple script request

    i posted earlier about a project i'm working on and i'd like to give a thanks to mjlorbet of these forums for his help but i've just got impatient. i just need a script that will load a random page in a selected frame using a text file for url's
    if someone could write up a script where the only thing i have to do is replace a couple values (like the web address) id really appreciate it
    here some details to help you out

    frames will be named master and slave. master being the main frame where the js code will be and slave being the target for the redirect url

    redirect url will be "http://homepage.com/profile/?id=XXXXXX" with XXXXXX being random values from a text file which will be named id.txt (always 6 digits)

    no redirect delay

    select XXXXXX from a line number in id.txt file

    example:

    254698
    257961
    349857
    273549

    if the script selected line 4 the url would be "http://homepage.com/profile/?id=349857"

    i know this is alot to ask but if theres anyone whos good at javascript and has a few minutes i'd appreciate the help

  • #2
    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
    why a text file and not a js file?

    Anyway, how do you do to inport/load your txt file into the HTML document? Under which aspect (a string?)?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    why a text file and not a js file?

    Anyway, how do you do to inport/load your txt file into the HTML document? Under which aspect (a string?)?
    i could use a js files too, basically i just dont want to have to use sql or any other databases. the exact way the id's will be recorded is

    584967%20
    267954%20
    597682%20
    329765%20
    875942%20

    and so forth. if your wondering i use the %20 as a separater so my php file searches for the id properly because i was having problems before with duplicate entries. i can use anything as a separater but i found if i use %20 if it accidently runs through the entire line and includes it in the url it still loads the page properly (in IE and FF anyway)

    i guess you'd consider that strings, they aren't variables so i think that only leaves strings. i just dont want to have to manually update a js file everytime a name is added. if it would be easier the write directly to the js file instead of reading an external file i can do that too as long it still didnt allow duplicates. heres the snippet from my php file that checks the user variable for existence then writes it.

    PHP Code:
    <?php


    function registerUser($user){
        
    $errorText '';
        if (
    strlen($user) != 6$errorText "Id must be six digits long.";

        
        
        
    // Check user existance    
        
    $pfile fopen("id.txt","a+");
        
    rewind($pfile);

        while (!
    feof($pfile)) {
            
    $line fgets($pfile);
            
    $tmp explode("%20"$line);
            if (
    $tmp[0] == $user) {
                
    $errorText "That user id is already registered!";

                            break;
            }
        }
        
        
    // If everything is OK -> store user data
        
    if ($errorText == ''){
            
            
            
    fwrite($pfile"\r\n$user%20");
        }
        
        
    fclose($pfile);
        
        
        return 
    $errorText;
    }


    ?>

  • #4
    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
    I would have used AJAX like that: send the id input to the php application, and wait for a response (echo). If the response is not an error text, give, via javascript, a new location href to the document.

    Do you know how to write an AJAX request?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    I would have used AJAX like that: send the id input to the php application, and wait for a response (echo). If the response is not an error text, give, via javascript, a new location href to the document.

    Do you know how to write an AJAX request?
    i'm not familiar with ajax, i think it has to do with js and xml but im not sure. i could research it a little though.

    so whats happening is the user input goes to php then gets passes on to ajax to be written. or does it goto ajax then php then ajax again. or just ajax then php? or am i completely off?

  • #6
    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
    AJAX is a sum of techniques. It is based on a special object (ActiveXObject() for IE5, IE6, XMLHttpRequest() for IE7 and the rest of the browsers), javascript and (not required) XML.

    That special object does a request (same as a classical submit) except that the request is made asynchronously (without changing the session) and receives an answer which is handled with javascript.

    How could work AJAX in your case?

    AJAX request -> php file ->txt file ->response from txt ->response from php-> javascript handle and display on page

    As I said, works like a submit, except that data is handled "behind the scene", without changing the session.
    Last edited by Kor; 04-25-2008 at 07:02 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    already suggested that in the other thread, but glad to see i'm remembered
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #8
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, i think i got that. so do i need to write a php script that returns all the values from the txt to the js file to be randomly selected and redirected or should the php file randomly pick a line from the txt file to be returned to js and redirected. is that what you saying?

  • #9
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    basically all your php file has to do is output your file from the harddrive exactly as is, no xml needed

    your ajax script will be loaded in the context of the window requesting the file, so the responseText property of the ajax object (or xhr, short for xml http request) would contain the contents of that file (once it has been received from the server).

    you would then use
    Code:
     
    window["links"] = this.responseText.split("\n");
    in the onreadystatechange handler of the xhr object (checking to make certain the readyState is 4 and status is either ok, cached, or ok from local (200, 304, 0 respectively))

    after that you would call a function to select the random result from the list, comparing it to the list of selected indicies
    Code:
     
    var prevSelected = new Array();
    function checkPrev(index){
    var found = false;
    for(var a = 0; a < prevSelected.length; a++)
        found = found || (prevSelected[a] == index);
    return found;
    }
    which would be called from your randomizing function to make sure the same index hasn't been selected twice.
    Code:
     
    var selectionTimeout;
    function getRandom(){
        var tmp = -1;
        if(links.length > prevSelected.length){
            var trand = parseInt(Math.random()*links.length);
            while(checkPrev(trand))
                trand = parseInt(Math.random()*links.length);
            tmp = trand;
        }
        return tmp;
    }
    function showProfile(){
    var useIndex = getRandom();
    //your code here
    }
    selectionTimeout = setTimeout(showProfile, 5000);
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender


  •  

    Posting Permissions

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