p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Global Variables problem


Message #1 by WH Micro <wh_micro@y...> on Wed, 24 Apr 2002 10:00:46 -0700 (PDT)
Hello:

Is me once again needing your help.

I would like to know why I can't get the variable's
content with $HTTP_GET_VARS["var"] or 
$HTTP_POST_VARS["var"]. It works fine on Win running
PHPdev4 but when I upload it to the webserver it
doesn't show the var value.

register_global = on

This is what my code looks like:

print( "<br>" . "<form method='$formMethod'
action='$PHP_SELF'>" . 
"<table class='poll_table' align='center' width='100%'
cellpadding='0' cellspacing='0'><tbody>" .
"<tr><td colspan='4'><center><b>< ENCUESTA
></b></center></td></tr>" .
"<tr><td width='2%'></td><td colspan='2'><p>" .
 $row["question"] . 
"</p></td></tr>" );

for( $i = 1; $i < 5; $i++ ){
$colTitle = "choice$i"; 
print( "<input type='hidden' name='$colTitle' value='"
. $row[$colTitle] . "'>" . 
"<tr bgcolor='#c9c9c9' " . 
"<td>> "  . $row[$colTitle] . "</td>" . 
"<td><input type='radio' name='choice' 
value='$colTitle'></td>" . 
"<td width='5%'></td><tr>" );
}// end forloop

// print submit button		
	print(  "<tr align='center'>" .
			  "<td colspan='4'>" .
			  "<br><input type='submit' name='poll_submitted'
value='Votar'>" .
			  "</td></tr>" .
			  "<tr><td colspan='4'>" .
   "</tbody></table></form>" );
}	
	
	
/* POLL RESULTS SECTION
*********************************************/
if( $poll_submitted == "Votar" )
{
	if( isset( $choice ) ){
	show_poll_results( $question_id, $choice );
	}

	
	
}
/**************************************************************************
	SHOW POLL RESULTS FUNCTION
**************************************************************************/
function show_poll_results( $question, $choice ){
	
	Make_Connection();
	
	$sql = "select * from responses where question_id 
$question";
	
	$result = runQuery( $sql, "Select from responses" );
	
	 // table layout
	print( "<br>" . 
			"<table class='result_poll_table' align='center' 
width='100%' cellpadding='0' cellspacing='2'><tbody>"
.
"<trcolspan='4'><center><b>< ENCUESTA
></b></center></td></tr>" .
"<tr align='center'><td colspan='4'><p>Resultados de
Encuesta<br>" . 
"</p></td></tr>" );
	 
// since there's only one row coming back, I put them
on variables 
// and access the variables through the for loop
list( $q_id, $resp1, $resp2, $resp3, $resp4, $tot_res
) = mysql_fetch_array($result);
        
// this loop will traverse through each response or
response column
for( $i = 1; $i < 5; $i++ )
{
$temp_name = "resp" . $i; // create the column name
here
		 
// calculate to integer the percent of each response
$pct = intval( ( $$temp_name / $tot_res ) * 100 );
// calculate the width of the image used as a bar
graph,
$width = intval( $pct ); 
// print each response in a row with the image
print( "<tr>" .
"<td>" . $HTTP_POST_VARS["choice$i"] . "</td>" . 
"<td width='100'><img src='./images/rainbow.gif'
border='0' width='$width' height='15'></td>" . 
"<td>$pct%</td>" . 
"</tr>" );
}// end for loop
******************************************
		
// print the end of the table
print( "<tr align='center'>" .
  "<td colspan='4'>" .
"Total de Votos: $tot_res</td></tr>" . 
 "</tbody></table>" );

?>

I don't know if the email will come out readable but,
in case it doesn't let me know so I can attach the
actual file.

Thanks



=====
www.whmicro.com

__________________________________________________
Do You Yahoo!?
Yahoo! Games - play chess, backgammon, pool and more
http://games.yahoo.com/
Message #2 by "Duncan Hutty" <duncanh@h...> on Wed, 24 Apr 2002 14:04:36 -0400
try using $_REQUEST["foo"] 
This should work for PHP >= 4.1.0

Duncan Hutty

Message #3 by "Nikolai Devereaux" <yomama@u...> on Wed, 24 Apr 2002 11:53:55 -0700
> // print each response in a row with the image
> print( "<tr>" .
> "<td>" . $HTTP_POST_VARS["choice$i"] . "</td>" .

Do you mean "choice[$i]" here?  or are your variables named choice1 choice2
choice3 etc..?

Remember, you can always do a print_r() of your get and post arrays to see
if you're actually posting the data to the processing script, and everything
is named as you expect.  Since dout  (debug out) is a lot easier to type
than print_r(), I use this function whenever I'm developing or debugging
code:

function dout($var)
{
    echo "<pre>\n";
    print_r($var);
    echo "</pre>\n";
}


Nik


  Return to Index