Prayag
Prayag

Reputation: 211

display Value from database based on Checkbox checked on the same form

I am trying to populate the value of the first name from the database based on the checkbox checked.

below is the form , which populates all the table data added in database. by selecting anyone of checkbox and click on update, it should display an alert displaying the first name which is queried from database of that row-id and then redirect to another page

i tried using method=get in form.

Right now I am stuck on how to fix the following error that I'm getting:

<Undefined index: users on line 98>

here is my code

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>
   <style>
        html {
            background-image: url("WDF_2493417.jpg");
            background-size: 100% 100%;
            background-repeat: no-repeat;
            background-position: center;
        }

        html {
            margin: 0;
            padding: 0;
            height: 100%;
        }
    </style>
</head>
<body>
<?php
    // php populate html table from mysql database
    $conn = mysqli_connect("localhost","root","");
    mysqli_select_db($conn, "my_db");
    $result = mysqli_query($conn,"SELECT * FROM ijob ORDER BY userId DESC");
?>

    <div class="form-style-1">
        <form name="frmUser" method="post" action="">
            <div style="width:1200px;">
                <table border="0" cellpadding="10" cellspacing="1" width="1200" class="tblListForm" align="center">
                    <tr class="listheader">
                        <td></td>
                        <th>first name</th>
                        <th>last name</th>
                        <th></th>
                    </tr>
                    <?php
                        $i=0;
                        $j=1;
                        while($row = mysqli_fetch_array($result)) {
                            if($i%2==0){
                                $classname="evenRow";
                            } else {
                                 $classname="oddRow";
                            }
                    ?>
                            <tr class="<?php if(isset($classname)) { echo $classname; }?>">
                                <td>
                                    <input type="checkbox" name="users" value="<?php echo $row["userId"]; ?>" >
                                </td>
                                <td><?php echo $row["fname"]; ?></td>
                                <td><?php echo $row["lname"]; ?></td>
                                <th colspan="4">
                                    <input type="button" name="update" class="button" value="Apply" onClick="checkAddress(this);" />
                                </th>
                            </tr>
                    <?php 
                            $i++;
                            $j=$i+1;
                        }
                        $conn->close();
                    ?>
                </table>
            </div>
        </form>
    </div>
</body>
<script>
    function checkAddress()
     {
        if (document.querySelector('form[name="frmUser"] input[type="checkbox"]:checked')) {

            if(!empty($_POST['users'])){
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "my_db";

                // Create connection
                $conn = new mysqli($servername, $username, $password, $dbname);
                // Check connection
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                } 
                $num_var= " ";
                $num_var = mysqli_real_escape_string($conn,$_POST['users']); // getting error on this Line.

                $query = "SELECT fname FROM ijob WHERE userId='$num_var'";
                $result = mysqli_query($conn,$query);
                if (!$result) {
                    echo 'MySQL Error: ' . mysqli_error($conn);
                    exit;
                }

                if ($result->num_rows > 0) {
                    // output data of each row
                    while($row = $result->fetch_assoc()) {
                        $fnames[] = $row['fname'];
            ?>
            alert("Hello <?php echo $fnames;?>");
            <?php
                    }
                }
                $conn->close();

            ?>
            }

            document.frmUser.action = "form.php";
            document.frmUser.submit();
        } else {
            alert("Pl select checkbox which You wish to Apply ");
        }
    }
</script>
</html>

Upvotes: 0

Views: 1726

Answers (1)

Tom Udding
Tom Udding

Reputation: 2294

Your PHP opening and closing tags within your <script></script> are not in the right place. 1. You have a PHP opening tag (<?php) after this if-statement

if(!empty($_POST['users'])){
   <?php

it should be before the if-statement

<?php
if(!empty($_POST['users'])){
  1. Your PHP closing tag (?>) is before the last }

    ?>
    }

Upvotes: 1

Related Questions