更新数据库OnClick()<a>

时间:2017-02-16 22:57:19

标签: javascript php jquery html ajax

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

3 个答案:

答案 0 :(得分:2)

它的工作选择。

> ca <- commandArgs()
> print(ca)
[1] "/Library/Frameworks/R.framework/Resources/bin/exec/R"
[2] "-f"                                                  
[3] "/Users/davidwinsemius/ptest.r"                       
[4] "--args"                                              
[5] "test of args"                                        
> 
> 

在身体:

<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>

您的帖子页面(在本例中为您的modules / tca / updatedb.php)

<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>

答案 1 :(得分:0)

看起来你的问题在于LI元素。

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

应该是:

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

你也不应该使用mysql_query。

答案 2 :(得分:0)

&#34; onClick代码的返回值决定了链接的固有点击操作是否得到处理 - 返回false意味着它没有被处理,但是如果你返回true那么在函数返回后,浏览器将继续处理它并转到正确的锚点#34;

在此处找到HTML anchor link - href and onclick both?。所以尝试返回true

//the data
$data = array(
    'someVar' => "12345",
    'myFunc' => function($var) {
        return "got Value:" . $var;
    }
);
$engine = new Mustache_Engine();
$rendered = $engine->render('{{#myFunc}}{{someVar}}{{/myFunc}}', $data);