Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

accessing form variables in PHP 4.0.4

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

accessing form variables in PHP 4.0.4

Postby reject » Thu Oct 03, 2002 9:23 am

I know this is a common problem. I can't get form variables. I've researched the problem and tried all of the code I came across. I've tried $_Post['Username'], $HTTP_POST_VARS['Username'], and $Username. register_globals is on. Keep in my mind that I am totally new to PHP (started today). Here's the form:

Login:
Code: Select all
<form action="login_action.php" method="post">
<table border="0">
<tr><td>Username:</td><td><input type="text" name="Username"></td></tr>
<tr><td>Password:</td><td><input type="Password" name="Password"></td></tr>
</table>
<p>
<input type="submit" value="Login">
</form>



login_action:
Code: Select all
function Database_Entries($msg) {
    echo $msg;
}

function Output_Entries() {
    /*
        Make the connection to the database. The syntax is
        odbc_connect( 'SYSTEM_DSN' , 'USER', 'PASSWORD' );
        $cnx will hold the
        pconnect is used to establish a persistent database
        connection to the Database until the procedure is completed.
    */

    $cnx = odbc_connect( 'gclntn' , '', '' );
    if (!$cnx) {
        Error_handler( "Error in odbc_connect" , $cnx );
    }

    // send a simple odbc query . returns an odbc cursor
  //  $cur= odbc_exec( $cnx, "SELECT Username,Password from tbl_admin WHERE Username='".$_POST["Username"]."' AND Password='".$_POST["Password"]."'");

$cur= odbc_exec( $cnx, "SELECT Username,Password from tbl_admin WHERE Username='".$HTTP_POST_VARS['Username']."' AND Password='".$HTTP_POST_VARS['Password']."'");

//$HTTP_POST_VARS['Username'] $HTTP_POST_VARS['Password']


    if (!$cur) {
        Error_handler( "Error in odbc_exec( no cursor returned ) " , $cnx );
    }
    $nbrow=0;   //Local variable to count number of rows

    // fetch the succesive result rows
    while( odbc_fetch_row( $cur ) ) {
        $nbrow++;
        $UsernameSQL= odbc_result( $cur, 1 ); // get the field "Username"
        $PasswordSQL= odbc_result( $cur, 2 ); // get the field "Password"
    }
   
    // close the connection. important if persistent connection are "On"
    odbc_close( $cnx);
   //echo $HTTP_POST_VARS["Username"];
   
echo $Username;
   
   phpinfo();

   if($nbrow>=1)
   {
      //Header ("Location: index.php");
      
   }
   else
   {
      //Header ("Location: login.php");
   }   
}

function Error_Handler( $msg, $cnx ) {
    echo "$msg \n";
    odbc_close( $cnx);
    exit();
}

Output_Entries();
reject
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Oct 03, 2002 8:56 am

Postby Jay » Thu Oct 03, 2002 12:04 pm

What is the actual problem you're getting? Can you actually 'see' the $_POST variables, or not? First determine that much! Once you've got that sorted, work out if your SQL syntax is OK (by printing it out). You should solve most of your problems this way!
Jay
 

Postby reject » Thu Oct 03, 2002 12:09 pm

Sorry. I should have been more clear. I meant that I can't see the variables. The SQL syntax is fine, ignore that. I have it printing the form variable, which is how I know where the prob is. Thanks.
reject
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Oct 03, 2002 8:56 am

Postby Jay » Thu Oct 03, 2002 12:15 pm

Your code is very messy, I'm having trouble deciphering it and finding out what exactly your problem is? You're having trouble seeing the form variables? Try print_r($_POST) on that page, and see if it's there!
Jay
 

Postby reject » Thu Oct 03, 2002 12:20 pm

Yeah, I have a lot of code commented out or moved because I was testing..... I'll try that method. Thanks. It's not actually my code though. It's from a tutorial.
reject
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Oct 03, 2002 8:56 am

Postby reject » Thu Oct 03, 2002 1:41 pm

I don't get anything using print_r($_POST).
reject
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Oct 03, 2002 8:56 am

Postby Jay » Thu Oct 03, 2002 11:28 pm

Try the same with print_r($HTTP_POST_VARS), and if you don't get anything there, try
Code: Select all
while(list($val,$var) = each($GLOBALS)) print "$val => $var<br>\n";

That'll print out ALL the $GLOBAL variables, see if it's there. It has to be going somewhere!
(BTW, you are putting values into the form fields when you're testing this, arn't you? It won't submit the field if there's nothing there!
Jay
 

Postby reject » Fri Oct 04, 2002 12:27 pm

Yes, I entered something. Like most programmers, I make some dumb mistakes, but not that dumb! :wink:

Yes, the variables are there when I list them with
Code: Select all
while(list($val,$var) = each($GLOBALS)) print "$val => $var<br>\n";


The output is:

Username => hjk
Password => hj
reject
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Oct 03, 2002 8:56 am

Postby Jay » Fri Oct 04, 2002 1:52 pm

Then you should be able to use them with just $Username and $Password ;)
Jay
 

Postby DoppyNL » Sat Oct 05, 2002 1:20 am

but theire not (imediatly) available in functions (wich is what you are doing).

you can do 3 things to make them available in a function:
1: pass them with the function:
Code: Select all
function Output_Entries($username);
{
blabla;
}
Output_Entries($username);

2: make them global in the function:
Code: Select all
function Output_Entries();
{
global $username;
blabla;
}

3: use the global array:
Code: Select all
function Output_Entries();
{
$GLOBALS['username'] = whatever;
}


Greetz Daan
DoppyNL
 

Postby reject » Sat Oct 05, 2002 9:07 am

Duh on my part. I am quite positive that's the problem, DoppyNL. Thanks!
reject
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Oct 03, 2002 8:56 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron