0

Hello,

I am looking for a script to add items to a database that can be found in the my account section,

The table in the database is structured:
id | user_id | slug

I want to use the ajax to update and remove it later on down the road if they want to.

The items are in a loop on the search page and I would like to also have the code added to the item details page.

Does anyone know of a code like that? or is it easy to write?

Thanks in advance!

4
Contributors
7
Replies
33
Views
3 Years
Discussion Span
Last Post by matrixdevuk
0

Are you familiar with jQuery?

There are basically two components you need. Your jQuery script and the server side script that will take data from the Ajax call and write to your DB. Then, your script can return data back to your client.

0

Im not to keen with jquery. I know a little bit of it. I did write this:

<?php

include 'connect.php';

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

$username = $_POST['user_id'];
$liked = $_POST['liked'];   
$slug = $_POST['url_slug'];

mysqli_query($con,"INSERT INTO favorites (username ,slug, value) VALUES ('$username','$slug',$liked)");

if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}

mysqli_close($con);
?>
1

This would something to handle your problem with jquery.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
<script type="text/javascript">
jQuery(document).ready(function($){
    $('#button').on('click', function(e){
        e.preventDefault();

        $.ajax({
            url: 'path/to/script.php',
            type: 'POST',
            data: {user_id: $_SESSION['user_id'], liked: true, url_slug: $(location).attr('href')},
            cache: false,
            success: function(data){
                alert(data);
            }
        });
    });
});
</script>
0

I'm going to be critical with @gabrielcastillo's example.
Since the request will be from the client, you don't need to send the user_id parameter within the POST request. You can do this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
<script type="text/javascript">
jQuery(document).ready(function($){
    $('#button').on('click', function(e){
        e.preventDefault();

        $.ajax({
            url: 'path/to/script.php',
            type: 'POST',
            data: {liked: true, url_slug: $(location).attr('href')},
            cache: false,
            success: function(data){
                alert(data);
            }
        });
    });
});
</script>

then pick it back up like this:

<?php
    session_start();
    $user = (empty($_SESSION['user_id']) ? null : intval($_SESSION['user_id']));

    if ($user)
    {
        // Update the database
    }else{
        die();
    }

If you were to send the user_id and then retrieve it from $_POST, it would cause problems and enable one extra way for a hacker to exploit the system. Accessing the sessions directly is the smart thing to do in this case.

Edited by matrixdevuk

0

Good think I said something like this and not just like this. Someone could read my post and take the code as is, without making modifing to their needs.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.