SRLA
SRLA

Reputation: 21

Jquery ajax to update mysql table

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
}
?>

Upvotes: 1

Views: 2808

Answers (1)

Sachin PATIL
Sachin PATIL

Reputation: 745

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>

Upvotes: 1

Related Questions