Jump to content

Selected value in php option


claro

Recommended Posts

I've been working this code like in 4 hours just to look for the error. My problem is I can't get the selected values. It refresh and display the selected 'Select One' in options.

 

echo "<label for='device'> Select Device </label>";
echo "<select name='device_name' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
//echo "<option value=''>Select one</option>";


if (is_array($list))
{
foreach ($list as $dname)
{
$dev_code = $dname[0];
$dev_name = $dname[1];

if ($dev_code == @$dev)
{
echo "<option selected value ='$dev_code'>$dev_name</option>";
}
else
{
echo "<option value='$dev_code'>$dev_name</option>";
}
}
}

echo "</select><br/>";

 

Any help is greatly appreciated. Thank you.

Edited by claro
Link to comment
Share on other sites

echo "<label for='device'> Select Device </label>";

 

 

 

@$dev = $_GET['dev']; // Use this line or below line if register_global is off
   if(strlen($dev) > 0 and !ctype_alpha($dev)){ // to check if $cat is numeric data or not. 
   echo "Data Error";
   exit;
   }

 

$dev is responding, I put it above my code.

Link to comment
Share on other sites

Ok, is there an error or is there a warning (or perhaps a notice)? These are different things. Rather than suppression you should properly code to catch issues like that by checking the if(!isset(...)). Using !isset() will not throw an error/warning/notice if the variable has not been declared.

 

To be fair, you really shouldn't touch error supression ar all unless you are already managing errrors on your own with custom code.

 

We will need you to post up all the relevent code, including the form, and also a source view of the page once it's rendered in the browser. What you have in the OP is not enough for us to work with.

Link to comment
Share on other sites

Ok, I hope this helps.

   <SCRIPT language="javascript"> 
    function reload(form){
        var val=form.device_name.options[form.device_name.options.selectedIndex].value; 
        self.location='assign_sn.php?dev=' + val ;
        }
   </SCRIPT> 
 </head>
 <body>
   <form method='post' action='assign_sn_action.php'>
   <?php
   $dev = $_GET['dev']; // Use this line or below line if register_global is off
   if(strlen($dev) > 0 and !ctype_alpha($dev)){ // to check if $cat is numeric data or not. 
   echo "Data Error";
   exit;
   }
    $device = new DevType();
    $list = $device ->viewList();

      echo "<label for='device'> Select Device </label>";
      echo "<select name='device_name' onchange=\"reload(this.form)\"><option value=''>Select one</option>";

        if (is_array($list))
        {
          foreach ($list as $dname)
          {
            $code =  $dname[0];
            $name =  $dname[1];

             if ($code == $dev)
             {
               echo "<option  selected value ='$code'>$name</option>";
             }
             else
             {
               echo "<option value='$code'>$name</option>";
             }
          }       
        }
        echo "</select><br/>";

   ?>
 <input type='submit' value='submit'>
 </form>


Link to comment
Share on other sites

I think I found the error now. But I don't know how did it happen.

 

array(3) {
 [0]=>
 array(2) {
   [0]=>
   string(10) "HDD       "
   [1]=>
   string( "Harddisk"
 }
 [1]=>
 array(2) {
   [0]=>
   string(6) "MBOARD"
   [1]=>
   string(11) "Motherboard"
 }
 [2]=>
 array(2) {
   [0]=>
   string(10) "MTOR      "
   [1]=>
   string(7) "Monitor"
 }
}

 

 

The string count of my 'HDD' and 'MTOR'.

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.