PHP / MySQL> Refresh field when clicking a link

I need help with a simple code that will update the (+1) field in MySQL when clicking a link on my site. This is for the "Report" button in my comments database.

So, I see how many times a comment has been “reported”. It is important that the script does not redirect or load a new page, but simply an echo of the JS message or alert.

Thanks for any help.

+3
source share
6 answers
$sql = "update `table` set `increment` = `increment` + 1 where `link` = '".$link."'";

This is just a request. You must look into jquery ajax to process the request

In some.php you need a way to read the post.

<?php
if(isset($_POST['clicked_row']))
{
    $sql = "UPDATE $row_to_update SET increment = increment + 1 WHERE id = '".$_POST['clicked_row']."'";
    mysql_query($sql);
}
?>

, ajax, jquery post, HTML - <a href="javascript:increment(row_id);">Add click</a>, jquery , some.php

+3

, . GET, URL POST; , POST.

: , , , , .. .

, , , . , . .

view.php

, , script. - - ...

<a class="flag" href="flag.php?comment=100">Flag</a>

view.php click, javascript- jQuery ( $.ajax() $.get $.post MooTools Prototype).

, JSON , , ( "" - - ?). . http://json.org/.

<script type="text/javascript">

$(document).ready(function(){
    $('a.flag').click(function(event){
        $.ajax({
            type: "GET",
            url: $(this).attr('href'),
            data: dataString,
            dataType: "json",
            success: function (data) {
                if (data.error == -1) {
                    // Not logged in, redirect to login page.
                    window.location = 'login.php';
                } else if (data.flagged == 1 && data.count != -1) {
                    // Success! With a count too.
                    alert('Comment flagged ('+data.count+' times flagged).');
                } else {
                    switch(data.error) {
                        case 1:
                            alert('Comment not found');
                        break;
                        case 2:
                            alert('Comment not flagged due to an update error.');
                        break;
                        case 3:
                            alert('Comment flagged but count not returned.');
                        break;
                        default:
                            alert('There was a general error flagging the comment.');
                        break;
                    }
                }
            },
            error: function(){
                alert('Comment not flagged; general send error.');
            }
        });

        // Call these to prevent the a tag from redirecting
        // the browser to the a-tags href url.
        event.preventDefault();
        return false;
    });
});

</script>

flag.php

flag.php - , mysql-, , , PHP, JSON. (), , :

{"flagged":1,"count":15,"error":0}

( ), , 15 (, ) .

. HTML. Javascript $.ajax(). , HTML, , . . http://www.json.org/example.html.

<?php

// Need to output JSON headers and try to prevent caching.
session_cache_limiter('nocache');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

// Our JSON array to return
//   - flagged would be 1 = success, 0 = failure
//   - count would return the # flags, with -1 no return #
//   - error code, see comments for description
$json = array('flagged'=>0,'count'=>-1,'error'=>0);

// Your logged in check code goes here 
// you don't want non-logged in people doing this

// Here, however you test to find out if someone is logged in,
// check and return a -1 error to redirect the login.    
if (!$logged_in) {
    // error -1 = not logged in, redirect browser
    $json['error'] = -1;
    exit(echo(json_encode($json)));
}

// Your mysql connection code goes here

$comment = mysql_real_escape_string($_GET['comment']);

if (empty($comment) || !is_numeric($comment)) {
    // error 1 = comment id not found
    $json['error'] = 1;
} else {
    $result = mysql_query("
UPDATE comments 
SET flags = flags+1 
WHERE commentID = $comment
");
    if (!$result) {
        $json['flagged'] = 0;
        // error 2 = update error
        $json['error'] = 2;
    } else {
        $json['flagged'] = 1;
        $count = mysql_query("
SELECT flags 
FROM comments 
WHERE commentID = $comment 
LIMIT 0, 1
");
        if ($count) {
            $query = mysql_fetch_assoc($count);
            $json['count'] = $query['count'];
        } else {
            // error 3 = updated but did not get count
            $json['error'] = 3;
        }
    }
}

echo json_encode($json);

?>
+2

.

: http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php

sql .

"UPDATE post SET flagcount = flagcount + 1 WHERE postID = {$myPostID}"
+1

, ajax, POST- php-, . - jquery?

$('a').click(function(event) {
    $.ajax({
       type: "POST",
       url: "some.php", // page where insertion to database should have made
       data: "name=John",
       success: function(msg){
         alert("Counter updated" );
       }
     });
 });
+1
source

Use jQuery to send an AJAX request:

$.post('yourScript.php', {commentID: yourCommentId});

The second argument is the request data. Change this to fit your needs. If you want to execute the JavaScript function after completing the request:

$.post('yourScript.php', {commentID: yourCommentId}, function(data) {

});
0
source

Can I use a form?

<form method="post" action="report.php">
  <input type="hidden" name="report">
  <input type="submit" name="submit" value="report">
</form>

And then the request in the report.php file?

0
source

Source: https://habr.com/ru/post/1789816/


All Articles