PHP - getting all the data corresponding to the switch

I use php and have less experience with it. I show the data in a table that has a switch at the beginning of each row. I want to get all the data that is on the line corresponding to the switch that I am checking. Here is what I have done so far:

<form name="test" method="POST">
        <table>
            <tr>
                <th></th>
                <th>book_id</th>
                <th>card no</th>
                <th>fname</th>
                <th>lname</th>
            </tr>
        
Run code
<?php
            $conn = mysql_connect("localhost", "root", "");
            if ($conn) {
                mysql_select_db("library");
            } else {
                echo 'no such database';
            }

$query_test = "select book_id2,a.card_no,fname,lname from book_loans a, borrower b where a.card_no = b.card_no ";

            $result = mysql_query($query_test);

            if ($result === FALSE) {
                mysql_error();
            } else {

                while ($rows = mysql_fetch_assoc($result)) {
                    echo "<tr>";
                    echo "<td><input type='radio' name='test'></td>";
                   echo "<td>" .$rows['book_id2']. "</td>";
                   echo "<td>" .$rows['card_no']. "</td>";
                    echo "<td>" .$rows['fname']. "</td>";
                    echo "<td>" .$rows['lname']. "</td>";

                    echo "</tr>";
                }
            }

       ?>

 <input type="submit" name="test_val" value="submit"/>
        </table>
        </form>
Run code

Here I want to print the data, i.e. book_id, card_no, fname, lname when I click the submit button:

<?php

if($_POST)
            {
                if(isset($_POST['test_val']))
                {
                 // TO PRINT THE DATA CORRESPONDING TO THE RADIO BUTTON      
                }
            }
?>
+4
source share
3 answers

Add hidden input fields to each line <td>. For instance:

echo "<td>".$rows['book_id2']."</td><input type='hidden' name ='book_id2' value='".$rows['book_id2']."' />";

I tried writing JavaScript, but it is much easier in jQuery. Add these scripts to the end of the closing tag </body>.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
 <script>
    var toggleInput = function(){
        $('input[type=hidden]').prop('disabled', true);
        var selection = $('input:checked').eq(0);
            selection.parent().parent().find('input[type=hidden]').each(function(){
               $(this).prop('disabled',false);
            });
        }
        $(form).on('submit',toggleInput);
 </script>

JQuery DOM, . , JavaScript. , - JavaScript, !

. , .

, , . JavaScript JQuery - .

+2

value

echo "<input type='radio' name='test' value='".$rows['book_id2']." ".$rows['card_no']." ".$rows['fname']." ".$rows['lname']."'>";

post

if(isset($_POST['test']))
{
    echo $_POST['test'];
}

: :

echo "<input type='radio' name='test' value='".addslashes( json_encode($rows) )."'>";

if(isset($_POST['test']))
{
    $result = json_decode( stripslashes($_POST['test']) ) ;
    echo $result->book_id2;
    echo $result->card_no;
    echo $result->fname;
    echo $result->lname;
}
+1

:

  • ( , )

  • () <tr> <td> , JS, WRT , , .

  • Send the data and you can capture the necessary data.

Functions you require (built-in and user-defined):

1 $( "#tr_unique td_unique" )[.text()][1]; // text() Since you are storing data in td else it would be val()
2 insertData(number){
// Here number is the row # through which you'll track corresponding data
 }
3$( "#hidden_textarea" )[.val][1]('data')

See if this helps.

0
source

Source: https://habr.com/ru/post/1598861/


All Articles