Jump to content

isset($_POST)


powpow

Recommended Posts

Running into some problems with my code.  I am having the user search the Database with either a UAID or the user's full name.  It works fine when you input both the UAID and name; however, I want it to work if only one is give (i.e. just the name or just the UAID).

 

I decided to use the isset($_POST) function of php but I am not getting it to work correctly.  PHP believe it is set even though it is empty.

 

Here is my html form.

<?php
@include 'accesscontrol.php';

echo "<!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\">"
     ."<title> Hello </title>"
     ."<meta http-equiv=\"Content-Type\"
      content=\"text/html; charset=iso-8859-1\" />"
     ."<head>"
     ."<style type=\"text/css\">"
     ."h1{"
     ."border-bottom-style: double;"
     ."border-bottom-color: black;"
     ."border-bottom-width: 5px;"
     ."}"
     ."p{"
     ."background-color:#4682B4;"
     ."}"
     ."</style>"
     ."</head>"
     ."<body style=\"background-color:#B8B8B8\">"
     ."<table align=\"center\" style=\"background-color:#B8B8B8\" WIDTH=\"100%\" HEIGHT=\"100%\">"
     ."<tr valign=\"middle\">"
     ."<td align=\"center\">"
     ."<form method=\"post\" action=\"user.php\">"
     ."<table align=\"center\" cellpadding=\"10\" style=\"background-color:#5CADFF\" width=\"500\" height=\"400\">"
     ."<tr>"
     ."<th colspan=6 align=\"center\"><h1>Welcome: ".$uid."</h1></th>"
     ."</tr>"
     ."<tr>"
     ."<td colspan=3 width=40% align=center>"
     ."UAID:<br>  <input type=\"text\" size=\"8\" name=\"UAID\" /></td>"
     ."<td colspan=3 width=60% align=center>"
     ."Name (Last, First):<br><input type=\"text\" size=\"8\" name=\"last\"/>, <input type=\"text\" size=\"8\" name=\"first\" />"
     ."</tr>"
     ."<tr>"
     ."<td colspan=6 align=\"center\"><input type=\"submit\" value=\"submit\" />"
     ."</td>"
     ."</tr>"
     ."</tr>"
     ."</table>"
     ."<tr>"
     ."</tr>"
     ."</td>"
     ."</tr>"
     ."</table>"
     ."</body>"
     ."</html>";

?>

 

 

Here is the PHP:

 

<?php
@include 'accesscontrol.php';
//include 'common.php';
include 'db.php';

$fname = $_POST['first'];
$lname = $_POST['last'];


$sql = "SELECT * FROM `user` WHERE UAID='$_POST[uAID]'";
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);


if(isset($fname) && isset($lname))
{

echo "Post first $fname <br>"
     ."Post last $lname";
$firstname = $rows['firstname'];
$lastname = $rows['lastname'];

if($fname != $firstname OR  $lname != $lastname)
{
?>
<html>
<head>
<script language="JavaScript">
    alert(" Hello this is crap" ); 
    history.back(); 
</script>";
</head>
</html>
<?
exit; 

}

else
{ 
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
      \"URL\">"
     ."<html xmlns=URL>"
     ."<title> Hi </title>"
     ."<meta http-equiv=\"Content-Type\"
      content=\"text/html; charset=iso-8859-1\" />"
     ."<head>"
     ."<style type=\"text/css\">"
     ."h1{"
     ."border-bottom-style: double;"
     ."border-bottom-color: black;"
     ."border-bottom-width: 5px;"
     ."}"
     ."p{"
     ."background-color:#4682B4;"
     ."}"
     ."</style>"
     ."</head>"
     ."<body style=\"background-color:#B8B8B8\">"
     ."<table align=\"center\" style=\"background-color:#B8B8B8\" WIDTH=\"100%\" HEIGHT=\"100%\">"
     ."<tr valign=\"middle\">"
     ."<td align=\"center\">"
     ."<table align=\"center\" cellpadding=\"10\" style=\"background-color:#5CADFF\" width=\"600\" height=\"400\">"
     ."<tr>"
     ."<th colspan=6 align=\"center\"><h1>Welcome: ".$uid."</h1></th>"
     ."</tr>"
     ."<tr>"
     ."<form method=\"Post\" action=\"userview.php\">"
     ."<td colspan=3 width=40%>"
     ."UAID:  <input type=\"text\" size=\"8\" name=\"UAID\" value= ".$_POST['UAID']." /></td>"
     ."<td colspan=3 width=60%>"
     ."Name (LAST, FIRST):<input type=\"text\" size=\"8\" name=\"last\"  value= ".$_POST['last']." />,
      <input type=\"text\" size=\"8\" name=\"first\" value= ".$_POST['first']." />"
     ."</td>"
     ."</tr>"
     ."<tr>"
     ."<td colspan=3 width=40%>"
     ."Department:<input type=\"text\" size=\"8\" name=\"department\" value=" .$rows['BOG'].">"
     ."</td>"
     ."<td colspan=3 width=60%>"
     ."Employee ID:<input type=\"text\" size=\"8\" name=\"empID\" value=" .$rows['employeeID'].">"
     ."</td>"
     ."</tr>"
     ."<tr>"
     ."<td colspan=3 align=\"center\"><input type=\"submit\" value=\"View\">"
     ."</form>"
     ."</td>"
     ."<form method=\"Post\" action=\"homepage.php\">"
     ."<td colspan=3 align=\"center\"><input type=\"submit\" value=\"Back\">"
     ."</td>"
     ."</tr>"
     ."</tr>"
     ."</table>"
     ."<tr></tr></td></tr></table></body></html>";

}}
mysql_free_result($result);
?>

Link to comment
Share on other sites

my html not valid? I don't know what you mean by that, it runs. 

 

As for your second comment I wanted to make it search the database using these three parameters: UAID & name, UAID, or NAME. The problem I was getting stuck on was in my original code I did not make an if statement that changed the query with the different input.  It now works perfectly.

 

<?php
@include 'accesscontrol.php';
//include 'common.php';
include 'db.php';




if($_POST['first']=="" && $_POST['last']=="" && $_POST['UAID']=="")
{
?>
<html>
<head>
<script language="JavaScript">
    alert(" Please search by either UAID or First name and Last name." );
    history.back();
</script>";
</head>
</html>
<?
exit;
}
else{
//     ."UAID = $_POST[uAID]";
$fname = $_POST['first'];
$lname = $_POST['last'];
$uaid = $_POST['UAID'];
}

if($_POST['UAID'] == "")
{
$sql= "SELECT * FROM `user` WHERE firstname='$fname' AND lastname='$lname'";
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);
}
else
{
$sql = "SELECT * FROM `user` WHERE UAID='$_POST[uAID]'";
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);

}

if(isset($fname) && isset($lname))
{
$firstname = $rows['firstname'];
$lastname = $rows['lastname'];

if($fname != $firstname OR  $lname != $lastname)
{
?>
<html>
<head>
<script language="JavaScript">
    alert(" Hello this is crap" ); 
    history.back(); 
</script>";
</head>
</html>
<?
exit; 

}

else
{ 
echo "<!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\">"
     ."<title> Hi </title>"
     ."<meta http-equiv=\"Content-Type\"
      content=\"text/html; charset=iso-8859-1\" />"
     ."<head>"
     ."<style type=\"text/css\">"
     ."h1{"
     ."border-bottom-style: double;"
     ."border-bottom-color: black;"
     ."border-bottom-width: 5px;"
     ."}"
     ."p{"
     ."background-color:#4682B4;"
     ."}"
     ."</style>"
     ."</head>"
     ."<body style=\"background-color:#B8B8B8\">"
     ."<table align=\"center\" style=\"background-color:#B8B8B8\" WIDTH=\"100%\" HEIGHT=\"100%\">"
     ."<tr valign=\"middle\">"
     ."<td align=\"center\">"
     ."<table align=\"center\" cellpadding=\"10\" style=\"background-color:#5CADFF\" width=\"600\" height=\"400\">"
     ."<tr>"
     ."<th colspan=6 align=\"center\"><h1>Welcome: ".$uid."</h1></th>"
     ."</tr>"
     ."<tr>"
     ."<form method=\"Post\" action=\"userview.php\">"
     ."<td colspan=3 width=40%>"
     ."UAID:  <input type=\"text\" size=\"8\" name=\"UAID\" value= ".$rows['UAID']." /></td>"
     ."<td colspan=3 width=60%>"
     ."Name (LAST, FIRST):<input type=\"text\" size=\"8\" name=\"last\"  value= ".$rows['lastname']." />,
      <input type=\"text\" size=\"8\" name=\"first\" value= ".$rows['firstname']." />"
     ."</td>"
     ."</tr>"
     ."<tr>"
     ."<td colspan=3 width=40%>"
     ."Department:<input type=\"text\" size=\"8\" name=\"department\" value=" .$rows['BOG'].">"
     ."</td>"
     ."<td colspan=3 width=60%>"
     ."Employee ID:<input type=\"text\" size=\"8\" name=\"empID\" value=" .$rows['employeeID'].">"
     ."</td>"
     ."</tr>"
     ."<tr>"
     ."<td colspan=3 align=\"center\"><input type=\"submit\" value=\"View\">"
     ."</form>"
     ."</td>"
     ."<form method=\"Post\" action=\"homepage.php\">"
     ."<td colspan=3 align=\"center\"><input type=\"submit\" value=\"Back\">"
     ."</td>"
     ."</tr>"
     ."</tr>"
     ."</table>"
     ."<tr></tr></td></tr></table></body></html>";

}}
mysql_free_result($result);
?>
  

 

 

I am now wondering if there is a more efficient way of coding this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.