Using jQuery to store basic text string in mySQL base?

Posted by Kenny Bones on Stack Overflow See other posts from Stack Overflow or by Kenny Bones
Published on 2010-05-10T08:19:51Z Indexed on 2010/05/10 8:24 UTC
Read the original article Hit count: 209

Filed under:
|
|
|

Could someone point me in the right direction here? Basically, I've got this jQuery code snippet:

$('.bggallery_images').click(function () {
    var newBG = "url('" + $(this).attr('src');
    var fullpath = $(this).attr('src');
    var filename = fullpath.replace('img/Bakgrunner/', '');

    $('#wrapper').css('background-image', newBG);

    // Lagre til SQL
    $.ajax({
        url: "save_to_db.php",
        // The url to your function to handle saving to the db
        data: filename,
        dataType: 'Text',
        type: 'POST',
        // Could also use GET if you prefer
        success: function (data) {
            // Just for testing purposes.
            alert('Background changed to: ' + data);
        }

    });

});

This is being run when I click a certain button. So it's actually within a click handler.

If I understand this correctly, this snippet takes the source if the image I just clicked and strips it so I end up with only the filename. If I do an alert(filename), I get the filename only. So this is working ok.

But then, it does an ajax call to a php file called "save_to_db.php" and sends data: filename. This is correct right? Then, it does a callback which does an alert + data.

Does this seem correct so far?

Cause my php file looks like this:

<?php
require("dbconnect2.php");
$uploadstring = $_POST['filename'];
$sessionid = $_SESSION['id'];
echo ($sessionid);
mysql_query("UPDATE brukere SET brukerBakgrunn = '$uploadstring' WHERE brukerID=" .$_SESSION['id']);
mysql_close(); 
?>

When I click the image, the jQuery snippet fires and I get the results of this php file as output for the alert box. I think that the variables somehow are empty. Because notice the echo($sessionid); which is a variable I've created just to test what the session ID is. And it returns nothing. What could be the issue here?

Edit: I just tried to echo out the $uploadstring variable as well and it also returns nothing. It's like the jQuery snippet doesn't even pass the variable on to the php file?

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery