简体   繁体   中英

Pass string through Jquery and ajax to PHP

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);
?>

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?

Only change this line:

var superstr = { lyricsave:lyr }

with

var superstr = { data:lyr }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM