0

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>
Prayag
  • 211
  • 1
  • 13
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Tom Udding Mar 18 '17 at 08:43
  • Can you give me the URL of this website? And revert your code to the one you posted in your question? – Tom Udding Mar 18 '17 at 09:13
  • it is the localhost project. – Prayag Mar 18 '17 at 09:21

1 Answers1

1

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 }

    ?>
    }

Tom Udding
  • 2,264
  • 3
  • 20
  • 30
  • i am assigning values to chexkbox `" >` – Prayag Mar 18 '17 at 08:51
  • if the value of Users in not empty it will go to database and display the misql query result based on the value of Checkbox in the alert box. thus i have to included php – Prayag Mar 18 '17 at 08:54
  • I understand that, but your PHP code within your `` (for the alert) will not work if you misplace the PHP opening tag. – Tom Udding Mar 18 '17 at 08:54
  • if i write php before if statement, then i am getting error as `Parse error: syntax error, unexpected '{` – Prayag Mar 18 '17 at 09:08
  • how to write add alert in php code. is it like ` – Prayag Mar 18 '17 at 09:18
  • `` works and so does `` – Tom Udding Mar 18 '17 at 09:27