I am developing a product pricing calculator in laravel. Inside the <script> tag of blade.php i have some javascript variables like,
<script>
.........
front-end logics
.........
var country;
var product_type;
var product_link ;
var seller_shipping_fee;
var weight;
var dimensions;
........
........
</script>
I want to store those variables in the database. I have already created the table in database using laravel migration.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePriceCalculatorResponsesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('pgsql')->create('price_calculator__responses', function (Blueprint $table) {
$table->increments('id');
$table->string('country');
$table->string('product_type');
$table->string('product_link');
$table->string('seller_shipping_fee');
$table->string('weight');
$table->string('dimension');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('pgsql')->dropIfExists('price_calculator__responses');
}
}
How do I save those Javascipt variables in the database?
<script>
$(".save-data").click(function(event){
event.preventDefault();
let country = $("input[name=country]").val();
let product_type = $("input[name=product_type]").val();
let product_link = $("input[name=product_link]").val();
let seller_shipping_fee = $("input[name=seller_shipping_fee]").val();
...............more variables..............
$.ajax({
url: "/ajax-request",
type:"POST",
data:{
country:country,
product_type:product_type,
_token: _token,
................. more variables....................
},
success:function(response){
console.log(response);
if(response) {
$('.success').text(response.success);
$("#ajaxform")[0].reset();
}
},
error: function(error) {
console.log(error);
}
});
});
</script>
Something like that to put your on the right track, do not forget to have your input id match with yur variables if sending data is secure do not forget the token variale it is usually hidden input on symfony i dont know if it is the same case in Laravel. include ajax cdn too :)