简体   繁体   English

使用 while 循环将所选选项作为 php 值发送

[英]Sending selected option as php value using a while loop

What i am trying to do: I have a while loop that lists every user from mysql database with a dropdown select option in front of each and a button in front in the last column, the goal is to select a value from the dropdown and submit with button to a php script that inserts the value you selected for the specific your.我正在尝试做什么:我有一个while循环,列出mysql数据库中的每个用户,每个用户前面都有一个下拉select选项,最后一列前面有一个按钮,目标是Z99938282F04071859941E下拉列表1671859941E使用 php 脚本的按钮插入您为特定您选择的值。

what i have tried: I used while loop to fill the table and some jquery to add the selected option on the url before moving to the script page like below我已经尝试过:我使用 while 循环来填充表格和一些 jquery 在 url 上添加选定的选项,然后再转到脚本页面,如下所示

    <?php 
while ($a = mysqli_fetch_array($result0)){
                                                                
$id=$a['id'];
$fname = $a['fname'];
$lname = $a['lname'];

$a = 5;
$b = 10;
$c = 20;
$d = 25;
                                                                


$sql1 = "SELECT * FROM users where id =$ref";
$result1 = $conn->query($sql1);
$row1 = $result1->fetch_assoc();

$ref_name = $row1['fname'] ." ". $row1['lname'] ;

                                                            
    echo "<tr>
    
    <td class='h5 text-white text-center d-none d-sm-table-cell'>$id</td>
    <td class='h5 text-white font-w600'>$fname ". $lname."</td>
     <td>
    <select id='rank' name='rank'>
    <option value='$a'>Attended Seminar (5pts)</option>
    <option value='$b'>Inspection Trip (10pts)</option>
    <option value='$c'>No Chilled(20pts)</option>
    <option value='$d'>Test Activity (25pts)</option>
    </select>
    </td>
    "?>
    <td class='h5 text-white text-center'>
    <div class='btn-group'>
     <a class='btn-rank' href="../actions/update_rank.php?id=<?php echo $id ?>&rank="><button type='button' class='btn btn-sm btn-success js-tooltip-enabled' data-toggle='tooltip' title='' data-original-title='Verify'>
     <i class='fa fa-check'></i>
    </button>
    </div>
                                                                        
    </td>
    
    </tr>";
     }
    ?>      

the js i use to attach the option with我用来附加选项的js

$(function () {
var a = $('a.btn-rank');
a.attr('href', a.attr('href') + $('#rank').val())
 });

so far i get just the first value which is $a and the first user on the table, will appreciate any assistance with direction on how to proceed with this.到目前为止,我只得到了第一个值 $a 和桌面上的第一个用户,将感谢任何有关如何进行此操作的指导的帮助。 thanks in advance提前致谢

Lots of syntax and format errors with PHP. PHP 存在大量语法和格式错误。 try the below code.试试下面的代码。

<?php 
    while ($a = mysqli_fetch_array($result0)){
                                                                
        $id    = $a['id'];
        $fname = $a['fname'];
        $lname = $a['lname'];

        $a = 5;
        $b = 10;
        $c = 20;
        $d = 25;

        $sql1 = "SELECT * FROM users where id =$ref";
        $result1 = $conn->query($sql1);
        $row1 = $result1->fetch_assoc();

        $ref_name = $row1['fname'] ." ". $row1['lname'] ;

            ?><tr><?php                                                       
                echo 
                '
                <td class="h5 text-white text-center d-none d-sm-table-cell">'.$id.'</td>
                <td class="h5 text-white font-w600">'.$fname.' '. $lname.'</td>
                <td>
                    <select id="rank" name="rank">
                        <option value='.$a.'>Attended Seminar (5pts)</option>
                        <option value='.$b.'>Inspection Trip (10pts)</option>
                        <option value='.$c.'>No Chilled(20pts)</option>
                        <option value='.$d.'>Test Activity (25pts)</option>
                    </select>
                </td>
                ';
                ?>
                <td class="h5 text-white text-center">
                    <div class="btn-group">
                        <a class="btn-rank" href="../actions/update_rank.php?id=<?php echo $id ?>&rank="><button type="button" class="btn btn-sm btn-success js-tooltip-enabled" data-toggle="tooltip" title="" data-original-title="Verify"><i class="fa fa-check"></i></button></a>
                    </div>                                                    
                </td>
            </tr>
    }
?>

$(document).ready(function(){
    var href =  $('.btn-rank').attr('href');
    $('.btn-rank').attr('href',href + $('#rank').val() );

    $('#rank').change(function(){
        $('.btn-rank').attr('href',href + $(this).val() );
    });  
});

Try the below snippet.试试下面的代码片段。

 $(document).ready(function(){ var href = $('.btn-rank').attr('href'); $('.btn-rank').attr('href',href + $('#rank').val() ); $('#rank').change(function(){ $('.btn-rank').attr('href',href + $(this).val() ); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="rank" name="rank"> <option value="Attended Seminar (5pts">Attended Seminar (5pts)</option> <option value="Inspection Trip (10pts)">Inspection Trip (10pts)</option> <option value="No Chilled(20pts)">No Chilled(20pts)</option> <option value="Test Activity (25pts)">Test Activity (25pts)</option> </select> <a class="btn-rank" href="../actions/update_rank.php?id=12345&rank=" >Button</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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