Page 1 of 1

Need to read file with PHP and pass info to Javascript

Posted: Mon Mar 08, 2004 11:02 pm
by hiewall
All I want to do is read a file :

http://www.wallysweb.com/testfile.txt

and create some kind of string or array that would have names that were parsed from this file.

The names are located on the last 4 records ---- right after "PLAYERID=" and before " ,PLAYERINFO1"

These names could have commas , spaces and any other characters. There could be as many as 300 records with names on them.

Once this string or array is created I would like to read it with Javascript and check a name entered on a form to see if it already was used.

Posted: Tue Mar 09, 2004 12:11 am
by scorphus
Hi, welcome to PHPDN.

1- What have you done so far? Can you read the file? Can you parse the file contents into an array? If there is some code post it here.

2- Do you know and understand the way JavaScript (client side) and PHP (server side) work? Read Frames, JavaScript, and PHP Overview tutorial.

To load the file contents you could use [php_man]file[/php_man]() or any other Filesystem Function and then use RegExp to catch the names and throw them into an array,

Then use a loop to write the JavaScript code to be sent to the browser where you'll tell the browser's JavaScript engine to create an array of names.

This array will be created on client side and used to check if the chosen name is already in use.

This is like PHP would send a letter to JavaScript, telling it what to do. There is no way they can talk each other on-the-fly.

Scorphus.

Posted: Tue Mar 09, 2004 9:12 pm
by hiewall
This is what I have so far --- but it does not work. Some kind of parsing error.

I don't know PHP ----- so can't figure it out.

Also --- how do I tell it to list the names on the $names array.


And ---- finally ---- how do I read this $names array in Javascript and check a name that I have in my code to see if it exists on this array.


Thanks for your help !!!!!!

Posted: Tue Mar 09, 2004 9:15 pm
by hiewall
OOPs !!!!!

Forgot the code

<?php
// Read in file
$Contents = file("http://www.poolsforfun.com/golfdata/testfile.txt","r");
$Names = array();


if (!$Contents) {
echo "<P>Couldn't open the user database.\n";
exit();

echo "<P>Database Opened.\n";

foreach($Contents as $Line)
{
// Is this a data line?
if(substr($Line,0,9) == "PLAYERID=")
{
// Get first part of string
$Line_Pieces = split(",PLAYERINFO1", $Line);
$Names[] = substr($Line_Pieces[0],9);
echo "<P>List of names created.\n";

}

}

}
?>

Posted: Tue Mar 09, 2004 9:17 pm
by tim
{echo "<P>List of names not created.\n";

whats the { for?

what line are u getting the parse error? I bet its that line

Posted: Tue Mar 09, 2004 9:43 pm
by hiewall
Thanks --- just a typo.

When i fixed that the message said that fopen needs 2 paramaters.

I added the "r" for read only and now it runs but I get nothing to tell me if it worked.

Posted: Wed Mar 10, 2004 5:25 pm
by tim
scorphus wrote:Hi, welcome to PHPDN.

1- What have you done so far? Can you read the file? Can you parse the file contents into an array? If there is some code post it here.

2- Do you know and understand the way JavaScript (client side) and PHP (server side) work? Read Frames, JavaScript, and PHP Overview tutorial.

To load the file contents you could use [php_man]file[/php_man]() or any other Filesystem Function and then use RegExp to catch the names and throw them into an array,

Then use a loop to write the JavaScript code to be sent to the browser where you'll tell the browser's JavaScript engine to create an array of names.

This array will be created on client side and used to check if the chosen name is already in use.

This is like PHP would send a letter to JavaScript, telling it what to do. There is no way they can talk each other on-the-fly.

Scorphus.
I suggest having a look at them links, read what hes saying.

All you pretty much need is there

Posted: Wed Mar 10, 2004 5:51 pm
by scorphus
Yeah, also check the JavaScript Central for JavaScript References and further info.

Scorphus.