Need to read file with PHP and pass info to Javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hiewall
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2004 11:02 pm

Need to read file with PHP and pass info to Javascript

Post 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.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
hiewall
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2004 11:02 pm

Post 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 !!!!!!
hiewall
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2004 11:02 pm

Post 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";

}

}

}
?>
Last edited by hiewall on Wed Mar 10, 2004 7:25 am, edited 4 times in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
hiewall
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2004 11:02 pm

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Yeah, also check the JavaScript Central for JavaScript References and further info.

Scorphus.
Post Reply