p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: action with $PHP_SELF and register globals off


Message #1 by "Philipp Feer" <svaziphil@s...> on Fri, 25 Oct 2002 02:35:34 +0800
Hi all
 
 I try to change a website to the new standard (register globals = off)
and have a lot of troubles doing so.
 
One of the problems I am facing is that the switch that chooses the
different function does not work anymore.
The code I have so far is:
 
--------------- (only a part shown) ----------------------------
 
 
<form name="form1" method="post" action="<? echo
"$_SERVER[PHP_SELF]?action=UnsubscribeToList" ?>">
 
 
--------------------------------
 
// *** Switching between the functions ***
 
              if(isset($_SERVER['action']) && $_SERVER['action '] !
''){
              
                          switch($_SERVER['action']) {
                                      case "DisplayMenu": 
                                                DisplayMenu();
                                      break;
                                      case "UnsubscribeToList":
                                      UnsubscribeToList();
                                      DisplayMenu();
                                      break;
                                      case "SubscribeToList":
                                      SubscribeToList();
                                      DisplayMenu();
                                      break;
                                    
                                                  default: 
                                                  DisplayMenu();
                                                  break;
 
             }// end switch 
} else {
    
    echo "NO ACTION<br>\n";   
            DisplayMenu();                  
 
}
 
--------------------------------------
 
I guess somewhere in the above part is the error but I simply cant
figure it out.
It keeps giving me the NO ACTION on the page which tells me that $action
is not set.
 
phil
                

Message #2 by Anonymous on Thu, 24 Oct 2002 21:04:54 +0200
Hi Philip,


Reason why you receive this is that when you set 'register globals = off'
within your php config file is that all global variables must be
declared.

Check your code and see if some are missing declaration.

You can correct this by simply setting some value to your
$action.

e.g.

$action = "";

I would recommend you not to use 'register globals = on' and register all
global vars. yourself.


Best Regards,


-Anonymous





At 02:35 2002-10-25 +0800, you wrote:
>Hi all
>
>  I try to change a website to the new standard (register globals = off)
>and have a lot of troubles doing so.
>
>One of the problems I am facing is that the switch that chooses the
>different function does not work anymore.
>The code I have so far is:
>
>--------------- (only a part shown) ----------------------------
>
>
><form name="form1" method="post" action="<? echo
>"$_SERVER[PHP_SELF]?action=UnsubscribeToList" ?>">
>
>
>--------------------------------
>
>// *** Switching between the functions ***
>
>               if(isset($_SERVER['action']) && $_SERVER['action '] !
>''){
>
>                           switch($_SERVER['action']) {
>                                       case "DisplayMenu":
>                                                 DisplayMenu();
>                                       break;
>                                       case "UnsubscribeToList":
>                                       UnsubscribeToList();
>                                       DisplayMenu();
>                                       break;
>                                       case "SubscribeToList":
>                                       SubscribeToList();
>                                       DisplayMenu();
>                                       break;
>
>                                                   default:
>                                                   DisplayMenu();
>                                                   break;
>
>              }// end switch
>} else {
>
>     echo "NO ACTION<br>\n";
>             DisplayMenu();
>
>}
>
>--------------------------------------
>
>I guess somewhere in the above part is the error but I simply cant
>figure it out.
>It keeps giving me the NO ACTION on the page which tells me that $action
>is not set.
>
>phil
>
>
>

Message #3 by "Nikolai Devereaux" <yomama@u...> on Thu, 24 Oct 2002 12:08:11 -0700
'action' isn't an index into the SERVER array.  I'm assuming it's an index into
your $_POST array, but can't be positive since you didn't include the rest of
your form.


If your form contained an input named "action", then substitute all your
$_SERVER['action']s with $_POST['action'].


ta
ke care,

nik

Message #4 by "Philipp Feer" <svaziphil@s...> on Fri, 25 Oct 2002 16:40:56 +0800
Yes you are right Nik, it should be $_POST['action'].
But I still can't make it work. It just does not set anything for
action.

I tried a small prg for testing:

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

<?php

function form(){
?>
<form name=3D"form1" method=3D"post" action=3D" <? echo 
"$_SERVER[PHP_SELF]"
?> ">
  <p>
    <input name=3D"name" type=3D"text" id=3D"name">
  </p>
  <p>
    <input type=3D"submit" name=3D"Submit" value=3D"Submit">
  </p>
</form>

<? }// end form()


if (!isset($_POST['name'])){
 form();
 exit;
 }
 else if (isset($_POST['action'])){
	 echo $_POST['name'];
	 echo "<P>";
	}else
	 echo "NO action";

?>
----------------------------
it keeps telling me "no action".
If I remove the "if (isset($_POST['action']" then the form works.


Would this line to the prg below (not the one above),
action=3D"<? echo "$_SERVER[PHP_SELF]?action=3DUnsubscribeToList" ?>">
make sense and should work?

Greetings, phil



Below the whole code of the actual problem:
--------------------

<?

function DisplayMenu(){
$LoginName =3D "feer"; // temporary, cause no login

//retrieving all mail lists
$newsletters =3D mysql_query("select newsletter_id, newsletterName,
newsletterInfo
							FROM
newsletter")
							or
die(mysql_error());


// retrieveing the subscriber_id from the current user
  $getSubscriber_id =3D mysql_query("select subscriber_id
							      FROM
maillist
							      WHERE
LoginName =3D '$LoginName'")
							      or
die(mysql_error());

  $subscriber_id =3D mysql_fetch_row($getSubscriber_id);

?>
<p><strong><font size=3D"4">Newsletter Subscription Menu</font></strong>
</p>
<form name=3D"form1" method=3D"post" action=3D"<? echo
"$_SERVER[PHP_SELF]?action=3DUnsubscribeToList" ?>">
  <table width=3D"770" border=3D"1" cellspacing=3D"0" cellpadding=3D"0">
    <tr>
      <td width=3D"265"><strong>Newsletter Name </strong></td>
      <td width=3D"431"><strong>Newsletter Description</strong></td>
      <td width=3D"66"><strong>Unsubscribe </strong></td>
    </tr>
    <?			=09
while ($query_data =3D mysql_fetch_row($newsletters)){
			=09
		$newsletter_id =3D $query_data[0];
		$newsletterName =3D $query_data[1];
		$newsletterInfo =3D $query_data[2];
	=09
	=09
		$subscriptionYes =3D mysql_query("select n.newsletter_id,
n.newsletterName, n.newsletterInfo
=09
FROM maillist m, newsletter n, listsubscribers l
=09
WHERE m.LoginName =3D '$LoginName'
                    				  AND m.subscriber_id =3D
l.subscriber_id
=09
AND l.newsletter_id =3D '$newsletter_id'
=09
AND l.newsletter_id =3D n.newsletter_id")
=09
or die(mysql_error());
  
	if (mysql_num_rows($subscriptionYes)) {
=09
		?>
			<tr>
			  <td><? echo $newsletterName?> <? echo "
$subscriber_id[0] " ?> </td>
			  <td> <? echo $newsletterInfo ?> </td>
			  <td><input name=3D"newsletter_id[]"
type=3D"checkbox" id=3D"Subscribe" value=3D"<? echo $newsletter_id ?>" >
				<? echo $newsletter_id ?> <input
name=3D"subscriber_id" type=3D"hidden" id=3D"subscriber_id" value=3D"<? 
echo "
$subscriber_id[0] " ?>"></td>
			</tr>
			<p>
      <?
   		}// end if
 }// end while
 ?>
  </table>
  <p><input name=3D"Unsubscribe" type=3D"submit" id=3D"Unsubscribe"
value=3D"Unsubscribe"></p>
  </form>

<p>&nbsp;</p>







<?

}/************* end function DisplayMenu() *********************/



function UnsubscribeToList(){
=09
	if ($_POST['Unsubscribe']=3D=3D "Unsubscribe") {
		if (is_array($_POST['newsletter_id'])) {
		   while (list(,$value) =3D each($_POST['newsletter_id']))
{
				 
			$unsubscribe =3D "delete from listsubscribers
							where
newsletter_id =3D $value
							AND
subscriber_id =3D '$_POST[subscriber_id]'";
=09
mysql_query($unsubscribe) or
=09
die(mysql_error());
=09

			}// end while
			 // echo " <p> press successful<p> value: $value
";
		}//end if
		else
		  echo "<p> <b> Activity NOT successful<b>";
		=09
	}//end if

}/******************* end UnsubscribeToList() *********************/


function SubscribeToList(){
=09
	if ($_POST['Subscribe'] =3D=3D "Subscribe") {
		if (is_array($_POST['newsletter_id'])) {
		   while (list(,$value) =3D each($_POST['newsletter_id']))
{
		  
		   $subscribed_already =3D mysql_query("select
newsletter_id, subscriber_id
=09
FROM listsubscribers
=09
WHERE newsletter_id =3D '$value'
=09
AND subscriber_id =3D '$_POST[subscriber_id]'")
=09
or die(mysql_error());
		  
		  
			if (!mysql_num_rows($subscribed_already) ){ 
			$subscribe =3D "insert into listsubscribers
(newsletter_id, subscriber_id)
=09
values('$value', '$_POST[subscriber_id]') ";
=09
mysql_query($subscribe) or
=09
die(mysql_error());
								//echo
"<p>the subscribing $value<br>";
				}//end if	=09
			}// end while
			  echo " <p> press successful<p> value: $value
";
		}//end if
		else
		  echo "<p> <b> Activity NOT successful<b>";
		=09
	}//end if

}/******************* end SubscribeToList() *********************/



// *** Switching between the functions ***

	  if(isset($_POST['action']) && $_POST['action '] !=3D ''){
	 
		  switch($_POST['action']) {
			  case "DisplayMenu":
				DisplayMenu();
			  break;
			  case "UnsubscribeToList":
			  UnsubscribeToList();
			  DisplayMenu();
			  break;
			  case "SubscribeToList":
			  SubscribeToList();
			  DisplayMenu();
			  break;
		=09
				  default:
				  DisplayMenu();
				  break;

  	 }// end switch
} else {
   
    echo "NO ACTION<br>";  
	DisplayMenu();                 

}	   


?>



'action' isn't an index into the SERVER array.  I'm assuming it's an
index into
your $_POST array, but can't be positive since you didn't include the
rest of
your form.


If your form contained an input named "action", then substitute all your
$_SERVER['action']s with $_POST['action'].


ta
ke care,

nik



Message #5 by "Nikolai Devereaux" <yomama@u...> on Fri, 25 Oct 2002 10:21:57 -0700
Ahhhhh see, the whole code is really what we needed.

If you look at your form, there's no input field named action at all!  It
follows that an "action" index would not be defined in the form array.

The form you submitted would only create two indexes in the $_POST array,
"name" and "Submit".  (They're the values of the input fields in the form.)


Moving along...

> Would this line to the prg below (not the one above),
> action="<? echo "$_SERVER[PHP_SELF]?action=UnsubscribeToList" ?>">
> make sense and should work?


Well, it would send your action value as a GET parameter on the URL, but I've
always been against sending URL parameters as the action to a form.  You're
mixing form methods here.  action is sent to PHP but, since it's in the URL,
is available through $_GET['action'] not $_POST['action'].

To keep things consistent, you're better off creating a hidden input inside
the form:

  <INPUT type="hidden" name="action" value="UnsubscribeToList" />

Hope this clears things up!

nik

Message #6 by "Philipp Feer" <svaziphil@s...> on Mon, 28 Oct 2002 17:16:42 +0800
Finally it worked!!
It was exactly what nik said. Thank you.

One problem down, at least 2 more to go... (in next mails)

Below the code in case someone is interested:


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

function form(){
?>
<form name=3D"form1" method=3D"post" action=3D" <? echo 
"$_SERVER[PHP_SELF]"
?> ">
  <p>
    <input name=3D"name" type=3D"text" id=3D"name">
  </p>
  <p>
    <input type=3D"submit" name=3D"Submit" value=3D"Submit">
   <INPUT type=3D"hidden" name=3D"action" value=3D"UnsubscribeToList" >

  </p>
</form>

<? }// end form()


if(isset($_POST['action']) && $_POST['action'] !=3D ''){
	 
		  switch($_POST['action']) {
			  case "DisplayMenu":
				DisplayMenu();
			  break;
			  case "UnsubscribeToList":
			  UnsubscribeToList();
			  DisplayMenu();
			  break;
			 =09
				  default:
				  DisplayMenu();
				  break;

  	 }// end switch
} else {
   
    echo "NO ACTION<br>";  
	DisplayMenu();                  
}	   



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


Ahhhhh see, the whole code is really what we needed.

If you look at your form, there's no input field named action at all!
It
follows that an "action" index would not be defined in the form array.

The form you submitted would only create two indexes in the $_POST
array,
"name" and "Submit".  (They're the values of the input fields in the
form.)


Moving along...

> Would this line to the prg below (not the one above),
> action=3D"<? echo "$_SERVER[PHP_SELF]?action=3DUnsubscribeToList" ?>">
> make sense and should work?


Well, it would send your action value as a GET parameter on the URL, but
I've
always been against sending URL parameters as the action to a form.
You're
mixing form methods here.  action is sent to PHP but, since it's in the
URL,
is available through $_GET['action'] not $_POST['action'].

To keep things consistent, you're better off creating a hidden input
inside
the form:

  <INPUT type=3D"hidden" name=3D"action" value=3D"UnsubscribeToList" />

Hope this clears things up!

nik




  Return to Index