Forms and variables

I have a problem, I have a function that draws a form, in the form the are ofcourse some options wich should set a variable $action
but it doesn't 'set'

it should do:
I press 'opret' and $action (and $_session['action'] should be set to 'opret'
I press 'rediger' and $action (and $_session['action'] should be set to 'rediger'

CODE:

<?
session_start();
regSVars();
echo $action;
debug();

loginForm();

function loginForm() {
  global $PHP_SELF; # The path and name of this file
$HTML=<<<HTML
<FORM name="Tilføj Ingrediens" action="admin.php" method="get">
<input name="action" type="button" value="opret" onclick= "this.form.action='$PHP_SELF'; this.form.submit(); ">
<input name="action" type="button" value="rediger" onclick="this.form.action='$PHP_SELF'; this.form.submit();">
<input name="action" type="button" value="Afslut" onclick="this.form.action='$PHP_SELF'; this.form.submit();">    
</FORM>
<?
endif;
HTML;
echo $HTML;
return $action;
} # End of function loginForm

function regSVars() {
     echo($r);
     echo($a);
     echo($u);
     session_register('action');
     session_register('running');
     session_register('admin');
     session_register('usn');
     global $running;
     if ($running>=1) :
          return array ($r,$a,$u);
     else:
          global $admin;
          global $usn;          
          $running = 2;
          $admin = 'no';
          $usn = 'N/A';
//          return array ($r,$a,$u);
     endif;
}

function debug() {
     echo ('<b>debug data</b><hr>');
     echo ('<font COLOR="#6699FF"> <br>Cookie data:</FONT><font COLOR="#6633FF"> ');
     print_r($_COOKIE);
     echo ('<br></font><font COLOR="#6699FF"> <br>Session data:</FONT><font COLOR="#6633FF"> ');
     print_r($_SESSION);
}
?>
<---- and if the rest should be modified ---->

function regSVars() {
     session_register('action');
     session_register('running');
     session_register('admin');
     session_register('usn');
     global $running;
     if ($running>=1) :
     else:
          global $admin;
          global $usn;          
          $running = 2;
          $admin = 'no';
          $usn = 'N/A';
     endif;
}

function debug() {
     echo ('<b>debug data</b><hr>');
     echo ('<font COLOR="#6699FF"> <br>Cookie data:</FONT><font COLOR="#6633FF"> ');
     print_r($_COOKIE);
     echo ('<br></font><font COLOR="#6699FF"> <br>Session data:</FONT><font COLOR="#6633FF"> ');
     print_r($_SESSION);
}
?>
cromozonAsked:
Who is Participating?
 
cirtapConnect With a Mentor Commented:
Hi,

this sharpens the picture a little but;-)

First of all:

I'd recommend you to activate ALL WARNINGS in the PHP.INI file on your development machine so you can find out why certain things don't work as you tend to introduce new variables and values without updating your code accordingly ;-)
To activate warnings open PHP.INI and look for the "Error handling and logging" section, there you should have something like:
  error_reporting = E_ALL
  display_errors = On

you may as well activate
  display_startup_errors = On
  log_errors = Off
  track_errors = On

This is for the development machine ONLY and NOT, NOT, NOT for the production server.
As long as you are testing code, have PHP tell you what *may* lead to errors because of an unknown/misspelled variable, missing quotes for the keys in associative arrays and more.
PHP allows a certain amount of "sloppyness", but don't rely on PHP to always have the correct assumptions about what you were actually trying to do.


1.) The suspicious } belongs to the JavaScript function, check the indention of the loginForm() code and it'll come out better.

2.) I doubt that "$PHP_SELF.php" will result in a valid filename:
   <FORM action="$PHP_SELF.php" method="POST">

3.) You want (the user) to choose from a set of OPTIONs and for a certain OPTION you want to execute some code. Ease your life and don't bother JavaScript but use what HTML has to offer to create "options" with either:

<form action="admin.php" method="POST">
<select name="action">
 <option value="opret" selected>opret
 <option value="rediger">rediger
 <option alue="afslut">Afslut>
</select>
<br><input type="submit">
</form>

or a set of RADIO buttons
<form action="admin.php" method="POST">
 <input type="radio" name="action" value="opret" selected>opret
 <input type="radio" name="action" value="rediger">rediger
 <input type="radio" name="action" alue="afslut">Afslut>
<br><input type="submit">
</form>

both would look nice if they are in the loginForm() function.


4.) SESSIONS...
Any particular reasons why you want sessions so far?
Using some sort of $action variable is okay, but it usually appearance in a FORM anyway, so you do not necessairly need it in your session, in fact it may confuse more than it helps.

The "submit" buttons I used so far do not have a "name" attribute: name it and use its name to indicate the "action status".
  <input type="submit" name="btn_login" value="Login">
or
  <input type="submit" name="btn_editaccount" value="Edit">

The button name will automagically tell your script what "action" was performed.
It's a very simple layout that wraps all the "option" and "action" specific code into include()'ed files and one of a thousand possible ways to structure functionality. And all that $_SESSION contains is the UserID/admin and maybe later: the Pizza and it's ingredients ;-)
Your individual include files may always check the $_SESSION["admin"] status and offer additional features. Of course this code snippet is not a bullet-proof solution for a workflow application.


if (!isset($_POST)) {
 // nothing posted, so display login form
  loginForm();
} else {
 // aha: a FORM was POSTed

  if (isset($_POST(['btn_login']))) {
    // user wants to login... this IS a good place for using
    // $_SESSION["UserID"] and maybe $_SESSION["admin"]
    include "loginstuff.inc";
  }

  // do we have a session?
  if (defined('SID')) { // or isset($_SESSION)
    if (isset($_POST['btn_edituser'])) {
      // the user edited her/his account
      include "accountstuff.inc";
    }

    if (isset($_POST['btn_orderpizza'])) {
      // give the user a pizza
      include "pizzaservice-stuff.inc";
    }
  } else {
  // oops: no session! maybe expired? so login again
     loginForm();
  }
}




Another little advice:
make sure your PHP configuration is equal, or as close as possible, to the one on the production server (ISP). This includes at least using the same PHP-version!
PHP 5 beta is nice to play with but useless if your ISP still has v4.1 installed.
The configuration also includes using the  same 'register_globals' setting (since PHP 4.2). Compare them with php_info() on both servers.


Have fun.
CirTap


You probably  should read "Chapter 2 - Dealing with Forms" and "Chapter 8 - Variables from outside PHP". Manual downloads: http://www.php.net/download-docs.php

I guess you're from Denmark, so you may check this site http://www.kandu.dk/dk/kat/303
0
 
VGRCommented:
just a comment : I don't like at all this writing, I find it twisted

function regSVars() {
    echo($r);
    echo($a);
    echo($u);
    session_register('action');
    session_register('running');
    session_register('admin');
    session_register('usn');
    global $running;
    if ($running>=1) :
         return array ($r,$a,$u);
    else:
         global $admin;
         global $usn;          
         $running = 2;
         $admin = 'no';
         $usn = 'N/A';
//          return array ($r,$a,$u);
    endif;
}

I would write it as

$r=array();
$a=array();
$u=array();

function regSVars() {
 GLOBAL $r, $a, $u, $running, $admin, $usn;
 if ($running>=1) return array ($r,$a,$u);
 else {
         $running = 2;
         $admin = 'no';
         $usn = 'N/A';
//          return array ($r,$a,$u);
  // NB this case doesn't return anything (1)
 } // endif
 session_register('action'); // completely useless, action is not transmitted nor declared GLOBAL, so it'll be EMPTY
 session_register('running');
 session_register('admin');
 session_register('usn');
 return FALSE; // to take care of (1)
 // NB : I think the "return array($r,$a,$u);" should be there
} // regSVars array function
 
0
 
cirtapCommented:
Sorry, but: are you sure you know what you're doing?
This code is a mess, and makes no sense at all.

You're registering variables that don't exist,
You echo out variables that come from nowhere.
$r $a $a may be a shorthand for $action, $admin $user.. whatever
echo $action;   in the beginning will raise a warning the first time the page is laoded

If you have PHP4.2 you have to use $_GET["action"] or $_POST["action"] to have the value after the form has been sent, it'll will no longer appear in the global scope starting with PHP 4.2 unless you have register_globals=on in your PHP.INI.
Check http://php.net for more on this multi-trouble issue.

The loginForm() function contains invalid code
You mixed "here-docs" syntax with echo
There's an endif; hanging around that belongs nowhere
It returns a value (famous $action) you don't evaluate

I fixed the JavaScript part so the buttons' values are written in a hidden field called "action".
You used
  this.form.action='$PHP_SELF'
which changes the action attribute of the FORM and you forgot the .value
'PHP_SELF' here makes no sense to me...

I used a function to set the hidden action. Three times the same code is worth a function :-)

> it should do:
> I press 'opret' and $action (and $_session['action'] should be set to 'opret'
> I press 'rediger' and $action (and $_session['action'] should be set to 'rediger'

Well this code "does" something.
Maybe you can give us some "hint", what task you're actually trying to accomplish.

CirTap


<?
session_start();
regSVars();

loginForm();
debug();

function loginForm() {
 global $PHP_SELF; # The path and name of this file
// get $action from $SESSION
 $action=(isset($_SESSION['action'])) ? $_SESSION['action'] : "";
// if first load: get $action, when POSTed
 $action=($action=="" && isset($_POST['action'])) ? $_POST['action'] : "";

// use here-docs the way the should
// NOTE THAT I CHANGED THE FORM'S METHOD TO   POST

echo <<<HTML
<script language="JavaScript">
<!--
function setAction(btn) {
     btn.form.elements['action'].value=btn.value
     btn.form.submit()
}
// -->
</script>
<FORM action="admin.php" method="POST">
<input type="text" name="action" value="$action">
<input type="button" value="opret"   onclick="setAction(this);">
<input type="button" value="rediger" onclick="setAction(this);">
<input type="button" value="Afslut"  onclick="setAction(this);">
</FORM>
HTML;
} # End of function loginForm

function regSVars() {
// FIRST gobal then register...
    global $running, $admin, $usn;

    if (isset($_POST['action'])) session_register($_POST['action']);

    session_register('running');
    session_register('admin');
    session_register('usn');

// maybe you should return the variables that you
// actually use: there was no $r $a $u anywhere but
// $running, $admin, $usn
    if ($running>=1) {
     // for WHAT use are they returned?? you have $_SESSION
         return array($running, $admin, $usn);
    } else {
         $running = 2;
         $admin = 'no';
         $usn = 'N/A';
         return array($running, $admin, $usn);
    }

}

function debug() {
    echo ('<b>debug data</b><hr>');
    echo ('<font COLOR="#6699FF"> <br>Cookie data:</FONT><font COLOR="#6633FF"> ');
    print_r($_COOKIE);
    echo ('<br></font><font COLOR="#6699FF"> <br>Session data:</FONT><font COLOR="#6633FF"> ');
    print_r($_SESSION);
}
?>
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
cromozonAuthor Commented:
no, im not sure what im doing ;)
anyway, I have cleaned up the code, and removed the $u,$a,$r vars, wich I should have done before posting... sorry

I have made some few changes to cirtap's code

and before code dump, little explenation:
the goal of this code is to make a forms, there have buttons I can press, runs specifik functions.

example I press 'opret' button and the page runs the opret function (wich is a new form and latter a database insert)
----------
CODE:
<?
session_start();
regSVars();
loginForm();
debug();

switch ($action) {
     case 'start':
          echo ('<br>action was set to start');
          break;
     case 'opret':
          // run the opret function (wich doesn't exists yet...
          break;

     default:
          echo ('<br>action was set to something else...');
          break;
}

function regSVars() {
     global $running, $admin, $usn, $action;
     if (isset($_POST['action'])) session_register($_POST['action']);
     session_register('action');
     session_register('running');
     session_register('admin');
     session_register('usn');
     if ($running<1) {
          $action='start';
          $running = 2;
          $admin = 'no';
          $usn = 'N/A';
     }
}

function debug() {
     echo ('<b>debug data</b><hr>');
     echo ('<font COLOR="#6699FF"> <br>Cookie data:</FONT><font COLOR="#6633FF"> ');
     print_r($_COOKIE);
     echo ('<br></font><font COLOR="#6699FF"> <br>Session data:</FONT><font COLOR="#6633FF"> ');
     print_r($_SESSION);
}

function loginForm() {
     global $PHP_SELF; # The path and name of this file
     // get $action from $SESSION
     $action=(isset($_SESSION['action'])) ? $_SESSION['action'] : "";
     // if first load: get $action, when POSTed
     $action=($action=="" && isset($_POST['action'])) ? $_POST['action'] : "";
     // use here-docs the way the should
     // NOTE THAT I CHANGED THE FORM'S METHOD TO   POST
     echo <<<HTML
     <script language="JavaScript">
     <!--
     function setAction(btn) {
         btn.form.elements['action'].value=btn.value
         btn.form.submit()
     } //think the '}' were missing
}

// -->
</script>
<FORM action="$PHP_SELF.php" method="POST">
<input type="text" name="action" value="$action">
<input type="button" value="opret"   onclick="setAction(this);">
<input type="button" value="rediger" onclick="setAction(this);">
<input type="button" value="Afslut"  onclick="setAction(this);">
</FORM>
HTML;
};
?>
0
 
cromozonAuthor Commented:
deleted this line in loginForm(). seems like the '}' wasn't missing after all

} //think the '}' were missing
0
 
cromozonAuthor Commented:
Thx for the help and the links, I have made the function work, and also learned that $_SESSION and $_REQUEST is case sensitive...
0
 
VGRCommented:
All PHP is case-sensitive...
0
All Courses

From novice to tech pro — start learning today.