Web Analytics Made Easy -
StatCounter standard select option doesn't work with javascript and mysql - CodingForum

Announcement

Collapse
No announcement yet.

standard select option doesn't work with javascript and mysql

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • standard select option doesn't work with javascript and mysql

    I want in the select option a standard word, for example: select name.
    But when I add a line of option just after the first select (bold text), that line takes the first record of the database, but it should be a blank line (nothing linked to it, just a reminder to select a name).

    This is the code I have now:
    Code:
    <script>
    function setVal()
    {
    document.getElementById('naam').value = cArray[document.getElementById('name').selectedIndex];
    document.getElementById('email').value = dArray[document.getElementById('name').selectedIndex];
    }
    var cArray = new Array();
    var dArray = new Array();
    </script>
    	<!-- Contact info -->
    	<table border="0" width="100%">
        <tr>
        <td style="text-align:right" width="150"><?php echo $hesklang['select_name']; ?>: <font class="important">*</font></td>
        <td width="80%">
        <?php    
          require(HESK_PATH . 'inc/database.inc.php');
          hesk_dbConnect();
          $sql = 'SELECT naam,email FROM `'.$hesk_settings['db_pfix'].'names`';
          $result = hesk_dbQuery($sql) or die ('Error in query: $query. ' . mysql_error());
          
          echo "<select id=\"name\" name=\"name\" onChange=\"javascript:setVal();\">";
          [B]echo "<option value='select name'>Select name</option>[/B]
          
          // keeps getting the next row until there are no more to get
          $Cnt=0;
          while($row = mysql_fetch_array( $result )) {
          // Print out the contents of each row into a dropdown
          echo "<option value='$row[naam]'>$row[naam]</option>";
          //Adding to Array
          echo "<script>cArray[$Cnt]='$row[naam]';</script>";
          echo "<script>dArray[$Cnt]='$row[email]';</script>";
          $Cnt++;
          }
          echo "</select>";
          ?>
    Please help me with this, need a solution soon.
    Last edited by UD2006; Apr 8, 2009, 05:00 AM.

  • #2
    Code:
     [ICODE]echo "<select[/ICODE] id=\"name\" name=\"name\" onChange=\"javascript:setVal();\">";
          echo "<option value='select name'>Select name</option>
          
          // keeps getting the next row until there are no more to get
          $Cnt=0;
          while($row = mysql_fetch_array( $result )) {
          // Print out the contents of each row into a dropdown
          echo "<option value='$row[naam]'>$row[naam]</option>";
          //Adding to Array
         [ICODE] echo "<script>cArray[$Cnt]='$row[naam]';</script>";
          echo "<script>dArray[$Cnt]='$row[email]';</script>";[/ICODE]
          $Cnt++;
          }
          [ICODE]echo "</select>";[/ICODE]
    Your markup is invalid, I believe.
    Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

    Comment


    • #3
      What do you mean by "Your markup is invalid"? And what is wrong then?

      It works, but only the standard selected ("select name") doesn't work.
      And I dont seem to get it right.

      Comment


      • #4
        Code:
        echo "<option value='select name'>Select name</option>[SIZE="6"][B][COLOR="Red"]";[/COLOR][/B][/SIZE]
        I don't know how the PHP page ran at all, with that syntax error.

        But in any case, why use PHP to write out fixed HTML content???

        Why not K.I.S.S.?
        Code:
            <td width="80%">
        [COLOR="Blue"]        <select id="name" name="name" onChange="javascript:setVal();">
                    <option value="">Select name</option>
        [/COLOR]    <?php    
              require(HESK_PATH . 'inc/database.inc.php');
              ... etc. ...
        It's not necessary, but usually if you have an <OPTION> that should not be used (that is, a "title" as you have here), you give it an impossible value= so it's easy to distinguish from legit options. So I changed your value to just blank.
        Be yourself. No one else is as qualified.

        Comment

        Working...
        X