[SOLVED] Problems on creating a dropdown menu using php

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
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

[SOLVED] Problems on creating a dropdown menu using php

Post by ryuuka »

k back again
this time i'm having a problem with a dropdown menu i wish to create for my site.

webserver info:
php5
mssql
adodb usage a possibility

what i basicly want to do is to create a menu that hold various server names and ads it to the dbase
when submitted.
but right now i will settle for getting the thing to actually show up.

i have tried a lot on this and i never got it to show up even once.
this is what i have so far:

Code: Select all

if ((isset($_GET['toevoegen'])) && (isset($_GET['toevoegen']))) {  
  echo "\n            <form action=\"index.php?pagename=servinv&save=1\" method=\"POST\">".
       "\n              <table>".
       "\n                <tr>".
       "\n                  <th>".
       "\n                    Server Naam:".
       "\n                  </th>".
       "\n                  <td>".
       $servers = array( 'DC0001', 'SR0001', 'SR0003','SR0004','SR0005','SR0007',
        'SR0008','SR0010','SR0022','SR0025','SR0026','SR0077','SR0078');
       // $servers = str_replace(" ", "&nbsp;", $servers);
       "\n                 \"<SELECT name=servers> 
                           foreach ($servers as $value){
                              <OPTION value='.$value.'>'.$value.' </option>'}"
                          '</select>' \"".
       "\n                  </td>".
       "\n                  <th>".
       "\n                    Displaynaam:".
       "\n                  </th>".
       "\n                  <td>".
       "\n                    <input type=\"text\" tabindex=\"5\" name=\"displaynaam\" />".
       "\n                  </td>".
       "\n                </tr>".
i know that the mistake is somewhere in the foreach record but i can't seem to be able to find it.
what i eventually want to do is get the names from a database and after that it needs to add the name
to another database.
if a javascript suggestion comes up please post the code that goes with it if you can as i have never worked with javascript before.
Last edited by ryuuka on Thu Sep 07, 2006 6:22 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Initial thoughts are you are mixing PHP and HTML.

I would code like:

Code: Select all

<?php
    if ((isset($_GET['toevoegen'])) && (isset($_GET['toevoegen']))) {
      
      // PHP processing here
      $server_opt='<option value="0">Choose One...</option>';
      $servers = array( 'DC0001', 'SR0001', 'SR0003','SR0004','SR0005','SR0007',                                   'SR0008','SR0010','SR0022','SR0025','SR0026','SR0077','SR0078');  
      foreach ($servers as $value) {
        $server_opt.="<option value=\"$value\">$value</option>";
      }
    
      // Output here
      echo('<form action="index.php?pagename=servinv&save=1" method="POST">
              <table>
                <tr>
                  <th>Server Naam:</th>
                  <td>
                    <select name="servers">
                      '.$server_opt.'
                    </select>
                  </td>
                  <th>
                    Displaynaam:
                 </th>
                 <td>
                   <input type="text" tabindex="5" name="displaynaam" />
                 </td>
                </tr><!-- is there more here ? -->');
  }
?>
Processing like this makes things a lot easier to read and correct. I use two types of string settings (there are three shown here) and this is purely my preference. When I am outputting pure HTML with possibly one or two php variables I generally use single quotes which means I don't have to backslash normal quotes but does mean I can't use php variables or \n without using concatenation (single quotes need \'). For lines where HTML code is required with lots of PHP variables I use double quotes where the " character must be escaped \" but I can place variables in as I want.
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

Sorry doesn't work i'm including the rest of the code. maybe the mistake is somehwhere else
(database is crossed out but shouldn't matter)

Code: Select all

// it checks if the page is opened right.
  @include "system/include_check.inc.php";
  @include "../system/include_check.inc.php";
 
  head("Services Invoer","m");
  
  // adds object to dbase

  if ((isset($_GET['save'])) && (isset($_GET['save']))) {
    $sql = ("INSERT INTO ********
                 VALUES ('".$_POST['computernaam']. "',   '".$_POST['displaynaam']."',
                               '".$_POST['servicenaam']."',        '".$_POST['status']."',
                                 { fn NOW() },                            '".$_POST['controleren']."')");
  }
  $db->Execute
  

   // Adding new services
  if ((isset($_GET['toevoegen'])) && (isset($_GET['toevoegen']))) {  
  
   // PHP processing here 
      $server_opt='<option value="0">Choose One...</option>'; 
      $servers = array( 'DC0001', 'SR0001', 'SR0003','SR0004','SR0005','SR0007',
      'SR0008','SR0010','SR0022','SR0025','SR0026','SR0077','SR0078');  
      foreach ($servers as $value) { 
        $server_opt.="<option value=\"$value\">$value</option>"; 
      } 

  echo(' <form action=\"index.php?pagename=servinv&save=1\" method=\"POST\">
                 <table>
                       <tr> 
                          <th>Server Naam: </th>
                       <td>
                          <select name="servers">'$server_opt.'</select> 
                       </tr>
                       </td>
                          <th>Displaynaam: </th>
                          <td><input type=\"text\" tabindex=\"5\" name=\"displaynaam\" /></td>
                       </tr>
                       <tr>
                         <th>Servicenaam:</th>
                         <td><input type=\"text\" tabindex=\"2\" name=\"servicenaam\" /></td>
                       </tr>
                       <tr>
                        <th>Status:</th>
                         <td><input type=\"text\" tabindex=\"6\" name=\"status\" /></td>
                       </tr>
                       <tr>
                         <th>controleren?:</th>
                         <td><input type=\"checkbox\" value=\"1\" name=\"controleren\" /></td>
                       </tr>
                     </table>
                     <input type=\"submit\" value=\"submit\">
                   </form>');
      } 
  foot();
}
maybe this will be off more use
thank coder for chopping of about 60 lines with his advice :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If you do a view source do you get anything ?

Insert a echo("<br />IN IF STATEMENT<br />"); immediately after the IF to check the code enters.

The echo statement does not need escaped double quotes (its the reason I encased it in single quotes as I hate escaping everything).
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

Post by jito »

CoderGoblin's code look fine to me - follow him. and if you get a blank page test
if ((isset($_GET['save'])) && (isset($_GET['save'])))
is it really set or not? you can check using query string: urpage.php?save=1. that code should run.
any special reason for double check?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Didn't notice that 8O
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

jito <- i don't know if the : if ((isset($_GET['save'])) && (isset($_GET['save']))) is really needed but i got this from another part of the site that always worked well. the problem is though that the guy who made this in the first place doesn't work here anymore.

CoderGoblin <- when i viewed the sourc from the bland page i ussally get, i get nothing and i can't get anything more out of it because it's a in a framwork (maybe important). Rest of the page doen't show either. tried the
?save=1 that didn't work.
and the: Insert a echo("<br />IN IF STATEMENT<br />"); didn't work either

really weird though i suspect the frames and the query havesomething to do with it. will go and look if anything special hides in there.
also uses css as a means to divide in different sections and for the makeup of the tables etc.

i also noticed that if i just input the page like this:

Code: Select all

<?php
// PHP processing here 
      $server_opt='<option value="0">Choose One...</option>'; 
      $servers = array( "DC0001", 'SR0001', 'SR0003','SR0004','SR0005','SR0007',
      'SR0008','SR0010','SR0022','SR0025','SR0026','SR0077','SR0078');  
      

  echo(' <form action=\"index.php?pagename=servinv&save=1\" method=\"POST\">
                 <table>
                       <tr> 
                          <th>Server Naam: </th>
                       <td>
                          <select name="servers">');
                          foreach ($servers as $key =>$value) { 
                            $server_opt.="<option value=\"$servers\">$value</option>";
                            echo '$server_opt';
                            }; 
       
                      echo('</select> 
                       </tr>
                       </td>
                 *******
                       :</th>
                         <td><input type=\"checkbox\" value=\"1\" name=\"controleren\" /></td>
                       </tr>
                     </table>
                     <input type=\"submit\" value=\"submit\">
                   </form>');

  ?>
it shows up. but the submit and checkbox don't work and the menu doesn't give me any options (completly empty)
ps. i know i'm being a really difficult client right now.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

To narrow down the problem... try this.. make a backup (copy the file elsewhere or simply comment out all your code).

Code: Select all

<?php
  error_reporting(E_ALL);
  ini_set('display_errors', TRUE);

  // it checks if the page is opened right.
  include "system/include_check.inc.php";
  include "../system/include_check.inc.php";
 
  head("Services Invoer","m");
  echo("HELLO");
?>
If you still get a blank page it's the framework at fault (could it be the filename ?).

If that works restore from the backup and add a condition to the if statement

Code: Select all

} else {
  print_r($_GET);
}
The

Code: Select all

if ((isset($_GET['save'])) && (isset($_GET['save']))) {
could be just

Code: Select all

if (isset($_GET['save'])) {
The $_GET['save'] variable will not change while processing the first isset to modify it for the second.
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

solution to the menu prob

Code: Select all

$rs = $db->Execute("SELECT computernaam FROM  ******* WHERE (computernaam LIKE 'sr0%') OR (computernaam LIKE 'dc0%')");

<select name:"servernaam">              
              <option value= "0" selected> Kies server: 
              
       ');
                                while (!$rs->EOF)
                                {
                                  $value = $rs->Fields['computernaam']->value;
                                  echo "<option value=$value>$value";
                                  $rs->movenext();
                                }</select>
yay :)
Post Reply