将整行的无线电选定值的值获取到下一个文件

时间:2016-10-21 07:17:34

标签: php html mysql

我已经显示了mysql表中的值以及具有不同值的单选按钮。在提交按钮上,它应重定向到下一页并显示所选值。我在第一个文件上的代码如下:

$query1 = "select * from booking";
$result1 = $connection->query($query1);
echo "<form method='post' action='edit.php'>";
echo "<center>";
echo "<table border='1'>";
$count = 0;
while($row = $result1->fetch_row())
{
    echo "<tr>";
    echo "<td width=".'10'.">";
    $count = $count + 1;
    #$sr=$sr+1;
    echo "<input type='radio' name='select' value='".$count."' />";
    #$row = mysqli_fetch_row($result1);
    #echo "<tr>";
    echo "<td width=".'10'.">";
    $rooms = $row[0];
    $srrooms = $rooms;
    echo "<input type='text' name='srrooms' value='".$srrooms."'     hidden='hidden'>";
    #echo $srrooms;
    echo $rooms;
    echo "</td>";
    echo "<td width=".'10'.">";
    $no_roomss = $row[1];
    $nosrrooms = $no_roomss;
    echo "<input type='text' name='nosrrooms' value='".$nosrrooms."' hidden='hidden'>";
    echo $no_roomss;
    echo "</td>";
    echo "<td width=".'10'.">";
    $no_dayss = $row[2];
    $nodays = $no_dayss;
    echo "<input type='text' name='nodays' value='".$nodays."' hidden='hidden'>";
    echo $no_dayss;
    echo "</td>";
    echo "<td width=".'10'.">";
    $name = $row[3];
    echo $name;
    echo "</td>";
    echo "</tr>";
    #$count++;
}
echo "<input name='count' type='hidden' id='count'>"; // hidden input where     counter value will be stored
echo "</table>";
echo "<input type='submit' name='submitRooms' />";
echo "</center>";
echo "</form>";

javascript代码是:                            $(函数(){         $( '输入[名称= “选择”]')。改变(函数(){             $( '#计数')VAL($( “输入[名称= '选择']:检查”)VAL()。)。         });     });     

edit.php代码是:

<?php
if(isset($_POST['submitRooms'])){
    @$count = $_POST['count'];
    @$srrooms=$_POST['srrooms'.$count];
    @$nosrrooms=$_POST['nosrrooms'.$count];
    @$nodays=$_POST['nodays'.$count];
    echo 'Selected values are: '. $srrooms . $nosrrooms . $nodays;
}
?>

1 个答案:

答案 0 :(得分:0)

您可以使用计数器将选定的radiobutton行的值提交为:

<?php 
$query1 = "select * from booking";
$result1 = $connection->query($query1);
echo "<form method='post' action='edit.php'>";
echo "<center>";
echo "<table border='1'>";

$count = 0; // counter variable
while($row = $result1->fetch_row())
{
    echo "<tr>";
    echo "<td width=".'10'.">";

    echo "<input type='radio' name='select' value='".$count."' />";
    echo "<td width=".'10'.">";
    $rooms = $row[0];
    $srrooms = $rooms;

    echo "<input type='hidden' name='srrooms".$count."' value='".$srrooms."' >"; // add counter value to every name of the input fields
    echo $rooms;
    echo "</td>";
    echo "<td width=".'10'.">";
    $no_roomss = $row[1];
    $nosrrooms = $no_roomss;
    echo "<input type='hidden' name='nosrrooms".$count."' value='".$nosrrooms."'>";
    echo $no_roomss;
    echo "</td>";
    echo "<td width=".'10'.">";
    $no_dayss = $row[2];
    $nodays = $no_dayss;
    echo "<input type='hidden' name='nodays".$count."' value='".$nodays."' >";
    echo $no_dayss;
    echo "</td>";
    echo "<td width=".'10'.">";
    $name = $row[3];
    echo $name;
    echo "</td>";
    echo "</tr>";
    $count++;
}
echo "<input name='count' type='hidden' id='count'>"; // hidden input where counter value will be stored
echo "</table>";
echo "<input type='submit' name='submitRooms' />";
echo "</center>";
echo "</form>";
?>

然后使用jquery保存隐藏计数输入字段中的活动radiobuttons计数[在上面的php代码之后]

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type='text/javascript'>
$(function(){
    $('input[name="select"]').change(function(){
        $('#count').val($("input[name='select']:checked").val());
    });
});
</script>

然后在您的edit.php上获取发布的数据:

<?php
if(isset($_POST['submitRooms'])){
    @$count = $_POST['count'];
    @$srrooms = $_POST['srrooms'.$count];
    @$nosrrooms = $_POST['nosrrooms'.$count];
    @$nodays = $_POST['nodays'.$count];
    echo 'Selected values are: '. $srrooms . $nosrrooms . $nodays;
}
?>

您没有添加整个表单代码,因此请添加上面提到的表单代码以及代码无效的原因是您没有使用输入字段名称添加计数。并且他们没有使用在edit.php中获取带有计数的已发布数据,直到您没有以$ count变量命名的输入字段

echo "<input type='hidden' name='srrooms".$count."' value='".$srrooms."' >";