1

in my program there is mysql table 'company'.companyid and activation_code are some of columns in that table.i show that table values in a html table where includes comapproval.php page.that html table each row has a accept button and when i click that button company table should be updated and that row remove from html table.update code is in 'approvecompany.php' page.i used jquery ajax for this.when i cliick accept button i get only success alert.but i could not do updaet table.even sucessfull alert get only for first row if html table.i am new for ajax.help me to solve this.

comapproval.php

<table class="table table-striped table-bordered table-list">
                    <thead>

                    <tr>
                        <th>Action</th>
                        <th>ID</th>
                        <th>Registration number</th>
                        <th>Company Name</th>
                        <th>Email</th>
                    </tr>
                    </thead>
                    <tbody>

                    <?php
                    $conn = mysqli_connect("localhost", "root", "", "internship");
                    if (mysqli_connect_errno()) {
                        echo "Failed to connect to MySQL: " . mysqli_connect_error();
                    }
                    $sql = "";

                    $sql = "select * from company where activation_code=0";

                    $res = mysqli_query($conn, $sql);

                    while ($row = mysqli_fetch_assoc($res)):

                    ?>

                    <tr>
                        <td align="center"><input type="submit" class="btn btn-default" value="Accept" id="accept" name="accept"></input></td>
                        <td><?php echo $row['companyid']; ?></td>
                        <td><?php echo $row['government_reg_no']; ?></td>
                        <td><?php echo $row['company_name']; ?></td>
                        <td><?php echo $row['email']; ?></td>
                    </tr>

                    <?php
                        endwhile
                    ?>

                    </tbody>
                </table>
<script>
            $("#accept").click(function () {
                $.ajax({
                   type:"POST",
                   url:"approvecompany.php",
                    data:{comid:$('<?php $row['companyid']?>').val()},
                    success:function () {
                        alert('Successfully approved');
                        window.location.reload(true);
                    }

                });
            });

    </script>

approvecompany.php

<?php
$conn=mysqli_connect("localhost","root","","internship");

$comid=$_POST['comid'];

if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="update company set activation_code=1 where companyid=$comid";

if ($conn->query($sql) === TRUE) {

?>

<?php
}

else{
?><script>alert("Error...")</script><?php
}
?>
SRLA
  • 21
  • 2
  • 6
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 13 '17 at 20:01
  • [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Apr 13 '17 at 20:01
  • [ID's Must Be Unique](http://stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-eleme), specifically because it will cause problems in [JavaScript](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) and CSS when you try to interact with those elements. If you have multiple rows whose `id="accept"` things will not work as planned. Change that to a class. – Jay Blanchard Apr 13 '17 at 20:02
  • if i jquery code use inside while loop is it incorrect?? – SRLA Apr 13 '17 at 20:08
  • You can use jQuery code inside a while loop. – Jay Blanchard Apr 13 '17 at 20:09
  • i used but same result – SRLA Apr 13 '17 at 20:10
  • how should i change my code to make my work success?? – SRLA Apr 13 '17 at 20:11
  • `data:{comid:$('').val()}` will not work – Jay Blanchard Apr 13 '17 at 20:12
  • yeah it wil not work – SRLA Apr 13 '17 at 20:14

1 Answers1

1

Try Rewriting comapproval.php as below:

<table class="table table-striped table-bordered table-list">
    <thead>
        <tr>
            <th>Action</th>
            <th>ID</th>
            <th>Registration number</th>
            <th>Company Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>

    <?php
        $conn = mysqli_connect("localhost", "root", "", "internship");
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        $sql = "";

        $sql = "select * from company where activation_code=0";

        $res = mysqli_query($conn, $sql);

        while ($row = mysqli_fetch_assoc($res)):

    ?>

        <tr>
            <td align="center">
                <button type="submit" class="btn btn-default myButton" value="<?php echo $row['companyid']; ?>" id="accept" name="accept">Accept</button>
            </td>
            <td><?php echo $row['companyid']; ?></td>
            <td><?php echo $row['government_reg_no']; ?></td>
            <td><?php echo $row['company_name']; ?></td>
            <td><?php echo $row['email']; ?></td>
        </tr>

    <?php
        endwhile
    ?>

    </tbody>
</table>
<script>
    $(".myButton").click(function () {
        var company_id = $(this).val();
        $.ajax({
            type:"POST",
            url:"approvecompany.php",
            data:{ comid: company_id },
            success:function () {
                window.location.reload(true);
            }

        });
    });
</script>
Sachin PATIL
  • 745
  • 1
  • 9
  • 16
  • 1
    100% works.thank u very much for you contribution Sachin PATIL....you are a great friend... – SRLA Apr 13 '17 at 20:39