How can I save a Javascript variable in php

0

I am trying to make a form that calculates the quality of implementation of certain math materials. The problem is that I do not know how to save the variable value that contains the result of the implementation, in php, and therefore in MySQL

Javascript

$(function(){
  $("input[type=radio]").add("input[type=checkbox]").on("click", function(){
    calcular();
  })

});

function calcular(){
  $radios = $("input[type=radio]:checked");
  $checkboxs = $("input[type=checkbox]:checked");
  var coste_total = 0;
  $checkboxs.each(function(){
    coste_total = coste_total + parseInt($(this).val());
  })
  $radios.each(function(){
    coste_total = coste_total + parseInt($(this).val());
  })
  $(".resultado").html(coste_total)
}

PHP

<?php
$conexion = new mysqli("localhost", "root", "");

if(!$conexion){
echo "Conexión no exitosa";
} else {

$base= mysqli_select_db($conexion, "datos1");
    if(!$base){
        echo "No se pudo conectar a la base de datos";
    }
}
//LLAMAMOS LAS VARIBALES
$Teachers_Name="";
$Teachers_Name= isset($_POST['Teachers_Name']) ? $_POST['Teachers_Name']:'';
$Teachers_Name= empty($_POST['Teachers_Name']) ? $_POST['Teachers_Name']:'';
$Teachers_Name= $_POST['Teachers_Name'] ?? '';
$School_Name="";
$School_Name= isset($_POST['School_Name']) ? $_POST['School_Name']:'';
$School_Name= empty($_POST['School_Name']) ? $_POST['School_Name']:'';
$School_Name= $_POST['School_Name'] ?? '';
$Implementation_Quality= $_POST['coste_total'];
//Guarda las variables


$sql= "INSERT INTO datos_1 (Teachers_Name, School_Name, 
                            Implementation_Quality) VALUES(?, 
                            ?,'Implementation_Quality')";

$sth = mysqli_prepare($conexion,$sql);
mysqli_stmt_bind_param($sth,'sss', $Teachers_Name, $School_Name, 
$Implementation_Quality);
 $ejecutar = mysqli_stmt_execute($sth);

if(!$ejecutar){
 echo "Hubo algun error";
} else {
    echo "Datos guardados correctamente<br><a href='index.html'>Volver</a>";
}

?>

HTML

 <p  class="resultado" style="background:#0774D9" id="coste_total" name="coste_total" value="coste_total" >Implementation Quality</p>
               <input type="submit" value="send">
    
asked by Marinowsky 19.11.2017 в 04:04
source

1 answer

0

You can use AJAX from your JavaScript code and execute the script in PHP, passing on the information you need.

with jQuery:

$.ajax({
   url: "tuscriptphp.php",
   type: "POST" // o GET, o PUT, PATCH, etc...
   data: {
      Teachers_Name: Teachers_Name_valor
      School_Name: School_Name_valor
      coste_total: coste_total_valor
      /* Tus otras variabels... */
   },
   success: function(response) {
      /*
         Lo que quieras que se ejecute si todo estuvo bien en la ejecución PHP
      */
   }
});
    
answered by 19.11.2017 в 04:57