Date:  06/06/2018 02:20:07 AM Msg ID:  004956
From:  FoxWeb Support Thread:  004955
Subject:  Re: INSERT into VFP table from PHP API
The problem does not seem to be related to FoxWeb. You should use your browser's developer tools to make sure that the AJAX request is made by the browser to your PHP script. The Network tab of the developer tools will show you the content of the request and the response returned from the PHP script.

If everything appears OK in there, then you should be troubleshoot the PHP script to find out why it is not writing the expected data.
FoxWeb Support Team email
Sent by Olatunji Beckley on 06/06/2018 01:02:38 AM:
Hi all,
I have an API that is supposed to insert payment records into a VFP table "SON2100"  when called, but the record is not created. The two PHP files of the API and the Foxweb script that calls the create_payment.php API are pasted below. Can anyone please take a look at the codes and help figure out the problem?
 // create_payment.php - first PHP file //
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// instantiate payment object
include_once 'payment.php';
$payment = new Payment();
// get posted data
$data = json_decode(file_get_contents("php://input"));

// set payment property values
$payment->refnum = $data->refnum;
$payment->paydate = $data->paydate;
$payment->custname = $data->custname;
$payment->custemail = $data->custemail;
$payment->demandno = $data->demandno;
$payment->demanddate = $data->demanddate;
$payment->amount = $data->amount;
$payment->recpdesc = $data->recpdesc;
$payment->paybank = $data->paybank;
$payment->payref = $data->payref;
// create the payment
echo "Payment was created.";
} else {
echo "Unable to create payment.";
// payment.php - second PHP file // 
class Payment{
    // object properties
    public $refnum;
    public $paydate;
    public $custname;
    public $custemail;
    public $demandno
    public $demanddate;
    public $amount;
    public $recpdesc;
    public $paybank;
    public $payref;

// create payment
function create(){
    // query to insert record
    $query = "INSERT INTO SON2100 (refnum, paydate, custname, custemail, demandno, demanddate, amount, recpdesc, paybank, payref) 
VALUE (:refnum, :paydate, :custname, :custemail, :demandno, :demanddate, :amount, :recpdesc, :paybank, :payref)";
    // prepare query
    $stmt = $this->prepare($query);
    // sanitize
    // bind values
    $stmt->bindParam(":refnum", $this->refnum);
    $stmt->bindParam(":paydate", $this->paydate);
    $stmt->bindParam(":custname", $this->custname);
    $stmt->bindParam(":custemail", $this->custemail);
    $stmt->bindParam(":demandno", $this->demandno);
    $stmt->bindParam(":demanddate", $this->demanddate);
    $stmt->bindParam(":amount", $this->amount);
    $stmt->bindParam(":recpdesc", $this->recpdesc);
    $stmt->bindParam(":paybank", $this->paybank);
    $stmt->bindParam(":payref", $this->payref);
    // execute query
    return false;
        return true;
    } else {
    return false;
// Foxweb script that accepts input from user and call the create_payment.php API //
* payment.fwx - Add new payment record
LOCAL refnum, paydate, custname, custemail, demandno, demanddate, amount, recpdesc, paybank, payref
Response.Expires = 0
error_txt = ''
cPaymentString = ''
STORE '' TO M.refnum, M.paydate, M.custname, M.custemail, M.demandno, M.demanddate, M.recpdesc, M.paybank, M.payref
STORE 0.00 TO M.amount 

IF Request.Form("payment_action") = "Cancel"
* Cancel was clicked in the payment form

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="">
<title>Payment Record</title>
<style type="text/css">


<form action="payment.fwx" method="post">
<input type="hidden" name="FormName" value="payment">
<table border=0 cellpadding=4 cellspacing=0>
<table border=0 cellpadding=4 cellspacing=0 bgcolor="#99cccc">
<td colspan=2 bgcolor="#4682b4"><font face="Arial,Helvetica" size="5" color="#ffffff">&nbsp;<b>Add Payment Record</b></font></td>
<td>RRR Number</td>
<td>Payment Date</td>
<td><input name="refnum" autofocus size="15" value="<%=TRIM(M.refnum)%>"></td>
<td><input type="date" name="paydate" value="<%=M.paydate%>"></td>
<td>Name of Customer</td>
<td>E-mail Address</td>
<td><input name="custname" size="40" value="<%=TRIM(M.custname)%>"></td>
<td><input name="custemail" size="50" value="<%=TRIM(M.custemail)%>"></td>
<td>Demand Note Number</td>
<td>Demand Note Date</td>
<td><input name="demandno" size="30" value="<%=TRIM(M.demandno)%>"></td>
<td><input type="date" name="demanddate" value="<%=M.demanddate%>"></td>
<td>Amount Paid</td>
<td>Description of Payment</td>
<td><input type="number" name="amount" step=".01" size="10" value="<%=M.amount%>"></td>
<td><input name="recpdesc" size="30" value="<%=TRIM(M.recpdesc)%>"></td>
<td>Paying Bank</td>
<td>Bank Teller Number</td>
<td><input name="paybank" size="30" value="<%=TRIM(M.paybank)%>"></td>
<td><input name="payref" size="15" value="<%=TRIM(M.payref)%>"></td>
<td colspan=2 align="center">
<input type="submit" name="payment_action" value="Save" onclick="sendData(Paydata())">
<input type="submit" name="payment_action" value="Cancel">

function sendData(data) {
var XHR = new XMLHttpRequest();
var urlEncodedData = "";
var urlEncodedDataPairs = [];
var name;

// Turn the data object into an array of URL-encoded key/value pairs.
for(name in data) {
urlEncodedDataPairs.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));

// Combine the pairs into a single string and replace all %-encoded spaces to 
// the '+' character; matches the behaviour of browser form submissions.
urlEncodedData = urlEncodedDataPairs.join('&').replace(/%20/g, '+');

// Define what happens on successful data submission
XHR.addEventListener('load', function(event) {
window.alert('Yeah! Data sent and response loaded.');

// Define what happens in case of error
XHR.addEventListener('error', function(event) {
window.alert('Oops! Something goes wrong.');

try {
// Set up our request'POST', 'http://localhost/api/create_payment.php', true);
    catch(err) {
        window.alert("Could not complete request.");
        return false;
// Add the required HTTP header for form data POST requests
XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

// Finally, send our data.

function Paydata() {
M.refnum     = Request.Form('refnum')
M.paydate    = Request.Form('paydate')
M.custname   = Request.Form('custname')
M.custemail  = Request.Form('custemail')
M.demandno   = Request.Form('demandno')
M.demanddate = Request.Form('demanddate')
M.amount     = Request.Form('amount')
M.recpdesc   = Request.Form('recpdesc')
M.paybank    = Request.Form('paybank')
M.payref     = Request.Form('payref')

CREATE CURSOR crPayment (refnum C(15), paydate C(15), custname C(40), custemail C(50), demandno C(30), demanddate C(15), amount N(10,2), recpdesc C(30), paybank C(30), payref C(15))
INSERT INTO crPayment (refnum, paydate, custname, custemail, demandno, demanddate, amount, recpdesc, paybank, payref);
VALUES (M.refnum, M.paydate, M.custname, M.custemail, M.demandno, M.demanddate, VAL(M.amount), M.recpdesc, M.paybank, M.payref)

* Set the content type to text/plain to prevent some Ajax frameworks from complaining
* Response.ContentType = "text/plain"
cPaymentString = fwJSON.WriteCursor()
return false;