Update Database from Javascript onClick By Ajax PHP - Stack Overflow

Hi i want to update id row when a user click the button below please help and sorry for my bad englishi

Hi i want to update id row when a user click the button below please help and sorry for my bad english

index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>

<body>

<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a>
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a>
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a>

</body>
</html>

update.php

<?php 
include('database_connection.php');

$update = "UPDATE id SET id = id + 1 WHERE id = updateId";

if (mysqli_query($connect, $update)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($connect);
}
?>

Hi i want to update id row when a user click the button below please help and sorry for my bad english

index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>

<body>

<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a>
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a>
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a>

</body>
</html>

update.php

<?php 
include('database_connection.php');

$update = "UPDATE id SET id = id + 1 WHERE id = updateId";

if (mysqli_query($connect, $update)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($connect);
}
?>
Share Improve this question asked Apr 18, 2016 at 10:14 AlexAlex 1,4795 gold badges15 silver badges16 bronze badges 2
  • You need to look at ajax javascript calls (look at jquery for simplicity) and a way to read data in php (look at $_GET[''] or $_POST['']) – Dean Meehan Commented Apr 18, 2016 at 10:25
  • You've identified that you need to use JavaScript / Ajax, but there i no JavaScript in the question. Start by looking for an introductory Ajax tutorial. – Quentin Commented Apr 18, 2016 at 10:27
Add a ment  | 

1 Answer 1

Reset to default 2

In your index.php file

<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<a href="javascript:void(0)" onClick="updateId('1')">Id 1</a>
<a href="javascript:void(0)" onClick="updateId('2')">Id 2</a>
<a href="javascript:void(0)" onClick="updateId('3')">Id 3</a>

</body>
</html>

<script>
function updateId(id)
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
        {
            alert(xmlhttp.responseText);
        }
    };
    xmlhttp.open("GET", "update.php?id=" +id, true);
    xmlhttp.send();
}
</script>

And in your update.php

<?php 
if(!empty($_GET['id']))
{
    $id = $_GET['id'];
    include('database_connection.php');
    
    $update = "UPDATE id SET id = id + 1 WHERE id = ?";
    
    $connect->execute_query($update, [$id]);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744771370a4592778.html

相关推荐

  • Update Database from Javascript onClick By Ajax PHP - Stack Overflow

    Hi i want to update id row when a user click the button below please help and sorry for my bad englishi

    16小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信