Mysql
 sql >> Database >  >> RDS >> Mysql

Passa il valore della variabile da JS a PHP

indice.html

<script src="http://code.jquery.com/jquery-latest.min.js" type='text/javascript'></script>
<script>
   $(document).ready(function(){ 
       $("#insert").click(function(event){
           $.post('insert.php',{username:$(this).html()});
       })
   });
</script>
<a href='javascript:void(0);' id='insert'>Username</a>

inserisci.php

<?php
function insert($username){
    $conn = mysql_connect("host","user","passwd");
    if($conn){
        $username =  mysql_real_escape_string($_POST['username']);
        $result = mysql_query("INSERT INTO test.user(username) VALUES('".$username."')") or die(mysql_error());
        mysql_close($conn);
    }
}
if(isset($_POST['username'])){
    insert($_POST['username']);
}
?>