使用ajax发布变量将不起作用

问题描述:

I have some data, processed with javascript that I want to write to a database. So I'm trying to code something with ajax. But with no luck...

What I need: I have two variables (id, naam) that I want to pass to a php-script. Nothing more...

<script type='text/javascript'>

function geefNaam(id, naam)
{
  $.ajax({
  type: "POST",
  url: "schrijfrecord.php",
  data: { id: id, naam: naam },
  })
}
</script>

and I call this function...

geefNaam(id, naam);

my php

<?php

$id = $_POST['id'];
$naam = $_POST['naam'];

echo $id;
echo $naam;
?>

First remove , after data: { id: id, naam: naam },. Also you aren't doing anything after your $.ajax call. Use success: function(){} or .done(). I would use $.post():

//<![CDATA[
<script type='text/javascript'>
function geefNaam(id, naam, where){
    $.post('schrijfrecord.php', {id: id, naam: naam},
      function(result){
        where.html(result);
      }
    });
}
$('#someElement').click(function(){
  geefNaam('someId', 'someName', $('#wherever'));
});
//]]>
</script>