Select button in PHP is not working how I expected it to?

Im am working on a college assignment and have managed to get stuck again!

I have an index page, this directs to a login page (with seperate log in form), once the user has logged in with details that match in a mysql database, it then goes to a page asking them to select a module. When I select a module rather then following some php test code it goes to a log out page. I think that I have an error with the two seperate submitt buttons in each form but can not see where... I have has this solution working with out the log in stuff etc.

Again any help would be great although im feeling really embrassed about asking as this is my 3rd question.. The code is as follows
//INDEX.PHP
<?php
// Start a session
session_start();
require_once ('db_connect.inc.php');
require_once ("functions.inc.php");
?>
<html>
<head>
<title>Assessment Plans</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
  <?php
include "login.php";
?>
</body>
</html>

//LOGIN FORM PHP
<form name="loginform.php" method="post" >
  <fieldset>
   <legend>Please login:</legend>
  <dl>
    <dt>
      <label title="Username">Username:
      <input tabindex="1" accesskey="u" name="username" type="text" maxlength="50" id="username" />
      </label>
    </dt>
  </dl>
  <dl>
    <dt>
      <label title="Password">Password:
      <input tabindex="2" accesskey="p" name="password" type="password" maxlength="15" id="password" />
      </label>
    </dt>
  </dl>
  <dl>
    <dt>
      <label title="Submit">
      <input tabindex="3" accesskey="l" type="submit" name="cmdlogin" value="Login" />
      </label>
    </dt>
  </dl>
  </fieldset>
</form>

// LOGIN PHP
<?php
if (!session_is_registered('loginid') || !session_is_registered('username'))
{
   // user is not logged in.
    if (isset($_POST['cmdlogin']))
    {
        $u = strip_tags($_POST['username']);
        $p = md5(strip_tags($_POST['password']));
        //Now let us look for the user in the database.
        $query = sprintf("SELECT loginid FROM login WHERE username = '%s' AND password = '%s' LIMIT 1;",
            mysql_real_escape_string($u), mysql_real_escape_string($p));
        $result = mysql_query($query);
         if (mysql_num_rows($result) != 1)
        {
            // invalid login information
            echo "Wrong username or password!";
            //show the loginform again.
           include "loginform.php";
        } else {
            // Login was successfull
            $row = mysql_fetch_array($result);
            $_SESSION['loginid'] = $row['loginid'];
             $_SESSION['username'] = $u;
              include "module_sel.php";
                    }
    } else {
           // User is not logged in and has not pressed the login button
           // so we show him the loginform
        include "loginform.php";
    }
 
} else {
       // The user is already loggedin, so we show the userbox.
    show_userbox();
 }
?>

//module_sel.php
<?php
session_start();
show_userbox();
include "module_form.php";

// query stuff
$result = mysql_query("SELECT Student.username, Modules.module_name FROM (course_details INNER JOIN Student ON course_details.course_code = Student.course_code) INNER JOIN Modules ON course_details.course_code = Modules.course_code");
 
print "<SELECT name='module_name'>";
while ($line = mysql_fetch_object($result))
      {
            foreach ($line as $value)
                  {
                        print "<OPTION module_name='$value'";
                         
                   }
            print ">$value </OPTION>";
      
          }

if (isset($_POST['cmdmodsel']))
      {
            if( isset($_POST['module_name']) && !empty($_POST['module_name']) )
                  {
                   $_SESSION['module_name']=$_POST['module_name'];
                  }
             include "worked.php";
       }
                             
 print "</SELECT>";

?>
//module_form.php
<form name="module_form.php" method="POST" > 
  <br><br><br><br><br><br><br>
  <fieldset>
    <legend>Please Select a Module for your course:</legend>
  <dl>
       <dt>
      <label title="Submit">
       <input tabindex="1" accesskey="l" type="submit" name="cmdmodsel" value="Select">
      </label>
    </dt>
  </dl>
  </fieldset>
</form>

Its all a mess !!! sorry im teaching myself
charly_newtsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thedwillCommented:
nothing jumps out at me, but I've never seen a form name with a period in it...   Can't find any supporting docs thought...

0
rjdownCommented:
<OPTION module_name='$value'";

should that be

<OPTION value='$value'";
0
charly_newtsAuthor Commented:
Thanks I have made those changes, naming the forms the way I had was just ne trying to get this to work. With these changes it still doesnt work.

Would there be anything in this code below that would be causing the problem ?

<?php
 
function show_userbox()
{
    // retrieve the session information
    $u = $_SESSION['username'];
    $uid = $_SESSION['loginid'];
    $sel = $_SESSION['module_value'];

    // display the user box
    echo "<div id='userbox'>
      Welcome $u
      <ul>
       <li><a href='./logout.php'>Logout</a></li>
      </ul>
           </div>";
 
}
?>
This is called in the index (funtions_inc.php) .

Again thanks for your help
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

rjdownCommented:
session_is_registered cannot be used with $_SESSION and is deprecated anyway.

In login.php, change this line

if (!session_is_registered('loginid') || !session_is_registered('username')) {

to

if (!isset($_SESSION['loginid']) || !isset($_SESSION['username'])) {
0
charly_newtsAuthor Commented:
Well that all makes perfect sense, its still not doing what I want it to, I still can not get my head around everything and why the select button on the module form seems to behave as if it is the log in button.

I think it may be time to admit defeat and submit something more simple ......
0
thedwillCommented:
what are your form ACTIONS?

0
charly_newtsAuthor Commented:
the actions were orginally
<form name="login-form" id="login-form" method="post" action="<?php echo $PHP_SELF; ?>">
but then I read that teh <?php ech $PHP_SELF;?>.... would redirect the form back to the orginal form and that you didnt really need it (cant remember what site said that there has been so many) anyway due to that advice I got rid of the action.

I just cant see in my code why it is sending me to the log out command on the select button, I know it has something to do with the show userbox that is in functions_inc.php as it is that which is being shown, it is almost as if the code is still checking the username and password at that point. Sorry to be such a pain
0
charly_newtsAuthor Commented:
Ok I have been looking over and over agin and I may be closer to knowing what is happening but I dont know how to correct it...

On the login.php the final else is to determine if the user is already logged in and if the code gets there then it shows the log out bit.
I know need to find out in my code why when you click the button on the module form why it returns back to the login.php and executes that part of code and how to stop it doing that, Would I need to close the session or will that throw up more problems as in that seesion I save dat to use in other forms ??
0
john-formbyCommented:
Hi charly_newts,

Is it possible for you to post the code for the following files:

functions.inc.php
loginform.php

John
0
charly_newtsAuthor Commented:
Hi John

loginform.php

<FORM action="<?php echo basename($_SERVER['login.php']); ?>" method="post">

  <fieldset>
  <legend></legend>
  <legend></legend>
  <legend>Please login:</legend>
  <dl>
    <dt>
      <label title="Username">Username:
      <input tabindex="1" accesskey="u" name="username" type="text" maxlength="50" id="username">
      </label>
    </dt>
  </dl>
  <dl>
    <dt>
      <label title="Password">Password:
      <input tabindex="2" accesskey="p" name="password" type="password" maxlength="15" id="password" > 
      </label>
    </dt>
  </dl>
  <dl>
    <dt>
      <label title="Submit">
      <input tabindex="3" accesskey="l" type="submit" name="button_login" value="Login" > 
      </label>
    </dt>
  </dl>
  </fieldset>
</form>


functions.inc.php

</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<body>
<style type="text/css">
.style1 {
      text-align: center;
}
.style2 {
      background-color: #004687;
}
body,td,th {
      font-family: Comic Sans MS;
      font-size: 14px;
}
.style4 {
                        background-color: #029FB4;
}
.style5 {
                        font-family: Arial;
                        font-size: xx-large;
                        color: #004687;
}
</style>
</head>

<body>

<form id="form1" runat="server">

<table border="0" cellpadding="0" cellspacing="0" style="width: 1149px; height: 185px">
      <tr>
            <td valign="top" style="height: 22px" class="style4">
            <!-- MSCellType="ContentHead" -->            </td>
      </tr>
      <tr>
            <td height="96" valign="top" style="height: 29px">
            <!-- MSCellType="ContentHead" -->
            <img src="college_logo.jpg" width="275" height="81" /></td>
      </tr>
      <tr>
            <td valign="top" class="style2" style="height: 36px">
            <!-- MSCellType="ContentHead" -->            </td>
      </tr>
      <tr>
            <td valign="top" class="style1" style="height: 61px">
            <!-- MSCellType="ContentHead2" -->
            <br />
            <span class="style5"><strong>Assessment Plans</strong></span></td>
</form>
</body>
</html>
<?php
function show_userbox()
{
    // retrieve the session information
    $u = $_SESSION['username'];
    $uid = $_SESSION['loginid'];
    $sel = $_SESSION['module_name'];
    // display the user box
    echo "<div id='userbox'>
      Welcome $u
      <ul>
       <li><a href='./logout.php'>Logout</a></li>
      </ul>
           </div>";
}


?>

</body>
</html>

This is how it stands at the moment.... I have been changing code to try and fix the problem buut so far I havent got anywhere
0
Michel PlungjanIT ExpertCommented:
This looks really weird

foreach ($line as $value)
                  {
                         print "<OPTION module_name='$value'";
                         
                   }
            print  ">$value </OPTION>";
     
I would expect

foreach ($line as $value)  {
  print "<OPTION value='$value'>$value </OPTION>";
}      

0
charly_newtsAuthor Commented:
mplungjain

that part of the code is working fine, and is what I had been advised to use in a previous question, I have in fact tested the page by using it without the login parts. The problem is with the buttons after logging in the user is directed to a page that shows a list of modules in alistbox, they should then be able to select a module,this will then be passed on to a new form (im yet to design due to this problem), however at present it just goes straight to showing the userbox in functions.inc.php and the logout script. As far as I can see when this select button is pressed the code is jumping back to the login.php and to the last else statement (the one that shows if the user is already logged in)

I hope this makes sense
0
Michel PlungjanIT ExpertCommented:
Well it is not valid html so perhaps you need to change it anyway
0
charly_newtsAuthor Commented:
maybe im getting really confused but that is what I thought was PHP and there isnt any html  in that particular part of the code but this could be where my lack of understanding is letting me down
0
Dave BaldwinFixer of ProblemsCommented:
It appears to me that you have a number of things that are incomplete or out of order.  If you would post the separate files in separate 'code' attachments, it would be clearer what you are doing.
Code goes here....

Open in new window

0
Dave BaldwinFixer of ProblemsCommented:
PHP generates HTML for the browser to display.
0
Michel PlungjanIT ExpertCommented:
and there is an include in the select too
include "worked.php";

which I am also curious to see. So a "view-source" would be great


print "<SELECT name='module_name'>";
.
.
.

if (isset($_POST['cmdmodsel']))
      {
            if( isset($_POST['module_name']) && !empty($_POST['module_name']) )
                  {
                   $_SESSION['module_name']=$_POST['module_name'];
                  }
             include "worked.php";
       }
                             
 print "</SELECT>"; 

Open in new window

0
charly_newtsAuthor Commented:
OK I think alll have been added... I have not put the db connect as that has my username and password for mysql database.... for reference the website is

http://titan.citybathcoll.ac.uk/LocalUser/NEW05095840Z/

the username and password is simply student and student, it is a college assignment also please not at present the query to show moudles puts all usernames in I have had it working where it only shows the ones for the course that the user is enrolled on (its all fictional data).
functions.inc.php
index.php
login.php
loginform.php
logout.php
module-form.php
module-sel.php
index-test.php
0
john-formbyCommented:
Ok,

On loginform.php, change this line:

<input tabindex="3" accesskey="l" type="submit" name="button_login" value="Login" />

to:

<input tabindex="3" accesskey="l" type="submit" name="cmdlogin" value="Login" />

Hope this helps,

John
0
charly_newtsAuthor Commented:
sorry just realised that I may have posted those wrongly...its becoming a very long day
0
charly_newtsAuthor Commented:
Hi  John
That helps but it was orginally that, im afraid there have been so many changes that its all mixed up I have changed it so that they both match but its still not working
0
john-formbyCommented:
What is the code you currently have?  Is that what you posted in your last post?
0
charly_newtsAuthor Commented:
it was all the files i uploaded, they are the that are uploaded on the college server, with the change that you just suggested, I am now not going to make anymore changes as running around in circles and probable confusing everyone who is so kindly helping (i expect im very close to being booted out of here)
0
john-formbyCommented:
I will have a look at the files now for you :-)
0
john-formbyCommented:
I have just downloaded all the files you posted and I can log in fine.

What are you getting when you try to login?  Do you get an error message?
0
Dave BaldwinFixer of ProblemsCommented:
Your login query doesn't work as written.  Try the code below.  'sprintf' and 'mysql_real_escape_string' aren't doing anything for you.  And the extra punctuation is probably messing things up too.  All the '$_POST' variables must match the ones in your form.

The functions.inc.php file has two </body></html> tags.
$u = trim(strip_tags(stripslashes($_POST['username'])));
$p = md5(trim(strip_tags($_POST['password'])));
//Now let us look for the user in the database.
$query = "SELECT loginid FROM login WHERE username = '$u' AND password = '$p' LIMIT 1";
$result = mysql_query($query);

Open in new window

0
charly_newtsAuthor Commented:
Hi

Those changes have been made, log in is working fine, its just that when I select a module from the list it just logs me out I want it to store the module selected, in the code it will then go to worked.php this is just a line of code to print to screen what the user has selected, if i can get that to work then I can move on and stop being a monumental pain in the rear
0
john-formbyCommented:
Hi,

Please try adding:

session_start();

at the top of the index.php page.

Hope this helps,

John
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arnoldCommented:
I think your issue is that you have multiple <body></body> you have multiple html pages within the same "browser page".
It should be a single HTML page.

i.e. you have your functions.inc.php contain a a full HTML page.
you have:
</body>
</html>
<!doctype>
<html>
<body>
<body>
<form>
<table>
</form>
</body>
</html>
<!doctype>
<html>
<body>
<div>
<form>
<input submit>
</form>
<select>..</select>
</body>
</html>

All in one single page.

See errors in the link: http://validator.w3.org/check?uri=http%3A%2F%2Ftitan.citybathcoll.ac.uk%2FLocalUser%2FNEW05095840Z%2F&charset=%28detect+automatically%29&doctype=Inline&group=0

The form action for the selection of the module is not define and presumably is sent to itself.
Your select name=module_name is outside the <form></form>  block.




Instead of
<form action...
<select ..>
<input submit>
</form>
You have
<form ..>
input submit>
</form>
<select>

Open in new window

0
charly_newtsAuthor Commented:
Thanks John, and thanks Arnold I have been to that link, its going to take me while to work through it all but lets keep our fingers crossed, I will comment back either way again a big Thanks im feeling a bit more confident now
0
Michel PlungjanIT ExpertCommented:
You select is in the wrong place and has the wrong html

<SELECT name='module_name'><OPTION name='admin'<OPTION name='Research Project'>Research Project </OPTION>

should be

<SELECT name='module_name'><OPTION VALUE='admin'>admin</OPTION><OPTION VALUE ='Research Project'>Research Project </OPTION>
 
and so on
0
Bernard S.CTOCommented:
Just browsing thru the thread, it seems that you somehow forgot, at least for some time, several basic elements:
1 - Forms: you need to have ACTION defined and as was pointed it seems preferrable not to have dots on names.

2 - HTML structure:
a - be careful not to have any blank space or empty line before the first <html ou doctype tag, and of course no blank space or empty line either before your <?php. So presumably your codes should start by
<?php
sesion_start();
...
?><html

b - your html code must be clean, ie
<html>
<head>
...
</head>
<body>
...
<form ACTION=...
..
</form>
....
</body>
</html>
0
charly_newtsAuthor Commented:
Thanks everyone, although fibo its not a case of forgetting its just that I have no knowledge of php, we havent been taught any in fact the only bit of code we were given was incorrect and wouldnt work.

I will look closely at all comments given and fingers crossed i may just scrape a pass
0
Bernard S.CTOCommented:
B-) this seemed visible. But don't worry, we have all been newbies, and are still in the new domains we are all discovering.
0
Dave BaldwinFixer of ProblemsCommented:
What fibo said...  always looking for something to be a newbie in.
0
Michel PlungjanIT ExpertCommented:
Erm - Surely the answer you accepted was not the whole story here was it?
0
charly_newtsAuthor Commented:
Nope but I had that particular issue resolved at college and then hit another problem so had to scrap it all and start again.

All help was great but im sure you must admit that the code was all wrong, very messy and far too many issues with it.

Again thank you all for putting up with me on here
0
Michel PlungjanIT ExpertCommented:
Ok. Next time please notice the "SPLIT" button or request a deletion if the question is not answered
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.