php - 如果我单击选择按钮并将其值传递到文本框中,如何获取所选单选按钮的值?

标签 php javascript

 //this is saved in president.php
if ($result = $mysqli->query("SELECT * FROM candidate_info WHERE position= 'president'"))
                    {
     // display records if there are records to display
       if ($result->num_rows > 0)
                            {
          // display records in a table
           echo "<table border='1' cellpadding='10'>";

              // set table headers

          echo "<tr><th>Student ID</td><th>Course</th><th>Name</th><th></th></tr>";

  while ($row = $result->fetch_object())
             {
        // set up a row for each record
   echo "<tr>";
    echo "<td>" . $row->studid . "</td>";
    echo "<td>" . $row->course . "</td>";
    echo "<td>" . $row->fname . " ". $row->mname ." ". $row->lname ."</td>";
echo "<td><input type ='radio' name='radio'  value='". $row->studid ."'> </td>";
    echo "</tr>";
                               }


  echo "<td> <input type='button' name='button' value='select' onclick='". get_radio_value() ."'></td>";

    echo "</table>";
     }

     else {
        echo "No results to display!";
                            }
      }

//这是我的 javascript,用于读取并获取我的 radio 的值

   function get_radio_value()
      {
      for (var i=0; i < document.orderform.radio.length; i++)
        {
          if (document.orderform.radio[i].checked)
            {
             var rad_val = document.orderform.radio[i].value;
             }
        }
       }

//在这里,当我单击表单内//文本框中的选择按钮时,我将显示所选单选按钮的值 ”/>

//但是当我加载页面时,出现错误

// like this Fatal error: Call to undefined function get_radio_value()

//这是我的voting.php的代码

      <div id="TabbedPanels1" class="TabbedPanels">
      <ul class="TabbedPanelsTabGroup">
    <li class="TabbedPanelsTab" tabindex="0">President</li>
      <li class="TabbedPanelsTab" tabindex="0">Secretary</li>
      </ul>
    <div class="TabbedPanelsContentGroup">
      <div class="TabbedPanelsContent"><p>&nbsp;</p>
   <p><?php require_once('candidate/president.php'); ?></p>
     <p>&nbsp;</p></div>

              <td width="292" valign="top">
    <form method="post" action="voting.php" >
         <table width="289" height="76" border="0" cellspacing="1">

          <tr>
            <td width="131" height="24" ><div align="right">President</div></td>
            <td width="151"><div align="left">
                <input type="text" name="president" id="radio_value"/>
            </div></td>
          </tr>


        </table>
   </form></td>

最佳答案

改变这个

echo "<td> <input type='button' name='button' value='select' onclick='". get_radio_value() ."'></td>";

这里它调用了php的get_radio_value()函数,但是该函数是用javascript编写的,所以使用下面的代码。

echo "<td> <input type='button' name='button' value='select' onclick='get_radio_value()'></td>";

用于在文本框中传递单选按钮的值

<input type="text" name="president" value="WHAT CODE SHOULD I PUT HERE?" id="radio_value"/>

更改此行

var rad_val = document.orderform.radio[i].value;

return document.getElementById('radio_value').innerHTML = document.orderform.radio[i].value;

关于php - 如果我单击选择按钮并将其值传递到文本框中,如何获取所选单选按钮的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14313233/

相关文章:

php - 无法使用 mustUnderstand 设置 SOAP header

php - 从查询中获取 10 行

javascript - 数组自动更改值

javascript - AngularJS - 无法显示来自 JSON 的内容

javascript - 即使没有任何变化,React 也会重新渲染

PHP 检查 MySQL 变量

php - Highcharts 为空白

php - 使用 GROUP BY 和 ORDER BY 条件在两个 MySQL 表上加入与联合

javascript - 从外部只读,从内部不可读取

javascript - 我正在创建一个代码来根据一天中的时间向我打招呼,由于某种原因,它无法将我的名字与其他文本区分开来