[SOLVED] $_POST - Why won't this work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

[SOLVED] $_POST - Why won't this work

Post by melindaSA »

What I am doing is pulling open positions from a mysql database according to a login level.

If I do the following, it works just fine:

Code: Select all

<?php
$conn = db_connect();
function poslist($depID){

        if ((isset($_SESSION['level'])) && ($_SESSION['level']==2)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (39,28,38,37,58)");
                        }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==3)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (29,17,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==4)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID (18,58)");
                        }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==5)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID (32,58)");
                     }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==6)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='15'");
                      }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==7)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='20'");
                      }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==8)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (2,7,54,55,56,57,58)");
                      }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==9)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (3,10,40,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==10)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (11,58,14)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==11)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (25,4,54,55,56,57,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==12)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (26,41,58,42)");
          }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==13)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='27'");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==14)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='43'");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==15)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (8,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==16)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (19,58)");
                 }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==17)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (44,45)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==18)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (46,58,54)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==19)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (30,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==20)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (24,31,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==21)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (47,5,6,54,55,56,57,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==22)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (23,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==23)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (48,58,49)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==24)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (22,33,34,35,36,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==25)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (21,50,51,52,54,55,56,57,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==26)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='9'");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==27)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (53,16,54,55,56,57,58)");
             }
    echo "<select name="depID">";
    while(list($title, $depID)=mysql_fetch_array($sql)){
        $title = stripslashes($title);
        echo "<option value="$depID"";
        echo ">$title";
    }
    echo "</option></select>";

}
// The Form:

echo "<form method="post" action="page1.php?depID=$depID" select name="title">";
echo "<font face="Arial" size="2">Select Open Position: </font>";
poslist($title);
echo "<input type="Submit" Value="Select">
</form>";

//end added
?>

Code: Select all

<?php
session_start();
include("assets/login.php");
require_once('../../HRjobs/positions_inc_fns.php');

$conn = db_connect();
print_r($_SESSION);
print_r($_POST);

$query = "SELECT * FROM application WHERE position_type = $depID ORDER by appID DESC";

$result = mysql_query($query)
        or die ("My SQL Error: <b>".mysql_error());

echo "<table>";
echo "<tr bgcolor="#A6B1C6" align="left">";
echo "<td align="center" width="220"><font face="Verdana" size="1"><b>Applicant Name</b></font></td>";
echo "<td align="center"width="240"><font face="Verdana" size="1"><b>Position Applied</b></font></td>";
echo "<td align="center"width="100"><font face="Verdana" size="1"><b>Application</b></font></td>";
echo "<td align="center" width="100"><font face="Verdana" size="1"><b>Date</b></font></td>";
echo "<td align="center"width="100"><font face="Verdana" size="1"><b>Resume</b></font></td>";
echo "</tr><td height="20">&nbsp;</td>";

while ( $row = mysql_fetch_array($result))
{
   extract($row);
   echo "<tr>\n";
   echo "<td valign="top" width="220"><font face="Verdana" size="1">$first_name $last_name</font><br></td>";
   echo "<td valign="top" width="240"><font face="Verdana" size="1">$position_apply</font><br></td>";
   echo "<td align="center" valign="top" width="100"><font face="Verdana" size="1"><a href="../show_app.php?appID=$appID" target="_blank">view</a></font><br></td>";
   echo "<td align="center" valign="top" width="100"><font face="Verdana" size="1">$date</font><br></td>";
      if(file_exists("../resume/".$first_name.$last_name.".doc")){
      echo "<td valign="top" align="center" width="100"><font face="Verdana" size="1"><a href="../resume/".$first_name.$last_name.".doc">Yes</a></font><br></td>";
      } else {
      echo "<td valign="top" align="center" width="100"><font face="Verdana" size="1">No</font><br></td>";
      }
}
   echo "</tr></table>";
?>
If I change the query by $title instead of $depID, I get no records returned:

Code: Select all

<?php
$conn = db_connect();

function poslist($title){
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==2)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (39,28,38,37,58)");
                        }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==3)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (29,17,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==4)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID (18,58)");
                        }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==5)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID (32,58)");
                     }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==6)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='15'");
                      }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==7)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='20'");
                      }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==8)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (2,7,54,55,56,57,58)");
                      }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==9)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (3,10,40,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==10)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (11,58,14)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==11)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (25,4,54,55,56,57,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==12)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (26,41,58,42)");
          }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==13)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='27'");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==14)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='43'");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==15)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (8,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==16)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (19,58)");
                 }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==17)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (44,45)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==18)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (46,58,54)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==19)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (30,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==20)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (24,31,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==21)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (47,5,6,54,55,56,57,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==22)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (23,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==23)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (48,58,49)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==24)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (22,33,34,35,36,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==25)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (21,50,51,52,54,55,56,57,58)");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==26)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID='9'");
                       }
        if ((isset($_SESSION['level'])) && ($_SESSION['level']==27)) {
        $sql = mysql_query("SELECT distinct title, depID FROM positions WHERE depID IN (53,16,54,55,56,57,58)");
             }

    echo "<select name="title">";
    while(list($title, $depID)=mysql_fetch_array($sql)){
        $title = stripslashes($title);
        echo "<option value="$title"";
echo ">$title";
    }
    echo "</option></select>";

}
// The Form:


echo "<form method="post" action="page1.php?title=$title" select name="title">";
echo "<font face="Arial" size="2">Select Open Position: </font>";

poslist($title);
echo "<input type="Submit" Value="Select">
</form>";

?>

Code: Select all

<?php
session_start();
include("assets/login.php");
require_once('../../HRjobs/positions_inc_fns.php');

$conn = db_connect();

print_r($_SESSION);
print_r($_POST);

$query = "SELECT * FROM application WHERE position_apply = '. $title .' ORDER by appID DESC";
$result = mysql_query($query)
        or die ("My SQL Error: <b>".mysql_error());

echo "<table>";
echo "<tr bgcolor="#A6B1C6" align="left">";
echo "<td align="center" width="220"><font face="Verdana" size="1"><b>Applicant Name</b></font></td>";
echo "<td align="center"width="240"><font face="Verdana" size="1"><b>Position Applied</b></font></td>";
echo "<td align="center"width="100"><font face="Verdana" size="1"><b>Application</b></font></td>";
echo "<td align="center" width="100"><font face="Verdana" size="1"><b>Date</b></font></td>";
echo "<td align="center"width="100"><font face="Verdana" size="1"><b>Resume</b></font></td>";
echo "</tr><td height="20">&nbsp;</td>";

while ( $row = mysql_fetch_array($result))
{
   extract($row);
   echo "<tr>\n";
   echo "<td valign="top" width="220"><font face="Verdana" size="1">$first_name $last_name</font><br></td>";
   echo "<td valign="top" width="240"><font face="Verdana" size="1">$position_apply</font><br></td>";
   echo "<td align="center" valign="top" width="100"><font face="Verdana" size="1"><a href="../show_app.php?appID=$appID" target="_blank">view</a></font><br></td>";
   echo "<td align="center" valign="top" width="100"><font face="Verdana" size="1">$date</font><br></td>";
      if(file_exists("../resume/".$first_name.$last_name.".doc")){
      echo "<td valign="top" align="center" width="100"><font face="Verdana" size="1"><a href="../resume/".$first_name.$last_name.".doc">Yes</a></font><br></td>";
      } else {
      echo "<td valign="top" align="center" width="100"><font face="Verdana" size="1">No</font><br></td>";
      }
}
   echo "</tr></table>";

?>
The $title array is been passed to the next page! Not sure why this will not work.

Please HELP!!
Last edited by melindaSA on Tue Jun 15, 2004 1:50 pm, edited 1 time in total.
Elessar
Forum Newbie
Posts: 15
Joined: Tue Jun 15, 2004 11:17 am

Post by Elessar »

You're missing the session_start() instruction in your first script.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

It is there, just did not include in sample, the -- print_r($_SESSION); print_r($_POST); --
test works, as it posts to the result to the page -- Array ( [level] => 9 ) Array ( [title] => Physical Therapist ) --
Elessar
Forum Newbie
Posts: 15
Joined: Tue Jun 15, 2004 11:17 am

Post by Elessar »

Well, if I understand correctly, then my best guess is that you're not setting the $title array as a session variable. Check it.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

It is set, with the $_POST....

What is really confusing me is that it pulls records using $depID but no records when using $title??
Elessar
Forum Newbie
Posts: 15
Joined: Tue Jun 15, 2004 11:17 am

Post by Elessar »

Hold on a second, with the $_POST?

Code: Select all

echo "<form method="post" action="page1.php?title=$title" select name="title">"; 
echo "<font face="Arial" size="2">Select Open Position: </font>";
You're passing the title variable as a GET variable. (page1.php?title=$title)
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

Sorry, I thought that I was passing the variable via POST! How do I pass using POST?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

melindaSA wrote:Sorry, I thought that I was passing the variable via POST! How do I pass using POST?
http://wiki.devnetwork.net/index.php/FormsTutorial
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

I tested using:

Code: Select all

<?php
if (!empty($_POST)) { // or $_GET
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
    }
?>
And got the following result:

Code: Select all

Array
(
    &#1111;title] =&gt; Rehab Aide/Athletic Trainer
)
So this means that the title is been passed? right?
Elessar
Forum Newbie
Posts: 15
Joined: Tue Jun 15, 2004 11:17 am

Post by Elessar »

Try replacing:

Code: Select all

echo "<form method="post" action="page1.php?title=$title" select name="title">";
by

Code: Select all

echo "<form method="post" action="page1.php">\n<select name="title">";
First thing I just noticed is that you have the select inside the form tag. Also, since you already have the title variable inside your form (<select name=\"title\">), it'll pass as a POST variable, you don't need to specify the title variable again in the URL, because the effect of that is to pass the variable as a GET variable.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

OK, that makes sense - thanks...but my query still has no results
Elessar
Forum Newbie
Posts: 15
Joined: Tue Jun 15, 2004 11:17 am

Post by Elessar »

replace

Code: Select all

$query = "SELECT * FROM application WHERE position_apply = '. $title .' ORDER by appID DESC";
by

Code: Select all

$query = "SELECT * FROM application WHERE position_apply = '$title' ORDER by appID DESC";
or by

Code: Select all

$query = "SELECT * FROM application WHERE position_apply LIKE '%$title%' ORDER by appID DESC";
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

That works!!!

Code: Select all

$query = "SELECT * FROM application WHERE position_apply LIKE '%$title%' ORDER by appID DESC";
Thank you so much!!
?>
Elessar
Forum Newbie
Posts: 15
Joined: Tue Jun 15, 2004 11:17 am

Post by Elessar »

My pleasure. You were using the 'equal' operator, and since you enclosed the $title variable in dots, and the operator returns true only when there's an exact match, the query returned nothing at all. Wish I had seen that earlier.
Post Reply