4 Replies - 3025 Views - Last Post: 01 August 2008 - 06:50 AM Rate Topic: -----

#1 nahelna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-July 08

helpdesk system(populate two drop down lists) one depends on the othe

Post icon  Posted 30 July 2008 - 06:10 AM

pliz help me, i keep getting the error
Notice: Undefined index: cat in c:\program files\easyphp1-8\www\myhelpdesk\postqueryagain.php on line 40
if i remove the line $cat=$_GET['cat']; the second drop down menu doesnt function well thanx guys


<?php

include('connect_db.php');
$quer2=mysql_query("SELECT DISTINCT typename,probtypeid FROM probtype order by typename");
$cat=$_GET['cat'];//This line is added to take care if your global variable is off
if(isset($cat) and strlen($cat) > 0)
{
$quer=mysql_query("SELECT DISTINCT catname FROM category where probtypeid=$cat order by catname");
}
else
{
$quer=mysql_query("SELECT DISTINCT catname FROM category order by catname"); 
} 



echo "<form method=post name=f1 action='postquery.php'>";
//Add your form processing page address to action in above line. Example action=dd-check.php
// Starting of first drop downlist 
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>select problem type</option>";
while($noticia2 = mysql_fetch_array($quer2)) 
{
 if($noticia2['probtypeid'][email protected]$cat)
 {
 echo "<option selected value='$noticia2[probtypeid]'>$noticia2[typename]</option>"."<BR>";
 }
 else
 {
 echo "<option value='$noticia2[probtypeid]'>$noticia2[typename]</option>";
 }
} 
echo "</select>";

//Starting of second drop downlist 
echo "<select name='subcat'><option value=''>Select problem category</option>";
while($noticia = mysql_fetch_array($quer))
{
echo "<option value='$noticia[catname]'>$noticia[catname]</option>";
}
echo "</select>";
//This will end the second drop down list 
  

echo "</form>";

?>




Is This A Good Question/Topic? 0
  • +

Replies To: helpdesk system(populate two drop down lists) one depends on the othe

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5226
  • View blog
  • Posts: 14,005
  • Joined: 18-April 07

Re: helpdesk system(populate two drop down lists) one depends on the othe

Posted 31 July 2008 - 10:14 PM

Well the first problem is that your form is submitting using the "POST" method. That means that you should be checking the $_POST array and not the $_GET array for 'cat'. This is part of why you are getting the error. You should also always check if the value is "set" before attempting to get at its value. You can do this using the function "isset()".

// This results in $cat being set to the value from your posted form 'cat' field
// or it will be set to an empty string if the $_POST['cat'] is not set.
$cat = (isset($_POST['cat'])) ? $_POST['cat'] : '';



Also as a side note, besides checking if it is set or not, remember to always check the actual value of your form fields for the proper format and information. This is to protect yourself from SQL injection attacks which could compromise your machine.

You can read up on the net about SQL injection attacks and how to protect yourself by validating content coming into your forms. One such place is the link below, but I recommend reading multiple sites.

SQL Injection Attacks by Example

Good luck. :)

This post has been edited by Martyr2: 31 July 2008 - 10:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 nahelna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-July 08

Re: helpdesk system(populate two drop down lists) one depends on the othe

Posted 01 August 2008 - 04:07 AM

thanx a lot Martyr2 the error is gone!
about injections i read about that and am still reading am new to this stuff!
but!!!!!!!!!!
the values of the second drop down menu can nolonger be displayed! yet my table for the subcategory contains data. i can still get the 1st dropdown list. thanx agian
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php


$quer2 = mysql_query("SELECT DISTINCT typename,probtypeid FROM probtype order by typename");

$cat = (isset($_POST['cat'])) ? $_POST['cat'] : ''; 

if(isset($cat) and strlen($cat) > 0)
{
  $quer=mysql_query('SELECT DISTINCT `catname` FROM `category` WHERE`probtypeid`= \''.mysql_escape_string($cat).'\' ORDER BY `catname`');
}


else
{
  $quer=mysql_query('SELECT DISTINCT `catname` FROM `category` OREER BY `catname`');
}

echo '<form method="post" name="f1" action="postquery.php">';


//Add your form processing page address to action in above line. Example action=dd-check.php
// Starting of first drop downlist
echo '<select name="cat" onchange="reload(this.form)">
	   <option value="">select problem type</option>';

while($noticia2 = mysql_fetch_assoc($quer2))
{
  if($noticia2['probtypeid'][email protected]$cat)
   {
	echo ' <option selected value="',mysql_escape_string($noticia2['probtypeid']),'">',$noticia2['typename'],'</option>',"\n";
   }
  else
   {
	echo ' <option value="',$noticia2['probtypeid'],'">',$noticia2['typename'],'</option>',"\n";
   }
}
echo '</select>';


//Starting of second drop downlist
echo '<select name="subcat"><option value="">Select problem category</option>',"\n";

while($noticia = mysql_fetch_array($quer))
{
  echo ' <option value="',$noticia['catname'],'">',$noticia['catname'],'</option>',"\n";
}

echo '</select>';

//This will end the second drop down list
echo '</form>';



?>
</body>
</html>



Was This Post Helpful? 0
  • +
  • -

#4 nahelna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-July 08

Re: helpdesk system(populate two drop down lists) one depends on the othe

Posted 01 August 2008 - 05:15 AM

wow!!!!!!
it just worked out! i replaced the line with the code you gave me!
the error was gone but secondlist values could not be displayed then i used the code you gave me but using $_GET i dont know why it worked out but it did!

$cat = (isset($_GET['cat'])) ? $_GET['cat'] : ''; 



YOU JUST MADE MY DAY!
Was This Post Helpful? 0
  • +
  • -

#5 nahelna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-July 08

Re: helpdesk system(populate two drop down lists) one depends on the othe

Posted 01 August 2008 - 06:50 AM

well, taht was solved, but how can i catch the subcategory id (in my case it is the column catid from table category)
so that i can store it in another table called problems where users post their problems.
this is the page which generated the 2 dropdown lists

<head>
<script language=Javascript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value; 
self.location='postquery.php?cat=' + val;
}
</script>
</head>
<?php


$quer2 = mysql_query("SELECT DISTINCT typename,probtypeid FROM probtype order by typename");


$cat = (isset($_GET['cat'])) ? $_GET['cat'] : ''; 


if(isset($cat) and strlen($cat) > 0)
{
  $quer=mysql_query('SELECT DISTINCT `catname`,`catid` FROM `category` WHERE`probtypeid`= \''.mysql_escape_string($cat).'\' ORDER BY `catname`');
}


else
{
  $quer=mysql_query('SELECT DISTINCT `catname`,`catid` FROM `category` ORDER BY `catname`');
}

echo '<form method="post" name="f1" action="postquery.php">';


//Add your form processing page address to action in above line. Example action=dd-check.php
// Starting of first drop downlist


echo '<select name="cat" onchange="reload(this.form)">
	   <option value="">select problem type</option>';


while($noticia2 = mysql_fetch_assoc($quer2))
{

	 if($noticia2['probtypeid'][email protected]$cat)
	  {
		 echo ' <option selected value="',mysql_escape_string($noticia2['probtypeid']),'">',$noticia2		  ['typename'],'</option>',"\n";
	   }

	  else
	  {
	   echo ' <option value="',$noticia2['probtypeid'],'">',$noticia2['typename'],'</option>',"\n";
	   }
}
echo '</select>';



//Starting of second drop downlist 

echo '<select name="subcat" ><option value="catid">Select problem category</option>',"\n";

while($noticia = mysql_fetch_array($quer))
{
  echo ' <option value="',$noticia['catid'],'">',$noticia['catname'],'</option>',"\n";
   //<option value="',$noticia['catid'],'">',$noticia['catname'],'</option>;
}


echo '</select>';

//This will end the second drop down list
echo '</form>';



?>



and when the user selects the values from the dropdown menus i use the insertquery.php page which gets the catid and inserts iit in the category table.
$catid = $_POST['catid'];//used to get the catid of the category name selected by the user

$insert = "INSERT INTO problems(probid,catid)  VALUES ('','$catid')";//inserts the value in the problems table
$add_member = mysql_query($insert);


but i get the error Notice: Undefined index: catid in c:\program files\easyphp1-8\www\myhelpdesk\insertquery.php on line 3
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1