0

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' ");
?>
Machavity
  • 30,841
  • 27
  • 92
  • 100
Daniel
  • 57
  • 1
  • 8
  • And what is your question? What are you having an issue with? Is something not working as expected? – Jonathan Kuhn Feb 16 '17 at 22:59
  • It is not updating database. – Daniel Feb 16 '17 at 23:01
  • 1
    This is most likely pseudo code, but you should be careful about SQL injection, since you're not protecting against it here at all. – garrettmurray Feb 16 '17 at 23:04
  • I will cover that later. First I want to make it update succesfully. – Daniel Feb 16 '17 at 23:06
  • @Daniel You need to understand that the way you're doing is not only [insecure](http://stackoverflow.com/documentation/php/5828/pdo/2685/preventing-sql-injection-with-parameterized-queries), it could also be the reason your query is failing. Also, [stop using mysql_ functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), as they were removed from PHP – Machavity Feb 16 '17 at 23:13
  • @Machavity got it, thanks for the documentation. Tomorrow morning I will go deeper into it. I am a beginner in jQuery, Javascript. Can you tell me please, what can be wrong with my function? I appreciate it! – Daniel Feb 16 '17 at 23:26
  • Did you use the console to verify it was posting the data to PHP? – Machavity Feb 16 '17 at 23:30
  • @Machavity, yes, the function is executing correctly, it seems now that the only problem is that the database doesn't get updated. – Daniel Feb 16 '17 at 23:34

3 Answers3

2

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();
?>
marlonjd
  • 135
  • 10
0

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.

Nick Tucci
  • 336
  • 4
  • 16
  • Can you elaborate on the exact problem? Is it not accessing the PHP page? Is the HTTP request returning an error code other than 200? What is going on? – Nick Tucci Feb 16 '17 at 23:06
  • It is not returning anything, I click the link and nothing happens, I am thinking about the function. I guess I wrote it wrong or it is incomplete. – Daniel Feb 16 '17 at 23:08
0

"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;">
Community
  • 1
  • 1