Date:  06/06/2018 01:02:38 AM Msg ID:  004955
From:  Olatunji Beckley Thread:  004955
Subject:  INSERT into VFP table from PHP API
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?
 
TIA.
 
  
 // create_payment.php - first PHP file //
<?php
// 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
if($payment->create()){
echo "Payment was created.";
} else {
echo "Unable to create payment.";
}
?>
 
 
 
// payment.php - second PHP file // 
<?php
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
    $this->refnum=htmlspecialchars(strip_tags($this->refnum));
    $this->paydate=htmlspecialchars(strip_tags($this->paydate));
    $this->custname=htmlspecialchars(strip_tags($this->custname));
    $this->custemail=htmlspecialchars(strip_tags($this->custemail));
    $this->demandno=htmlspecialchars(strip_tags($this->demandno));
    $this->demanddate=htmlspecialchars(strip_tags($this->demanddate));
    $this->amount=htmlspecialchars(strip_tags($this->amount));
    $this->recpdesc=htmlspecialchars(strip_tags($this->recpdesc));
    $this->paybank=htmlspecialchars(strip_tags($this->paybank));
    $this->payref=htmlspecialchars(strip_tags($this->payref));
 
    // 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;
    if($stmt->execute()){
        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
Response.End
ENDIF
%>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Payment Record</title>
<style type="text/css">
<%=FILETOSTR('StyleSheet2.css')%>
</style>
</head>
<body>

<br><br>

<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">
<tr>
<td colspan=2 bgcolor="#4682b4"><font face="Arial,Helvetica" size="5" color="#ffffff">&nbsp;<b>Add Payment Record</b></font></td>
</tr>
<tr>
<td>RRR Number</td>
<td>Payment Date</td>
</tr>
<tr>
<td><input name="refnum" autofocus size="15" value="<%=TRIM(M.refnum)%>"></td>
<td><input type="date" name="paydate" value="<%=M.paydate%>"></td>
</tr>
<tr>
<td>Name of Customer</td>
<td>E-mail Address</td>
</tr>
<tr>
<td><input name="custname" size="40" value="<%=TRIM(M.custname)%>"></td>
<td><input name="custemail" size="50" value="<%=TRIM(M.custemail)%>"></td>
</tr>
<tr>
<td>Demand Note Number</td>
<td>Demand Note Date</td>
</tr>
<tr>
<td><input name="demandno" size="30" value="<%=TRIM(M.demandno)%>"></td>
<td><input type="date" name="demanddate" value="<%=M.demanddate%>"></td>
</tr>
<tr>
<td>Amount Paid</td>
<td>Description of Payment</td>
</tr>
<tr>
<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>
</tr>
<tr>
<td>Paying Bank</td>
<td>Bank Teller Number</td>
</tr>
<tr>
<td><input name="paybank" size="30" value="<%=TRIM(M.paybank)%>"></td>
<td><input name="payref" size="15" value="<%=TRIM(M.payref)%>"></td>
</tr>
<tr>
<td colspan=2 align="center">
<input type="submit" name="payment_action" value="Save" onclick="sendData(Paydata())">
<input type="submit" name="payment_action" value="Cancel">
</td>
</tr>
</table>
</table>
</form>

<script>
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
XHR.open('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.
XHR.send(urlEncodedData);
}
</script>

<script>
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')

SET DATE BRITISH
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;
}
</script>
</body>
</html>