I have been searching for hours and I do not understand MySQL so I am wondering if it is possible to do this another way. I have a custom html payment form that submits invoice number, amount, and name to a payment processor. I want to be able to add in a transaction number that starts at 30200 and auto increments by one every time the form is submitted to the processor. I wrote a simple increment function:
var i = 30200;
function buttonClick() {
document.getElementById('transNum').value = ++i;
}
The code passes the incremented variable correctly but doesn’t store the new value anywhere and I do not know how to store it. Every time the form is submitted the transaction number gets set to 30201. Is there a way to store it in an external file and call the last number stored? Like I said I do not know MySQL so storing it in the database is beyond my understanding. There has to be an easy understandable way for me to accomplish this but I cannot find a solution.
EDIT ALMOST THERE (I HOPE)
After a lot of research I am trying to use mySQL to accomplish this task.
So I followed a YouTube video and used phpMyAdmin to create a table called refNum and then created the auto incrementing variable refNumber and set it to 30300.
I created a php file and uploaded it to the server called reference.php with this code inside it:
<?php
function get_refNum($ref){
global $wpdb;
$results = $wpdb->get_results("SELECT refNumber FROM refNum");
return $results;
};
?>
Then in my form I wrote this code to call the page and get the results from the database:
function getTransNumber(){
$.ajax({
url : '/reference.php',
method : 'post',
data : {$results},
});
$results = document.getElementById('invoiceNum').value;
}
window.onload = getTransNumber;
My problem is now I am getting an error Uncaught reference error: $results is not defined in my ajax call under data. Which means either I am not getting the data correctly in my php code or I am not passing the data. Can someone please point me in the right direction and tell me what I did wrong here?
2
Answers
If you have a global number, that needs to be increased for anyone accessing the page (and not forgotten when the page is closed), then you need some place to store the last used number.
Whether or not you understand MySQL, the normal solution for this is to use some kind of database, although that doesn’t need to be MySQL.
I’m not a big MySQL user but there is an AutoIncrement attribute that each column can have (or not).
Here is an example from https://www.w3schools.com/sql/sql_autoincrement.asp
CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);
That first field doesn’t need to be provided by you when inserting. It will do it for you everytime you create a record. Not totally sure how to set the first one to 32000 though.