1

I have implemented the same setup elsewhere in my site and can't figure out why it's not working this time.

When a user clicks the accept button, it calls a JavaScript function acceptOrder(orderID) which passes the orderID onto a php page to update a record in the db.

orderID is assigned ok in the JavaScript but it doesn't reach the php. Var_dump on POST shows nothing, nor does $_POST('orderID'). I've even tried just sending an integer to the php in case there was a problem with the var but it made no difference.

Js

function acceptOrder(orderID) {
var orderID=orderID;
console.log("assigned: "+orderID);
var xmlhttp;

// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
   xmlhttp=new XMLHttpRequest();
}
// code for IE6, IE5
else
{
   xmlhttp=new ActiveXObject("Microsoft.    }

xmlhttp.onreadystatechange=function()
{
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
   {
      console.log (xmlhttp.responseText);
   }
}
xmlhttp.open("POST","acceptorder.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-rule encoded");
xmlhttp.send(orderID);
console.log(orderID+" sent");
//location.reload();
//console.log("reload");
}

Php

<?php
require_once("config1412/class.shop.php");
session_start();
$shop = new SHOP();

echo var_dump($_POST);
//$orderID = $_POST['orderID'];
//echo "orderId var = ".$orderID."<br/>post ".$_POST['orderID'];

//$shop->acceptOrder($orderID);
?>

Needless to say I've searched about and don't see any solutions elsewhere.

Many thanks

Paul
  • 15
  • 5

4 Answers4

0

Please replace this line

xmlhttp=new ActiveXObject("Microsoft.    } 

with following this line

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
Ashish Patel
  • 913
  • 10
  • 24
  • Yes I know, these were typos when copy/pasting via mobile phone. They're all ok in the original code. – Paul Nov 26 '16 at 09:39
0

You should write this in your js

function acceptOrder(orderID) {
var orderID=orderID;
console.log("assigned: "+orderID);
var xmlhttp;

// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
   xmlhttp=new XMLHttpRequest();
}
// code for IE6, IE5
else
{
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 }

xmlhttp.onreadystatechange=function()
{
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
   {
      console.log (xmlhttp.responseText);
   }
}
xmlhttp.open("POST","acceptorder.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-rule encoded");
xmlhttp.send(orderID);
console.log(orderID+" sent");
//location.reload();
//console.log("reload");
}
Jahid Mahmud
  • 1,072
  • 1
  • 11
  • 28
0

I would recommend you to use jQuery for ajax calls. It's much easier to setup and straightforward. Especially, and specialy because it is very easy setup for a beginner. And for people who want to install ajax the easy way. I use it every single time I want to do ajax in my code. Here is a link:

http://api.jquery.com/jquery.ajax/

Just put the tag to include jQuery and then one javascript command to call the ajax.

Vladimir Despotovic
  • 2,417
  • 2
  • 26
  • 46
0

As i can see, you didn't set variable name for orderID, change line of code:

xmlhttp.send(orderID);

to:

xmlhttp.send("orderID="+orderID);

If it's only SQL error of missing orderID, and all other passess okay, it's solution for you. As you said in comments "I just get a sql error because the variable orderID is empty". You're missing only naming post send data, that's why it's empty.

Ultrazz008
  • 1,460
  • 1
  • 10
  • 25
  • I think you are correct. That's probably where I'm going wrong. I will try this when I get home. Thank you very much – Paul Nov 26 '16 at 12:11