title: Sending POST (or GET) data to a PHP script using AJAX
body: <p>I'm trying to build a simple "I viewed this" function in a php directory for an intranet site.</p>
  
  <p>So when a user has read the page, they click a button at the bottom that says "I've read this" that sends the users User ID and the page ID to a simple database table that I can then harvest for other uses.</p>
  
  <p>I'm trying to do it with AJAX and PHP.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>&lt;SCRIPT LANGUAGE="JavaScript"&gt;
  function viewSave(form) {
  var itemid = &lt;?php echo $itemid; ?&gt;;
  var userid = &lt;?php echo $userid; ?&gt;;
  
  var xmlhttp;
  
  if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
  else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  
   xmlhttp.open("GET","saveView.php?q=itemid="+itemid+"&amp;userid="+userid,true);
   xmlhttp.send();
  }
  
  &lt;/script&gt;   
  </code></pre>
  
  <p>and I've set up a simple form with hidden fields:</p>
  
  <pre><code>&lt;form onSubmit="return viewSave(this)" &gt;
  &lt;input type="submit" name="submit" value="Save" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>When I click submit, the URL changes and I get my variables, like this:</p>
  
  <blockquote>
    <p>thispage.php?itemid=455&amp;userid=999&amp;submit=Save</p>
  </blockquote>
  
  <p>but "saveView.php" is not called and the data is not sent.</p>
  
  <p>Here's what's in saveView.php:</p>
  
  <pre><code>&lt;?php
  
  
  $userid =$_GET["userid"];
  $itemid = $_GET["itemid"];
  
    // Connect to the database
              $dbc = mysqli_connect('localhost', 'root', '', 'viewsave');
  
              // Write the data to the database
              $query = "INSERT INTO saves (entryID, userid, itemid) VALUES ( '0', '$userid', '$itemid')";
              mysqli_query($dbc, $query);
  </code></pre>
  
  <p>?></p>
  
  <p>I'm guessing the URL params are only appearing because of the form and that there's something wrong with the JavaScript. </p>
  
  <p>I'm only a beginner with JS.</p>
  
  <p>Help appreciated.</p>
  
  <p>Ray.</p>