Dropdown select to go url not working in while loop php

  <?php
    $sql = "select * from letter where societyn='" . $_SESSION['socityname'] ."' ";
    $result = mysql_query($sql);
    $i=1;
    while($row=mysql_fetch_array($result)){         
?>
<?php
    $id = $row['id'];
    $dater = $row['date'];
    $flatnonam = $row['flatno_name'];
    $addr = $row['adress'];
    $subject = $row['subject'];
    $body = $row['body'];
    $soci = $row['societyn'];
    $refnum = $row['refno'];
    $letterh = $row['letterh'];

        // split "dd-mm-yyyy" into an array of three elements
        $ddate = explode("-", $dater);
        // retrieve the values
        $month = $ddate[1]; // mm
        $day = $ddate[2]; // dd
        $year = $ddate[0]; // yyyy
    {
    ?>

<tr>
            <td><?php echo $day.'/'.$month.'/'.$year; ?></td>
            <td><?php echo $flatnonam; ?></td>
            <td><?php echo $letterh; ?></td>
            <td><?php echo $addr; ?></td>
            <td><?php echo $subject; ?></td>
            <td><?php echo $soci; ?></td>
            <td><?php echo '#'.$refnum; ?></td>

            <td>
            <select name="menu1" id="menu1">
                <option value="" selected>Pick a Website</option>
                <option value="letter/letter_print.php?id=<?php echo $row['id']; ?>">Without Letter Head</option>
                <option value="letter/letter_print_nohead.php?id=<?php echo $row['id']; ?>">Letter Head</option>
            </select>
            </td></tr>

    <?php }?>
    <?php
    echo "<br>";
    $i++;
    }

    ?>


<!--This is java script-->
<script type="text/javascript">
 var urlmenu = document.getElementById( 'menu1' );
 urlmenu.onchange = function() {
      window.open( this.options[ this.selectedIndex ].value, '_blank','width=595,height=842,directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no');
 };
</script>

      

This is my code. Only one option, the other doesn't work. please help me in trouble. from 2 days. or give another solution. In one id how to use all parameters. I tried from 2 days until it works.

+3


source to share


2 answers


As mentioned, change your id

to class

as you have multiple select

. Then define a onchange

listener for all of them:

var urlmenus = document.querySelectorAll('.menu1');
for(var i = 0; i < urlmenus.length; i++) {
    urlmenus[i].onchange = function () {
        window.open(this.options[this.selectedIndex].value, '_blank', 'width=595,height=842,directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no');
    };
}

      



jsfiddle DEMO

+1


source


Put this code and try



$(document).ready(function () {
        $("#menu1").change(function () {
            var val1 = $(this).val();
            window.open(val1, '_blank');
        });

    })
      

<form>

    <select name="menu1" id="menu1">
        <option value="" selected>Pick a Website</option>
        <option value="letter/letter_print.php?id=<?php echo $row['id']; ?>">Without Letter Head</option>
        <option value="letter/letter_print_nohead.php?id=<?php echo $row['id']; ?>">Letter Head</option>
    </select>
</form>
      

Run code


+1


source







All Articles