【问题标题】:Trying to get multiple radio buttons passed through form试图让多个单选按钮通过表单
【发布时间】:2018-08-06 21:33:18
【问题描述】:

我有一个 while 循环打印多个复选框..我将它们更改为复选框而不是单选按钮..现在我要做的就是将所有这些复选框的名称传递给我的 vote.php 文件。如果我在我的循环中给我的复选框一个简单的名称并将其转移到处理我所有 POST 数据的vote.php,它只会继承我最后的选择。我想要我的所有选择。我为你们整理了一些代码。

告诉我这里哪里出错了..这是我打印按钮的初始代码..

        while($row_nominee=mysql_fetch_array($result_nominee)){
        $id = $row_nominee[0];
        //print "$level";
        $prefix = $row_nominee[1];
        $fname = $row_nominee[2];
        $lname = $row_nominee[3];
        $suffix = $row_nominee[4];
        $city = $row_nominee[5];
        $state = $row_nominee[6];
        $zip = $row_nominee[7];
        $bio = $row_nominee[8];
        $level = $row_nominee[10];
        $name = $prefix . " " . $fname . " " . $lname;
        $address = $city . " " . $state . " " . $zip;
        //print "$voted";

            print "<tr>";
            print "<td width=\"4\" valign=\"top\"><input type=\"checkbox\" name=\"candidateOne\" id=\"candidate\" value=$id></td>";
        print "<td valign=\"top\"><FONT face=Tahoma,Arial,Helv size=-1><b>Name:</b> <font color=\"#ff0000\">$name</font><br><b>Hometown:</b> $address<br><b>Bio:<br /></b> $bio</font></td>";
        print "</tr>";


    }

    ?>
    //now here is my vote.php file which handles the checkboxes.
//get the contents from the vote ballot Form
$voter_id = safeEscapeString(qsrequest(voter));
$candidate_id = safeEscapeString(qsrequest(candidateOne));

//print "$voter_id and $candidate_id";
include '../../sql/usagym_connection.php';

if(qsrequest(correct))
{
    $voter_id1= safeEscapeString(qsrequest(voter1));
    $candidate_id1= safeEscapeString(qsrequest(candidate1));
    $votes1= safeEscapeString(qsrequest(votes1));
    $votes1 += 1;

    $sql_voter = "update stateChair_voters set voted='Y' where (usagnum='$voter_id1')"; 
    //print "$sql_voter<br>";
    $result_voter = mysql_query($sql_voter, $link) or die("Invalid query2");


    $update_candidate = "update stateChair_nominees set votes=$votes1 where (id=$candidate_id1)";
    //print "$update_candidate<br>";
    $result_update = mysql_query($update_candidate, $link) or die("Invalid query3");

    //print "Total votes is $votes1.";
    header( "Location: vote_thanks.html");
    exit;
}

else
{

    //connect the database

    $sql_candidate = "select id, prefix, fname, lname, suffix, city, state, zip, bio, votes from stateChair_nominees where id=$candidate_id";
    $result_candidate = mysql_query($sql_candidate, $link) or die("Invalid query1". mysql_error());

    while($row_candidate=mysql_fetch_array($result_candidate)){
        $id = $row_candidate[0];
        $prefix = $row_candidate[1];
        $fname = $row_candidate[2];
        $lname = $row_candidate[3];
        $suffix = $row_candidate[4];
        $city = $row_candidate[5];
        $state = $row_candidate[6];
        $zip = $row_candidate[7];
        $bio = $row_candidate[8];
        $votes = $row_candidate[9];

        $name = $prefix . " " . $fname . " " . $lname;
        $address = $city . " " . $state . " " . $zip;

    }

?>

我真正想做的就是让多人参与投票,而不仅仅是一个人。想法?谢谢大家!

这是我的复选框代码..

print "<td width=\"4\" valign=\"top\"><input type=\"checkbox\" name=\"candidateOne\" id=\"candidate\" value=$id></td>";

现在这里是处理这些复选框的代码。我没有编写此代码,我必须对其进行调试,因此感谢您的帮助。

$candidate_id = safeEscapeString(qsrequest(candidateOne));

这段代码现在处理的是一个字符串,而不是一个变量。让一个变量代表另一个文件上的多个复选框同时在这里记录它们的过程是什么?

【问题讨论】:

  • 这个问题有点令人困惑,因为您显示的是所有服务器端代码,而没有关于单选按钮。如果您尝试从表单输入中收集多个人,请使用复选框而不是单选按钮。
  • 请您整理一下您的源代码以便阅读。
  • 抱歉,我稍微清理了一下。

标签: php radio-button


【解决方案1】:
 print "<td width=\"4\" valign=\"top\"><input type=\"radio\" name=\"candidateOne\" id=\"candidate\" value=$id></td>";

您必须更改“名称”,因为您已通过变量更改循环中的“值”。

【讨论】:

  • 每次使用变量时如何给它一个不同的名称值?我只是对 $candidate 将被设置为什么感到困惑?
猜你喜欢
  • 2016-10-28
  • 2011-10-29
  • 1970-01-01
  • 2023-02-19
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2013-07-12
  • 1970-01-01
相关资源
最近更新 更多