...

View Full Version : simple script request



ddemaree
04-25-2008, 07:12 AM
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

Kor
04-25-2008, 10:13 AM
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?)?

ddemaree
04-25-2008, 05:48 PM
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


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


?>

Kor
04-25-2008, 07:11 PM
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?

ddemaree
04-25-2008, 07:25 PM
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?

Kor
04-25-2008, 08:00 PM
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.

mjlorbet
04-25-2008, 08:04 PM
already suggested that in the other thread, but glad to see i'm remembered ;)

ddemaree
04-25-2008, 08:08 PM
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?

mjlorbet
04-25-2008, 08:23 PM
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


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


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.


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum