-1

I've been struggeling with this problem for a while now and I still can't understand why it's not working. I've tried multiple possibilites but none of them worked, so can someone please help me with how to pass var superstr = $( "#savelyric" ).text(); through Ajax and into my database? This is what I've been experimenting with:

        function saveinPHP() {
        //alert("Came here");
        var lyr = $( "#savelyric" ).text();
        var superstr = { lyricsave:lyr }
        //var superstr = 'lol';
        //var hashString = "lol";
        //var data = { yoururl:'hmm'}
        $.ajax({
        type: "POST",
        url: "includes/sendlyrics.php",
        data: superstr,
        success:  function(data){
            alert("***DATA***"+data+"***MSG***");
            alert("Settings has been updated successfully." + data + "~~~" + hashString);
            //window.location.reload(true);
        }
    });
    }

And as you can see, I've tried with multiple ways of doing it, but it just never works. I don't understand how on earth you do this. And the PHP file goes like this:

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

$data = $_POST['data'];

$query = "UPDATE song SET time='".$data."' WHERE id='1'";
mysqli_query($query);

?>

And yes, I'm fully aware that my database is vulnerable for SQL injections, and I will fix that as soon as I get this working.

This is what I've tried, but I can do things completely different if you think that is necessary.

Right now I got the JS:

function saveinPHP() {
        var superstr = $( "#savelyric" ).text();
        $.ajax({
        type: "POST",
        url: "includes/sendlyrics.php",
        data: {superstr: superstr},
        success:  function(data){
            alert("***DATA***"+data+"***MSG***");
            alert("Settings has been updated successfully." + data + "~~~");
            //window.location.reload(true);
        }
    });

And PHP

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

$data = $_POST['superstr'];

$query = "UPDATE song SET lyrtime='".$data."' WHERE id='1'";
mysqli_query($query);
?>
7
  • stackoverflow.com/questions/20150130/… Commented Apr 14, 2014 at 15:12
  • 1
    $data = $_POST['data']; should be $data = $_POST['lyricsave']; Commented Apr 14, 2014 at 15:13
  • 1
    Why do you expect that your data will be in $_POST['data']? The object you post is superstr so it only has the key lyricsave . Did you already look if anything is inside of $_POST by using var_dump ? Commented Apr 14, 2014 at 15:14
  • Few ideas, there is fail function in JQuery, so you cansee if your query fail. Do var_dump of your variables. And MonkeyZeus, looks right, you have an error in your data. Commented Apr 14, 2014 at 15:15
  • ajax isn't some mystical new technology, what you're doing at the moment is no different than a <form method="POST"> with a single input <input type="text" name="lyricsave" value="somevalue">. Given that, how should you retrieve the value posted with a name "lyricsave" in php? Commented Apr 14, 2014 at 15:17

2 Answers 2

0

Only change this line:

var superstr = { lyricsave:lyr }

with

var superstr = { data:lyr }
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to pass var superstr = $( "#savelyric" ).text(); as your question states, but in your code you are actually assigning:

var superstr = { lyricsave:lyr }

Change it to:

var superstr = $( "#savelyric" ).text();

UPDATE (per Kevin B's comment)

You also need to change the following:

data: {superstr: superstr},

and in your PHP:

$data = $_POST['superstr'];

Also, why are you tring to set it to the time column? I have not seen your tables, but a wild guess will tell me that time is a different datatype (TIMESTAMP/DATETIME maybe?) and is rejecting your data. Don't you want to set it to a column named data or lyrics or anything which contains text?

9 Comments

My DB is set up with the song id, name, artist, lyrics and time. The time is "longtext", because the time is not "date-time", it's the time in the song a line of lyric will highlight :)
var superstr = $( "#savelyric" ).text(); won't work, the data option needs key/value pairs or a paramstring. What he was already doing should have worked, assuming he reads the proper form value in php and .text() is getting the correct value.
If you only change this in the code, then just a string is send as the body of the post. This will not appear in $_POST as it is not a key value pair then.
So I've changed it as you said in your post, and I've changed the name to "lyrtime" in the database, because it might have been something fishy with that name, but it still don't work...
I don't get the $data value in the success function either
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.