-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);
?>
Niklas
  • 15
  • 2
  • 5
  • http://stackoverflow.com/questions/20150130/ajax-and-php-to-enter-multiple-forms-input-to-database/20150474#20150474 – MonkeyZeus Apr 14 '14 at 15:12
  • 1
    `$data = $_POST['data'];` should be `$data = $_POST['lyricsave'];` – MonkeyZeus Apr 14 '14 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` ? – t.niese Apr 14 '14 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. – Tigran Apr 14 '14 at 15:15
  • ajax isn't some mystical new technology, what you're doing at the moment is no different than a `
    ` with a single input ``. Given that, how should you retrieve the value posted with a name "lyricsave" in php?
    – Kevin B Apr 14 '14 at 15:17
  • Additionally, what kind of element is `"#savelyric"`? does it have `.text()`? or does it have a value. – Kevin B Apr 14 '14 at 15:19
  • Save lyric is a div with content, yes. – Niklas Apr 14 '14 at 15:22

2 Answers2

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?

dev7
  • 6,259
  • 6
  • 32
  • 65
  • 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 :) – Niklas Apr 14 '14 at 15:21
  • `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. – Kevin B Apr 14 '14 at 15:22
  • 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. – t.niese Apr 14 '14 at 15:22
  • 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... – Niklas Apr 14 '14 at 15:28
  • I don't get the $data value in the success function either – Niklas Apr 14 '14 at 15:29
  • @Yani Yes I did, but I don't get it to work. I can update with the things I got now... – Niklas Apr 14 '14 at 15:31
  • what errors are you receiving?? looks legit to me, (1) do var_dump (2) check your browser console for error (3) debug the response from the Ajax using your browser network tools. is the URL even correcT? – dev7 Apr 14 '14 at 15:35
  • Currently I'm not getting any errors at all, I have no idea how to do a var_dump, under the response section under the sendlyrics.php inside the network section it says: This request has no response data available, and I'm also wondering why my success function is not working :P – Niklas Apr 14 '14 at 15:42
  • You need to learn how to debug both your client and server side scripts. If you are not listening for errors, you are not always going to see them. Ajax only return the success event if it was successful. Add an error event handler http://stackoverflow.com/questions/6991306/how-do-i-debug-a-jquery-ajax-request – dev7 Apr 14 '14 at 15:56
0

Only change this line:

var superstr = { lyricsave:lyr }

with

var superstr = { data:lyr }
Vishal Khode
  • 831
  • 2
  • 9
  • 15