p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: My php file does not accept a value from the form object


Message #1 by "Dom Sav" <namsaasphp@h...> on Mon, 18 Feb 2002 22:05:11
I have written a code to open a text file and read upto a certain point 

and echo the lines to the browser. The text file has three poems. Starting 

from underneath the first poem there are numbers between the poems from 1 

to 3. E.g. Poem1  1  Poem2  2  and Poem3  3. The htm file has a list box 

that the user can make a choice. The value is then passed on to the php 

file - the code below - 

<FORM method=get action="formtest.php">

<SELECT name="WhichPoem">

<OPTION selected value="1">Arrow of Separation</OPTION>

<OPTION value="2">Television Skies</OPTION>

<OPTION value="3">Flux</OPTION> 

</SELECT> <BR><BR>

<INPUT type=submit value="Choose next poem"> 

</FORM>



The php code to receive the variable value ($WhichPoem) is given below:

<?php

// read upto a given number

$verses = "./mypoems.txt";

$fp = fopen($verses,"r");

//$WhichPoem = 2;

for($lnr=1; $lnr<=100; $lnr++)

{

	$lines = fgets($fp, 200);

	if($lines == $WhichPoem)

	{

	fclose($fp);

	exit;

	}

	else

	{

	echo $lines;

	}

}

?>



This is the strange part!! When I manually supply the value to $WhichPoem 

then the code works fine. Say I include $WhichPoem = 2 in the code. 

However when I get the variable to be supplied by the value chosed in the 

htm file, for some reason the php code ignores this value and echos the 

entire content of the file.

Any help would be most appreciated. I have been working night and day on 

this without any success.

Thanks



Dom Sav
Message #2 by "Nikolai Devereaux" <yomama@u...> on Mon, 18 Feb 2002 14:49:42 -0800

You're probably experiencing the same thing as many other people on the list

recently.



Check php.ini -- if register_globals is off (default in php 4.1.0+) then

you'll have to access your form variables with either the $HTTP_GET_VARS

array or the $_GET array.



Try using $HTTP_GET_VARS['WhichPoem'] instead.



one of my earlier posts:

  http://p2p.wrox.com/archive/beginning_php/2002-02/24.asp





take care,



nik









> -----Original Message-----

> From: Dom Sav [mailto:namsaasphp@h...]

> Sent: Monday, February 18, 2002 10:05 PM

> To: beginning php

> Subject: [beginning_php] My php file does not accept a value from the

> form object

>

>

> I have written a code to open a text file and read upto a certain point

> and echo the lines to the browser. The text file has three poems.

> Starting

> from underneath the first poem there are numbers between the poems from 1

> to 3. E.g. Poem1  1  Poem2  2  and Poem3  3. The htm file has a list box

> that the user can make a choice. The value is then passed on to the php

> file - the code below -

> <FORM method=get action="formtest.php">

> <SELECT name="WhichPoem">

> <OPTION selected value="1">Arrow of Separation</OPTION>

> <OPTION value="2">Television Skies</OPTION>

> <OPTION value="3">Flux</OPTION>

> </SELECT> <BR><BR>

> <INPUT type=submit value="Choose next poem">

> </FORM>

>

> The php code to receive the variable value ($WhichPoem) is given below:

> <?php

> // read upto a given number

> $verses = "./mypoems.txt";

> $fp = fopen($verses,"r");

> //$WhichPoem = 2;

> for($lnr=1; $lnr<=100; $lnr++)

> {

> 	$lines = fgets($fp, 200);

> 	if($lines == $WhichPoem)

> 	{

> 	fclose($fp);

> 	exit;

> 	}

> 	else

> 	{

> 	echo $lines;

> 	}

> }

> ?>

>

> This is the strange part!! When I manually supply the value to $WhichPoem

> then the code works fine. Say I include $WhichPoem = 2 in the code.

> However when I get the variable to be supplied by the value chosed in the

> htm file, for some reason the php code ignores this value and echos the

> entire content of the file.

> Any help would be most appreciated. I have been working night and day on

> this without any success.

> Thanks

>

> Dom Sav




> $subst('Email.Unsub').



Message #3 by "Dom Sav" <namsaasphp@h...> on Sat, 23 Feb 2002 01:11:48
Hi Nik,

Thanks for your suggestions. I am afraid I am still having problems. My 

web hoster had the register_globals turned on already.

I carried some further investigations and discovered that the loops are 

the culprit. The "for" loop does not work and the "if else" does not work 

either. If the "if" is on its own work it works but when combined 

with "else" it stops working.

Your reply would greatly enable me to progress in PHP not to mention 

sleepless nights.

Thanks



Dom Sav



> You're probably experiencing the same thing as many other people on the 

list

> recently.

> 

> Check php.ini -- if register_globals is off (default in php 4.1.0+) then

> you'll have to access your form variables with either the $HTTP_GET_VARS

> array or the $_GET array.

> 

> Try using $HTTP_GET_VARS['WhichPoem'] instead.

> 

> one of my earlier posts:

>   http://p2p.wrox.com/archive/beginning_php/2002-02/24.asp

> 

> 

> take care,

> 

> nik

> 

> 

> 

> 

> > -----Original Message-----

> > From: Dom Sav [mailto:namsaasphp@h...]

> > Sent: Monday, February 18, 2002 10:05 PM

> > To: beginning php

> > Subject: [beginning_php] My php file does not accept a value from the

> > form object

> >

> >

> > I have written a code to open a text file and read upto a certain point

> > and echo the lines to the browser. The text file has three poems.

> > Starting

> > from underneath the first poem there are numbers between the poems 

from 1

> > to 3. E.g. Poem1  1  Poem2  2  and Poem3  3. The htm file has a list 

box

> > that the user can make a choice. The value is then passed on to the php

> > file - the code below -

> > <FORM method=get action="formtest.php">

> > <SELECT name="WhichPoem">

> > <OPTION selected value="1">Arrow of Separation</OPTION>

> > <OPTION value="2">Television Skies</OPTION>

> > <OPTION value="3">Flux</OPTION>

> > </SELECT> <BR><BR>

> > <INPUT type=submit value="Choose next poem">

> > </FORM>

> >

> > The php code to receive the variable value ($WhichPoem) is given below:

> > <?php

> > // read upto a given number

> > $verses = "./mypoems.txt";

> > $fp = fopen($verses,"r");

> > //$WhichPoem = 2;

> > for($lnr=1; $lnr<=100; $lnr++)

> > {

> > 	$lines = fgets($fp, 200);

> > 	if($lines == $WhichPoem)

> > 	{

> > 	fclose($fp);

> > 	exit;

> > 	}

> > 	else

> > 	{

> > 	echo $lines;

> > 	}

> > }

> > ?>

> >

> > This is the strange part!! When I manually supply the value to 

$WhichPoem

> > then the code works fine. Say I include $WhichPoem = 2 in the code.

> > However when I get the variable to be supplied by the value chosed in 

the

> > htm file, for some reason the php code ignores this value and echos the

> > entire content of the file.

> > Any help would be most appreciated. I have been working night and day 

on

> > this without any success.

> > Thanks

> >

> > Dom Sav




> > $subst('Email.Unsub').

> 

Message #4 by "Hermawan Haryanto" <hermawan@h...> on Sat, 23 Feb 2002 08:18:07 +0700
Hi Mr. Dom Sav,



Could you repeat your question again ?, coz I'm trying to help but I don't

know your question.

Nik..... get some sleep!



Thanks



Hermawan Haryanto

hermawan@h...

----- Original Message -----

From: "Dom Sav" <namsaasphp@h...>

To: "beginning php" <beginning_php@p...>

Sent: Saturday, February 23, 2002 1:11 AM

Subject: [beginning_php] RE: My php file does not accept a value from the

form object





> Hi Nik,

> Thanks for your suggestions. I am afraid I am still having problems. My

> web hoster had the register_globals turned on already.

> I carried some further investigations and discovered that the loops are

> the culprit. The "for" loop does not work and the "if else" does not work

> either. If the "if" is on its own work it works but when combined

> with "else" it stops working.

> Your reply would greatly enable me to progress in PHP not to mention

> sleepless nights.

> Thanks

>

> Dom Sav



Message #5 by "Dom Sav" <namsaasphp@h...> on Sat, 23 Feb 2002 01:31:14
Hello Hermawan,

Heres the question.

The codes first



I have written a code to open a text file and read upto a certain point 

and echo the lines to the browser. The text file has three poems. Starting 

from underneath the first poem there are numbers between the poems from 1 

to 3. E.g. Poem1  1  Poem2  2  and Poem3  3. The htm file has a list box 

that the user can make a choice. The value is then passed on to the php 

file - the code below - 

<FORM method=get action="formtest.php">

<SELECT name="WhichPoem">

<OPTION selected value="1">Arrow of Separation</OPTION>

<OPTION value="2">Television Skies</OPTION>

<OPTION value="3">Flux</OPTION> 

</SELECT> <BR><BR>

<INPUT type=submit value="Choose next poem"> 

</FORM>



The php code to receive the variable value ($WhichPoem) is given below:

<?php

// read upto a given number

$verses = "./mypoems.txt";

$fp = fopen($verses,"r");

//$WhichPoem = 2;

for($lnr=1; $lnr<=100; $lnr++)

{

	$lines = fgets($fp, 200);

	if($lines == $WhichPoem)

	{

	fclose($fp);

	exit;

	}

	else

	{

	echo $lines;

	}

}

?>



This is the strange part!! When I manually supply the value to $WhichPoem 

then the code works fine. Say I include $WhichPoem = 2 in the code. 

However when I get the variable to be supplied by the value chosed in the 

htm file, for some reason the php code ignores this value and echos the 

entire content of the file.

Any help would be most appreciated. I have been working night and day on 

this without any success.



Nic suggested that the register_globals might have been turned off. But 

they were on. I carried some further investigations and discovered that 

the loops are the culprit. The "for" loop does not work and the "if else" 

does not work either. If the "if" is on its own work it works but when 

combined with "else" it stops working.

Your reply would greatly enable me to progress in PHP not to mention

sleepless nights.



Thanks



Dom Sav

Message #6 by "Hermawan Haryanto" <hermawan@h...> on Sat, 23 Feb 2002 08:45:02 +0700
I assum that your poem files is in this way:



Content of mypoems.txt

----

poem1

poem2

poem3

----



so to open different poem when user submit it you can use this method :



<?php

if($HTTP_GET_VARS){

  $verses = "./mypoems.txt";

  $fp = @file($verses);

  $wp = $WhichPoem-1;

  $poem = $fp[$wp];

  print $poem;

}else{

?>

<FORM method=get>

<SELECT name="WhichPoem">

<OPTION selected value="1">Arrow of Separation</OPTION>

<OPTION value="2">Television Skies</OPTION>

<OPTION value="3">Flux</OPTION>

</SELECT> <BR><BR>

<INPUT type=submit value="Choose next poem">

</FORM>

<?php } ?>



if you had a different content of mypoems.txt, you can send me, and we'll

think another method.



Thanks



Hermawan Haryanto

hermawan@h...

----- Original Message -----

From: "Dom Sav" <namsaasphp@h...>

To: "beginning php" <beginning_php@p...>

Sent: Saturday, February 23, 2002 1:31 AM

Subject: [beginning_php] Re: My php file does not accept a value from the

form object





> Hello Hermawan,

> Heres the question.

> The codes first

>

> I have written a code to open a text file and read upto a certain point

> and echo the lines to the browser. The text file has three poems. Starting

> from underneath the first poem there are numbers between the poems from 1

> to 3. E.g. Poem1  1  Poem2  2  and Poem3  3. The htm file has a list box

> that the user can make a choice. The value is then passed on to the php

> file - the code below -

> <FORM method=get action="formtest.php">

> <SELECT name="WhichPoem">

> <OPTION selected value="1">Arrow of Separation</OPTION>

> <OPTION value="2">Television Skies</OPTION>

> <OPTION value="3">Flux</OPTION>

> </SELECT> <BR><BR>

> <INPUT type=submit value="Choose next poem">

> </FORM>

>

> The php code to receive the variable value ($WhichPoem) is given below:

> <?php

> // read upto a given number

> $verses = "./mypoems.txt";

> $fp = fopen($verses,"r");

> file://$WhichPoem = 2;

> for($lnr=1; $lnr<=100; $lnr++)

> {

> $lines = fgets($fp, 200);

> if($lines == $WhichPoem)

> {

> fclose($fp);

> exit;

> }

> else

> {

> echo $lines;

> }

> }

> ?>

>

> This is the strange part!! When I manually supply the value to $WhichPoem

> then the code works fine. Say I include $WhichPoem = 2 in the code.

> However when I get the variable to be supplied by the value chosed in the

> htm file, for some reason the php code ignores this value and echos the

> entire content of the file.

> Any help would be most appreciated. I have been working night and day on

> this without any success.

>

> Nic suggested that the register_globals might have been turned off. But

> they were on. I carried some further investigations and discovered that

> the loops are the culprit. The "for" loop does not work and the "if else"

> does not work either. If the "if" is on its own work it works but when

> combined with "else" it stops working.

> Your reply would greatly enable me to progress in PHP not to mention

> sleepless nights.

>

> Thanks

>

> Dom Sav

>




$subst('Email.Unsub').

>



Message #7 by "Hermawan Haryanto" <hermawan@h...> on Sat, 23 Feb 2002 08:46:54 +0700
Forgot 1 thing,

I've try it on this address, maybe you can test it either :



http://hermawan.haryan.to/test.php





Thanks



Hermawan Haryanto

hermawan@h...



Message #8 by "Dom Sav" <namsaasphp@h...> on Sat, 23 Feb 2002 09:47:17
Thanks Hermawan,

That was great but there is a slight problem. The structure of my poem is 

as follows



Contents of mypoems.txt

-----------------

Title of Poem1

<br>

Wandered lonely as a cloud<br>

1

<br>

Title of Poem2

<br>

In my dream I see your smile<br>

2

<br>

----------------

The array to read in each line is great, but if the text file has 100s of 

lines then don't we have to count the number of lines and also find a way 

of differentiating between a poem's line and its title line? And on top of 

that we have to find out the location where the title is stored in the 

arrays memory. Therefore to simiplify matters I decided to insert numbers 

between the poems. The code should read one line at a time till it reaches 

a particular number. If "1" was chosen by the user then the code will read 

upto 1 and exit - thereby displaying only the first number.

Unfortunately this is where I face the hiccup. Maybe there is a better 

approach than this. I eagarly await your reply.

regards



Dom Sav



> Forgot 1 thing,

> I've try it on this address, maybe you can test it either :

> 

> http://hermawan.haryan.to/test.php

> 

> 

> Thanks

> 

> Hermawan Haryanto

> hermawan@h...

> 

Message #9 by "Hermawan Haryanto" <hermawan@h...> on Sat, 23 Feb 2002 21:12:04 +0700
Hi Mr. Dom Sav

Ok, now I finally see your structure/content of your poem, so what if we

deal it this way :

1. The poem text file must be change into this kind :



-POEM-1-

This is my title of Poem 1<br><br>

This is my first line of poem 1<br>

This is my second line of poem 1<br>

This is my third line of poem 1<br>

-/POEM-1-



-POEM-2-

This is my title of Poem 2<br><br>

This is my first line of poem 2<br>

This is my second line of poem 2<br>

This is my third line of poem 2<br>

-/POEM-2-



-POEM-3-

This is my title of Poem 3<br><br>

This is my first line of poem 3<br>

This is my second line of poem 3<br>

This is my third line of poem 3<br>

-/POEM-3-



You see that every poem is started with -POEM-n- and ended with -/POEM-n-

that's the mark between each poem.



2. Now the processor is like this :



<?php

if($HTTP_GET_VARS){

  $wp = $WhichPoem;

  $chop="-POEM-$wp-(.+)-/POEM-$wp-";

  $verses = "./mypoems.txt";

  $fp = join('',file($verses));

  ereg($chop, $fp, $arr);

  print $arr[1];

}else{

?>

<FORM method=get>

<SELECT name="WhichPoem">

<OPTION selected value="1">Arrow of Separation</OPTION>

<OPTION value="2">Television Skies</OPTION>

<OPTION value="3">Flux</OPTION>

</SELECT> <BR><BR>

<INPUT type=submit value="Choose next poem">

</FORM>

<?php } ?>



Btw, I've test it on http://hermawan.haryan.to/poem.php maybe you wanna give

a test on it.



Thanks



Hermawan Haryanto

hermawan@h...






  Return to Index