Daniel
Daniel

Reputation: 57

Updating database OnClick() <a>

I have been looking around here at many questions but it seems nothing works for me... It doesn't update the database.

function language(id,lang){
  $.ajax({
      url: 'modules/tca/updatedb.php',
      type: 'POST',
      data: 'id='+id+'&lang='+lang,
   });
}
<li><a href="#en" onclick="return language($id,"en");"><span class="flag flag-usa flag-1x"></span> EN</a></li>
<li><a href="#fr" onclick="return language($id,"fr");"><span class="flag flag-frc flag-1x"></span> FR</a></li>

This is my updatedb.php.

<?php
@include_once('setdb.php');
$id = $_POST['id'];
$lang = $_POST['lang'];

mysql_query("UPDATE users SET lang='$lang' WHERE id = '$id' ");
?>

Upvotes: 0

Views: 9157

Answers (3)

marlonjd
marlonjd

Reputation: 135

It's working option.

<script>
    function language($id, $lang){
        //get the input value
        $.ajax({
            //the url to send the data to
            url: "modules/tca/updatedb.php",
            //the data to send to
            data: {id : $id, lang: $lang},
            //type. for eg: GET, POST
            type: "POST",
            //on success
            success: function(data){
                console.log("***********Success***************"); //You can remove here
                console.log(data); //You can remove here
            },
            //on error
            error: function(){
                    console.log("***********Error***************"); //You can remove here
                    console.log(data); //You can remove here
            }
        });
    }
</script>

And in Body:

<li><a href="#en" onclick="language(1,'en')"><span class="flag flag-usa flag-1x" ></span>EN</a></li>
<li><a href="#fr" onclick="language(2,'fr')"><span class="flag flag-frc flag-1x"></span> FR</a></li>

And your post page (in this example your modules/tca/updatedb.php)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "code";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$id = $_POST['id'];
$lang= $_POST['lang'];

$sql = "UPDATE users SET lang='$lang' WHERE id = '$id'";

if ($conn->query($sql) === TRUE) {
    echo "New record updated successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Upvotes: 2

Graham Murray
Graham Murray

Reputation: 35

"The return value from the onClick code is what determines whether the link's inherent clicked action is processed or not - returning false means that it isn't processed, but if you return true then the browser will proceed to process it after your function returns and go to the proper anchor"

Found Here HTML anchor link - href and onclick both?. So try returning true

<a href="#fr" onclick="language($id,"fr"); return true;">

Upvotes: 0

Nick Tucci
Nick Tucci

Reputation: 356

It looks like your problem lies within the LI element.

<a href="#en" onclick="return language($id,"en");">

should be:

<a href="#en" onclick="return language('<?= $id ?>', 'en');">

You also shouldn't be using mysql_query.

Upvotes: 0

Related Questions