将输入数组中的特定输入值发送到JavaScript

时间:2012-04-26 13:12:04

标签: php javascript

我使用查询通过循环填充消息列表,这是我的代码:

<?php
$sql_i_msg_sent_waiting="SELECT t1.i_message_id,t2.username,t2.name,t2.propic,t2.age,t2.dob,t3.religion,t3.caste 
FROM candidate_i_message as t1, candidate_register_table as t2, candidate_social_table as t3 WHERE t1.from_username='$_SESSION[logged_user]' AND t1.to_username=t2.username AND t2.username=t3.username AND t1.status='0'";
$result_i_msg_sent_waiting=mysql_query($sql_i_msg_sent_waiting,$con);
$count=mysql_num_rows($result_i_msg_sent_waiting);
echo $count;

?>



<div id="section_i_message_sent_waiting" style="width:650px; overflow:auto;">
         <h2>Awaiting Sent Request</h2> 
          <?php
          if(mysql_num_rows($result_i_msg_sent_waiting)==0)
           {
            ?>
            <div style="width:650px; border-bottom:1px solid #CCCCCC" align="center">
            <table border='0' cellspacing='0' cellpadding='0' width='550px' align='center'>
            <tr style='padding-botton:5px; border-bottom:1px solid #CCCCCC'>
            <td width='50px'/>
            <td><FONT COLOR=red FACE='Geneva, Arial' SIZE=2>
                       No Messages Found.</FONT></td>
                    <td width='50px'/></tr></table>
            </div>      

         <?php
         }
         else
         {
         while($row_i_msg_sent_waiting=mysql_fetch_array($result_i_msg_sent_waiting))
         {
         ?>
         <div style="width:650px; border-bottom:1px solid #CCCCCC" align="center">
           <table width="550px" cellpadding="0" cellspacing="0" border="2" align="center" style="vertical-align:middle">
           <tr style='vertical-align:middle'>
           <td style='vertical-align:middle' width="100px">
           <form method='post' action='id.php' name='showid' id='showid'>
           <input type='hidden' name='pro_username' id='pro_username' 
             value="<?php echo $row_i_msg_sent_waiting['username'];?>"/>
                     <input type='image' src='<?php echo $row_i_msg_sent_waiting['propic'];?> ' style="width:100px; vertical-align:middle"/>
         </form>
           </td>
           <td style='vertical-align:middle;border-right:1px solid #CCCCCC;border-top:1px solid #CCCCCC' width="450px" >
               <table width="450px" cellpadding="0" cellspacing="0" border="0">
                 <tr>
                 <td width="10px"/>
                 <td width="100px" align="left">Name</td>
                 <td width="10px">:</td>
                 <td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['name'];?>
                 </td>
                 <td width="10px">
                 <td width="100px" align="left">Age</td>
                 <td width="10px">:</td>
                 <td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['age'];?>
                 </td>
                 <td width="10px"/>
                 </tr>
                  <tr>
                 <td width="10px"/>
                 <td width="100px" align="left">Date Of Birth</td>
                 <td width="10px">:</td>
                 <td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['dob'];?>
                 </td>
                 <td width="10px">
                 <td width="100px" align="left">Religion</td>
                 <td width="10px">:</td>
                 <td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['religion'];?>
                 </td>
                 <td width="10px"/>
                 </tr>
                 <tr>
                 <td width="10px"/>
                 <td width="100px" align="left">Caste</td>
                 <td width="10px">:</td>
                 <td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['caste'];?>
                 </td>
                 <td width="10px">
                 <td width="100px" align="left">Religion</td>
                 <td width="10px">:</td>
                 <td width="100px" align="left"><?php echo $row_i_msg_sent_waiting['religion'];?>
                 </td>
                 <td width="10px"/>
                 </tr>
                 <tr>
                 <td width="10px"/>
                 <td width="100px" align="left">Action</td>
                 <td width="10px">:</td>
                 <td width="100px" align="left">
                 <form method="post" name="cancel_request_form"  id="cancel_request_form"
                 action="javascript:cancel_request(document.getElementById('cancel_request_form'));">
                 <input type="text" name="no_of_msg" id="no_of_msg" value="<?php echo $count;?>"/>
                    <input type="text" name="cancel_request[]" id="cancel_request[]" 
                    value="<?php echo $row_i_msg_sent_waiting['i_message_id'];?>"/>
                    <input type="submit" name="cancel" id="cancel" class="button" style="width: 100px" value="Cancel Request"/>
                    </form>
                 </td>
                 <td width="10px">
                 <td width="100px" align="left"></td>
                 <td width="10px">:</td>
                 <td width="100px" align="left">
                 </td>
                 <td width="10px"/>
                 </tr>
                             </table>
           </td>
           </tr>

           </table>
         </div>
         <?php 
         } 
         }?>

</div>

<input type="text" name="cancel_request[]" id="cancel_request[]" 
                    value="<?php echo $row_i_msg_sent_waiting['i_message_id'];?>"/>

                creates a array of input type text, and each input text contains corressponding message_id...since each input text is contained within a form, and since the form is within a loop, the form is also repeatative..when the submit button will be clicked corressponding to a particular form, the input type text[] within that form will fire the value to get accepted in the javascript i give below...

<script>
function cancel_request(obj) {

alert(document.getElementByName('cancel_request[]').value);
}
</script>

但脚本显示问题,它根本不起作用。我不知道出了什么问题。它没有显示任何东西。有message_id 1和2:一个文本类型input []包含值1,另一个值2。

当我点击对应于值1的提交按钮时,该文本类型输入的值将被发送到JavaScript并显示。我的剧本有什么问题?

1 个答案:

答案 0 :(得分:1)

函数是getElementsByName - 复数形式的元素。然后,您可以像数组一样访问它。