Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 777075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:32:52+00:00 2026-05-14T19:32:52+00:00

Could someone point me in the right direction here? Basically, I’ve got this jQuery

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:32:53+00:00Added an answer on May 14, 2026 at 7:32 pm

    You’re trying to send just the filename, but you’re retrieving a named form field in your PHP code. So you need to send a named form field:

    Change your ajax call like this:

    $.ajax({
        url: "save_to_db.php",
        // The url to your function to handle saving to the db
        data: {filename: filename}, // <= Change #1 (give jQuery a simple object)
        dataType: 'text',           // <= Change #2 ('text', not 'Text')
        type: 'POST',
        // Could also use GET if you prefer
        success: function (data) {
            // Just for testing purposes.
            alert('Background changed to: ' + data);
        }
    
    });
    

    Your PHP script will now receive a POST varible called filename whose value comes from your filename Javascript variable. (You can also use $.post to do this, but it’s just a wrapper for ajax anyway…)

    Passing a simple object into the ajax call is the easiest way to send fields to the server. jQuery will take the object and create the URL-encoded form data (doing all of the escaping for you) by using the object’s keys and field names. So for instance, if you give it this object:

    data: {a: 1, b: "testing one two three", c: 3}
    

    …it sends this URL-encoded data:

    a=1&b=testing+one+two+three&c=3
    

    (Note how it encodes it for us.) More in the ajax docs (but beware, at present what the docs say about array handling is wrong; see this bug report for details).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 377k
  • Answers 377k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When you create a new Thread, its ThreadState is ThreadState.Unstarted,… May 14, 2026 at 8:49 pm
  • Editorial Team
    Editorial Team added an answer It will clone all of the branches. However, there will… May 14, 2026 at 8:49 pm
  • Editorial Team
    Editorial Team added an answer If the problem is simply finding out which HTML element… May 14, 2026 at 8:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.