I am a new coder with a little bit of understanding of PHP and MySQL, but no exposure to Javascript.
I am planning to build out a database that will be used for collection tracking. The collections are part of pre-defined sets of items. The user will select a set to manage, and will be able to enter the quantity they have for each item in the set. Once the user enters the quantities, I then want to add those to the database by submitting the form data with Javascript.
I have been researching the Javascript that I need to use for this, and I thought I had found everything I needed to make my quick test example work, but it is not working.
Could someone please tell me what I need to do, to get this to work:
<?php
echo "Collection Tracking Enabled</br></br>";
// Variables
$location_sw = 1; //Testing - set as 1 so that location column is displayed
$quantity = !empty($_POST["quantity"]) ? $_POST["quantity"] : null;
$offering = !empty($_POST["offering"]) ? $_POST["offering"] : null;
$trade_sw = !empty($_POST["trade_sw"]) ? $_POST["trade_sw"] : null;
$sell_sw = !empty($_POST["sell_sw"]) ? $_POST["sell_sw"] : null;
$location = !empty($_POST["location"]) ? $_POST["location"] : null;
$notes = !empty($_POST["notes"]) ? $_POST["notes"] : null;
$submitted = !empty($_POST["submitted"]) ? $_POST["submitted"] : null;
// Testing - Display Variables that were passed from the form submit
if (!empty($submitted)) {
echo "passing new data into database" . "</br>";
echo "quantity: " . $quantity . "</br>";
echo "offering: " . $offering . "</br>";
echo "trade_sw: " . $trade_sw . "</br>";
echo "sell_sw: " . $sell_sw . "</br>";
echo "location: " . $location . "</br>";
echo "notes: " . $notes . "</br>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Collections Testing</title>
<meta name="description" content="MinisGallery Collections Tracking Test">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
function submitForm(){
var form = document.getElementById("myForm");
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open("POST", "collectionsTest1.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(formData);
}
</script>
</head>
<body>
<?php if (empty($submitted)) { ?>
<form id="myForm" method="post">
<label for="quantity">Quantity</label>
<input type="number" id="quantity" name="quantity"></br></br>
<label for="offering">Sale/Trade</label>
<input type="number" id="offering" name="offering"></br></br>
<label for="quantity">Trade</label>
<input type="checkbox" id="trade_sw" name="trade_sw"></br></br>
<label for="quantity">Sell</label>
<input type="checkbox" id="sell_sw" name="sell_sw"></br></br>
<?php
if ($location_sw == 1) {
?>
<label for="location">Location</label>
<input type="text" id="location" name="location"></br></br>
<?php
}
?>
<label for="notes">Notes</label>
<input type="text" id="notes" name="notes"></br></br>
<input type="hidden" id="submitted" name="submitted" value="complete">
<button type="button" onclick="submitForm()">Submit</button>
</form>
<?php }
?>
</body>
</html>