Jump to content

Choose from the drop down


kvnirvana

Recommended Posts

How can I make it so if the user chooses 'all' in the drop down list it shows a massage saying that the user has to choose from the drop down?

/*------------------------------------------------------------------------ 
            create the drop downs 
------------------------------------------------------------------------*/ 

function dropdown($field, $table) 
{  
  //initialize variables 
  $oHTML  = ''; 
  $result = ''; 
  
  //check to see if the field is passed correctly 
  if (($field == "")||($table == "")) 
  { 
    die("No column or table specified to create drop down from!"); 
  } 

  $sql = "select distinct($field) from $table"; 
  
  //call the db function and run the query 
  $result = conn($sql); 

  //if no results are found to create a drop down return a textbox 
  if ((!$result) ||(mysql_num_rows($result)==0)) 
  { 
    $oHTML .= "<input type='text' name='$field' value='' size='15'>"; 
  }elseif (($result)&&(mysql_num_rows($result)>0)){ 
    
    //build the select box out of the results 
    $oHTML .= "<select name='$field'>\n<option value='all'>All</option>\n"; 
    while ($rows = mysql_fetch_array($result)) 
    { 
      $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; 
    } 
    $oHTML .= "</select>\n"; 
  } 
  
  //send the value back to the calling code 
  return $oHTML; 
}//end function

Link to comment
Share on other sites

Ok, I tried it the way you suggested but it wont work, it still displays all result if all is chosen. I changed 'All' to 'Choose' so it makes more sense :=)

 $oHTML .= "<select name='$field'>\n<option onClick='alert(\"warning\")' value='choose'>Choose</option>\n"; 

Link to comment
Share on other sites

Still posible to choose the option 'choose'. There is no alert when choosing 'choose', if 'choose' is chosen it displays the message 'no results found' which is off course the message it should display if there is no results. I guess I should make an if statement, something like if 'choose' is chosen display this, but don't know how I should do this.

Link to comment
Share on other sites

You can add an onchange event to the select element, test its value, and alert the user at that point if it's the one you don't want them to select and then choose something else.  You could use that same onchange event to store the most recent "good" selection to return to if they select the wrong one.

Link to comment
Share on other sites

I don't now how to do it, but i would like if it is not java because the user can then just disable java from their browser couldn't it be done with an if statement? here is by the way my full code

<?php 

/*------------------------------------------------------------------------ 
            control codes 
------------------------------------------------------------------------*/ 


if (isset($_POST['submit'])) 
{ 

  search();       //call the search function 

}else{ 

  show_form();   //call the show form function 

}//end if 

/*------------------------------------------------------------------------ 
            show the search form 
------------------------------------------------------------------------*/ 

function show_form() 
{ 
  //call the dropdown function which creates an html string to build a select box for each element 
  $beha   = dropdown('beha','behan'); 
  $omraede  = dropdown('omraede','behan'); 
  $pr     = dropdown('pr','behan'); 


  
  
  
  echo "<form name='search' action=".$_SERVER['PHP_SELF']." method='post'> 
        <table width='50%' align='center' valign='center'> 
        
	<tr> 
          <td colspan='2' align='center'>Search Form</td> 
        </tr> 
        <tr> 
          <td align='right'>Beha:</td><td>$beha</td> 
        </tr> 
        <tr> 
          <td align='right'>Omraede:</td><td>$omraede</td> 
        </tr> 
        <tr> 
          <td align='right'>Pr:</td><td>$pr</td> 
        </tr> 
  
         
          <td colspan='2' align='center'> </td> 
        </tr> 
        <tr> 
          <td colspan='2' align='center'><input type='submit' name='submit' value='Go!'></td> 
        </tr> 

        </table> 
        </form>"; 

}//end function 


/*------------------------------------------------------------------------ 
            run the search and show the results 
------------------------------------------------------------------------*/ 

function search() 
{ 

//base sql 
  $sql = "select * from behan WHERE 1"; 

//get the values from the form 


  if ((!empty($_POST['beha']))&&($_POST['beha'] != 'all')) 
  { 
    $sql .= " and beha like '". addslashes($_POST['beha'])."%' "; 
  } 
  
  if ((!empty($_POST['omraede']))&&($_POST['omraede'] != 'all')) 
  { 
    $sql .= " and omraede like '". addslashes($_POST['omraede'])."%' "; 
  } 
  
  if ((!empty($_POST['pr']))&&($_POST['pr'] != 'all')) 
  { 
    $sql .= " and pr = '". addslashes($_POST['pr'])."' "; 
  } 
  

  
print "<table border=1>";

  //add more elements (or take away) as you desire...follow the same code structure as above 

    
  //run query 
$result = conn($sql);
  if (!$result){ die("No results due to database error.<br>".mysql_error());  }
  if (mysql_num_rows($result)==0)
  {
    echo "No Results found!";
  }else{
echo "<TABLE  width=100% height=300 border='0' cellpadding='5' cellspacing='10'>";
$first_run = "true";
while ($rows= mysql_fetch_array($result))
{
   echo "<TR>";   
if ($first_run == "true"){
    echo "<TH colspan='5' scope='colgroup'><u>Resultat for: <scope='row'>". $rows['beha'] .", ". $rows['omraede'] .", ". $rows['pr'] ."</u><br></br></Th>";
$first_run = "false";
}
    echo "</TR>";
   echo     "<TR>";
    echo "<TH scope='col' abbr='beha'><u>Beha</u></TH>";
    echo "<TH scope='col' abbr='rating'><u>Rating</u></TH>";
    echo "<TH scope='col'><u>Kommentar</u></TH>";
  
  echo "</TR>";
   echo "<TR>";
    echo "<TD width=20% height=100 scope='row'>". $rows['beha'] ."<p> ". $rows['na'] ."<p> ". $rows['ad'] ."<p> ". $rows['po'] .", ". $rows['by'] ."<p> ". $rows['tl'] ."</TD>";
    echo "<TD width=30% height=100>". rating_bar($rows['id'],'6','static')."<a href='jadak.php?id={$rows['id']}&na={$rows['na']}&pr={$rows['pr']}&beha={$rows['beha']}'>Bedøm </a> </TD>";
    echo "<TD width=30% height=100><a href='komment.php?na={$rows['na']}&id={$rows['id']}&beha={$rows['beha']}'>Læs kommentarer</a> </TD>";
echo "<TABLE  width=100% height=100 border='0' cellpadding='5' cellspacing='10'><hr>";
  echo "</TR>"; 
    }  
     echo "</table>";  }



  
  //end if 



}//end function 






/*------------------------------------------------------------------------ 
            create the drop downs 
------------------------------------------------------------------------*/ 

function dropdown($field, $table) 
{  
  //initialize variables 
  $oHTML  = ''; 
  $result = ''; 
  
  //check to see if the field is passed correctly 
  if (($field == "")||($table == "")) 
  { 
    die("No column or table specified to create drop down from!"); 
  } 

  $sql = "select distinct($field) from $table"; 
  
  //call the db function and run the query 
  $result = conn($sql); 

  //if no results are found to create a drop down return a textbox 
  if ((!$result) ||(mysql_num_rows($result)==0)) 
  { 
    $oHTML .= "<input type='text' name='$field' value='' size='15'>"; 
  }elseif (($result)&&(mysql_num_rows($result)>0)){ 
    
    //build the select box out of the results 
     $oHTML .= "<select name='$field'>\n<option onClick='alert(\"warning\")' value='choose' disabled>Choose</option>\n";
    while ($rows = mysql_fetch_array($result)) 
    { 
      $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; 
    } 
    $oHTML .= "</select>\n"; 
  } 
  
  //send the value back to the calling code 
  return $oHTML; 
}//end function 

/*------------------------------------------------------------------------ 
            database connection function 
------------------------------------------------------------------------*/ 

function conn($sql) 
{    
  $username  = "****"; 
     $pwd      = "***"; 
     $host        = "localhost"; 
     $dbname      = "*****"; 

    //echo "commnecing connection to local db<br>"; 
    
    if (!($conn=mysql_connect($host, $username, $pwd)))  { 
        printf("error connecting to DB by user = $username and pwd=$pwd"); 
        exit; 
    } 
    $db3=mysql_select_db($dbname,$conn) or die("Unable to connect to local database"); 
    
    $result = mysql_query($sql) or die ("Can't connect because ". mysql_error()); 
    
    return $result; 
    
}//end function      

?>

 

Any suggestions much appriciated

Link to comment
Share on other sites

If you don't want to use javascript (and I agree you should have some server-side validation) you can't do it without allowing them to submit the form, because the server won't see it until you've submitted the form.  So you'll just have to test the value and redraw the form with an error message if they have the wrong one selected.

Link to comment
Share on other sites

ok, but how can that be done?

 

Just put a test for it in your logic, something like:

 

if (isset($_POST['submit'] && $_POST['field'] != 'bad_value') {
  search();
} else {
  if ($_POST['field'] == 'bad_value') {
    ... display an error ...
  }
  ... continue with the original form ...
}

 

Anyone?

 

I would imagine that a good portion of the visitors to this board are in the US, so the five hours between 3am and 8am you're not going to likely hear from any of the US folk.  Patience is a virtue.

Link to comment
Share on other sites

You could use a script that enables a button that is hard coded with your php script to be disabled too, that way if java-script is not enabled that can't run the php script at all. You'd still want you checks though, just something that's fun to do is all. Probably be annoying to the 2% that still think Java-Script is going to give them a virus.

Link to comment
Share on other sites

My bad, I didn't think of that :=) I tried using your code like this

function search() 
{ 

//base sql 
  $sql = "select * from behan WHERE 1" ; 

//get the values from the form 
//NOTE: You should do way more valdation on the values before you attempt to process anything 

  if ((!empty($_POST['beha']))&&($_POST['beha'] != 'all')) 
  { 
    $sql .= " and beha like '". addslashes($_POST['beha'])."%' "; 
  } 
  
  if ((!empty($_POST['omraede']))&&($_POST['omraede'] != 'all')) 
  { 
    $sql .= " and omraede like '". addslashes($_POST['omraede'])."%' "; 
  } 
  
  if ((!empty($_POST['pr']))&&($_POST['pr'] != 'all')) 
  { 
    $sql .= " and pr = '". addslashes($_POST['pr'])."' "; 
  } 
if (isset($_POST['submit']) && ($_POST['field'] != 'bad_value')) {  search();} 
else {  if ($_POST['field'] == 'bad_value') {   echo "error";   } 
  }
  
print "<table border=1>";

 

But then I get this error 'Undefined index: field'

Link to comment
Share on other sites

Help :=) Bad value is off course =All so it's like this

function search() { //base sql   $sql = "select * from behan WHERE 1" ;
//get the values from the form //NOTE: You should do way more valdation on the values before you attempt to process anything  
if ((!empty($_POST['beha']))&&($_POST['beha'] != 'all'))   {     $sql .= " and beha like '". addslashes($_POST['beha'])."%' ";   }    
if ((!empty($_POST['omraede']))&&($_POST['omraede'] != 'all'))   {     $sql .= " and omraede like '". addslashes($_POST['omraede'])."%' ";   }    
if ((!empty($_POST['pr']))&&($_POST['pr'] != 'all'))   {     $sql .= " and pr = '". addslashes($_POST['pr'])."' ";   } 
if (isset($_POST['submit']) && ($_POST['field'] != 'all')) {  search();} else {  if ($_POST['field'] == 'all') {   echo "error";   }   }  print "<table border=1>";

Link to comment
Share on other sites

Ok, I got this right now

function search() 
{ 

//base sql 
  $sql = "select * from behan WHERE 1=1"; 

//get the values from the form 
//NOTE: You should do way more valdation on the values before you attempt to process anything 

  if ((!isset($_POST['submit'])) && ($_POST['beha'] != 'choose')) {  search();} else {  if ($_POST['beha'] == ' choose ') {   echo "error beha!<br>";   }   } 
  { 
    $sql .= " and beha like '". addslashes($_POST['beha'])."%' "; 
  } 
  
  if ((!isset($_POST['submit'])) && ($_POST['omraede'] != ' choose ')) {  search();} else {  if ($_POST['omraede'] == ' choose ') {   echo "error område!<br>";   }   }  
  { 
    $sql .= " and omraede like '". addslashes($_POST['omraede'])."%' "; 
  } 
  
if ((!isset($_POST['submit'])) && ($_POST['pr'] != ' choose ')) {  search();} else {  if ($_POST['pr'] == ' choose ') {   echo "error pr!";   }   }  
  { 
    $sql .= " and problem = '". addslashes($_POST['problem'])."' "; 
  } 
  
  
  

  // ADD ORDER BY  
  $sql .= ' order by total_value DESC ';
  
print "<table border=1>";

  //run query 
$result = conn($sql);
  if (!$result){ die("No results due to database error.<br>".mysql_error());  }
  if (mysql_num_rows($result)==0 && mysql_num_rows($result)!=' choose ')
  {
    echo "No results!";
  }

It works fine, but if there is no result from the search it just shows a blank page where it should show the message ”No results!”

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.