I am currently trying to make an AJAX request for a type of form post, but the PHP parse script I am trying to send the data to is returning a 404 error that I can see through Chrome developer tools. Here is my code:
<script type="text/javascript">
function ajax_post(){
var hr = new XMLHttpRequest();
var url = "/products/parse.blade.php";
var productName = document.getElementById("productName").value;
var quantityInStock = document.getElementById("quantityInStock").value;
var pricePerItem = document.getElementById("pricePerItem").value;
var totalValueNumber = quantityInStock * pricePerItem;
var route = "productName="+productName;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
hr.send(route);
document.getElementById("status").innerHTML = "processing...";
}
</script>
<div class="container-fluid form-container">
<div class="row">
<div class="col-lg-3 col-md-12">
<div class="form-group">
<input type="text" name="productName" id="productName" placeholder="Product Name" class="form-control" required>
</div>
<div class="form-group">
<input type="number" name="quantityInStock" id="quantityInStock" placeholder="Quantity in Stock" class="form-control" required>
</div>
<div class="form-group">
<input type="number" step="0.01" name="pricePerItem" id="pricePerItem" placeholder="Price per Item" class="form-control" required>
</div>
<input type="submit" value="Create Product Listing" class="btn btn-primary" onclick="ajax_post();">
<hr>
<div id="status"></div>
</div>
</div>
</div>
I am using Laravel, and both this view products.create
and the file I am trying to use to parse the data, products.parse
are both in the products
directory of my views folder. But, when I try to submit, all I get is a response of "processing" in the status div, and a 404 error for the route mydevsite.dev/products/parse
in Chrome's dev console.
My thought it that I needed to define a route, so I added:
Route::post('/products/parse', 'ProductController@parse');
Which returns
return view('products.parse');
But this just changes my error to a 419. I'm thinking it has something to do with CSRF field, but I'm not sure how to include that because it my html is not a form, but simply has form elements. Why is my route returning a 404 or 419 error, and how do I fix this?