简体   繁体   中英

How can i get value from selection box in javascript?

                                <table border="1">
                                    <tr>
                                        <th>
                                            SR No.<br />
                                        </th>
                                        <th>
                                            Name
                                        </th>
                                        <th>
                                            Address
                                        </th>
                                        <th>
                                            Area
                                        </th>
                                        <th>
                                            City
                                        </th>
                                        <th>
                                            Contact No.
                                        </th>
                                        <th>
                                            Volunteer List
                                        </th>
                                        <th>
                                            Bird Namea
                                        </th>
                                        <th>
                                            Status
                                        </th>
                                        <th>
                                            Send SMS
                                        </th>
                                    </tr>
                                    <?php
                                        while($row = mysql_fetch_array($pager->result))
                                        {
                                    ?>
                                    <tr>
                                        <td>
                                            <?php echo $row['InqID']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqFromName']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqFromAddress']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqArea']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqCity']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqContactNo']; ?>
                                        </td>
                                        <td>
                                        <select name="volunteerSelect1" id="volunteerSelect1">
                                            <option value="" selected="selected">Please Select Volunteer</option>
                                            <?php
                                                $select="SELECT * FROM tran_ngovolent where ngovolentNGOCode=".$ngoSelectId;
                                                //echo $select;
                                                $result=mysql_query($select);
                                                while($rowBird=mysql_fetch_array($result))
                                                {
                                                    echo"<option value=".$rowBird['ngovolentMobile']." id='volmobile'>".$rowBird['ngovolentName']."</option>";
                                                }
                                            ?>
                                        </select>
                                        </td>
                                        <td>
                                            <?php 
                                                $birdID=$row['InqBirdType'];
                                                $birdSelect="SELECT Bird_Name FROM mst_bird WHERE Bird_Code=".$birdID;
                                                $result=mysql_query($birdSelect);
                                                while($bird_name=mysql_fetch_array($result))
                                                {
                                                    $birdname=$bird_name['Bird_Name'];
                                                    echo $birdname;
                                                }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                                $inqStatus=$row['InqStatus'];
                                                if($inqStatus==0)
                                                {
                                                    echo "<a href='index.php?id=".$row['InqID']."&status=status&page=".$pageid."' onclick='return confirmAction()'>Pending</a>";

                                                }else
                                                {
                                                    echo "Finished";
                                                }
                                            ?>
                                        </td>
                                        <td>
                                            <input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/> 
                                        </td>
                                    </tr>
                                    <?php
                                        }
                                    ?>
                                    <tr>
                                        <td colspan="10">
                                           <?php echo $pager->show(); ?>
                                        </td>
                                    </tr>      
                                </table>

In this Program i get value from database in my selection box. show this image 在此处输入图片说明

In first selection box i got perfect out put in my JavaScript but other one selection box value is null or empty Here is my JavaScript and Ajax code.

function smsNgo(birdno,address,birdname,city,area,name,contact)
{
//document.write(mobile1);  
var ngocontact = document.getElementById("volunteerSelect1").value;
//var ngocontact = selectElement.options[selectElement.selectedIndex].value;

var xmlhttp;
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
   {
        //document.getElementById("smsStatus").innerHTML=xmlhttp.responseText;; 
    window.open("http://localhost/goal_bird/admin/edit.php","_self");

   }
  }
 var url="http://www.****.co.in/sendsms.aspx?mobile=******&pass=******&    senderid=SMSIdea&to="+ngocontact+"&msg=Bird id is "+birdno+" "+birdname+" "+name+"     "+address+" "+area+" "+city+" "+contact;
document.write(url);
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
setTimeout("location.href='index.php'",2000);
}

You can make the volunteerSelect1 name unique by adding some db unique id with it. May be InqID in this case?

<select name="volunteerSelect_<?php echo $row['InqID']; ?>" 
    id="volunteerSelect_<?php echo $row['InqID']; ?>">

From a glance, i can see the name="" for all the <select> tags are the same... "volunteerSelect1"

try using something like volunteerSelect[id] or volunteerSelect[]

The SAME thing applies to the id="" attribute, they are all using the same ID, this is used by javascript to identify a particular DOM item

However you cant use [] in the id try volunteerSelect_ID

That's probably the problem, the ID attribute

modify this <select name="volunteerSelect1" id="volunteerSelect1"> to this

<select name="volunteerSelect1" id="volunteerSelect<?php echo $row['InqID']; ?>">

then this <input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/>

to this <input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $row['InqID']; ?>,<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/>

then this function smsNgo(birdno,address,birdname,city,area,name,contact) to this function smsNgo(inqid,birdno,address,birdname,city,area,name,contact)

and finally this var ngocontact = document.getElementById("volunteerSelect1").value;

to this var ngocontact = document.getElementById("volunteerSelect" + inqid).value;

Basically, what I did that i added an unique identifier to each selectbox according to inqid. Then I call function smsNgo with this ID and select the right selectbox :)

Since you tagged this as jquery. so to get the value from select dropdown, you will use something like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM