Sending a Javascript value to MySQL database

134
March 04, 2018, at 02:32 AM

I am trying to send the value P (ive assigned points to radio button selections and P is the total of those selections) to a php file and then to a MySQL database but i'm having trouble with doing it. I know that it cant be done directly. How can I do this? Any help would be appreciated.

//Javascript 
function getRBtnName(GrpName) { 
	  var sel = document.getElementsByName(GrpName); 
	  var fnd = -1; 
		var str = ''; 
	  for (var i=0; i<sel.length; i++) { 
	    if (sel[i].checked == true) { str = sel[i].value;  fnd = i; } 
	  } 
	//  return fnd;   // return option index of selection 
	// comment out next line if option index used in line above  
	  return str; 
	} 
	  
	function calculateA() { 
	    var A = getRBtnName('q1') * 1; 
	    var B = getRBtnName('q2') * 1; 
	    var C = getRBtnName('q3') * 1; 
	    var D = getRBtnName('q4') * 1; 
	    var E = getRBtnName('q5') * 1; 
	    var F = getRBtnName('q6') * 1; 
	    var G = getRBtnName('q7') * 1; 
		var H = getRBtnName('q8') * 1; 
		var I = getRBtnName('q9') * 1; 
		var J = getRBtnName('q10') * 1; 
		var K = getRBtnName('q11') * 1; 
		var L = getRBtnName('q12') * 1; 
		var M = getRBtnName('q13') * 1; 
		var N = getRBtnName('q14') * 1; 
		var O = getRBtnName('q15') * 1; 
	     
	    var P = A + B + C + D + E + F + G + H + I + J +K + L + M + N + O; 
	    document.formC.totalSectionA.value = P; 
		 
		document.getElementById('mytotal').value = P; 
 
		$('#mytotal').val( P ); 
 
		$.post('main.php', { total : mytotal }, function(data) { 
			alert( data ); 
		} ) 
	} 
 
 
    //php code 
 
    <?php 
	$servername = ""; 
	$username = ""; 
	$password = ""; 
	$dbname = ""; 
	 
	// Create connection 
	$conn = new mysqli($servername, $username, $password, $dbname); 
	// Check connection 
	if ($conn->connect_error) { 
		die("Connection failed: " . $conn->connect_error); 
	}  
	echo $_POST['total']; 
	 
    $sqlStatement = "INSERT INTO `ScoreDB`(`team_Id`) VALUES ($total)"; 
    $res = mysqli_query($conn, $sqlStatement ); 
	echo $sqlStatement; 
?>

Answer 1

It seems that mytotal is undefined because you didn't defined it. Use the P variable like

$.post('main.php', { total : P }, function(data) {
    alert( data );
})

Or define myTotal by var myTotal = P; before your request.

Answer 2

You have to add session_start() to the two pages, the one that holds the variable p and the .php file to pass the variable to.

Then you can pass the variable to the .php file like so

//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];

After the .php page has the variable (you can confirm this by echoing the variable within the .php page and check whether it gives the correct value) you can then pass it to the database

READ ALSO
View of a PDF in Laravel

View of a PDF in Laravel

I am new to laravel, I want to see a pdf stored by a client, without downloading it, just see itI used PHP code before in the past

101
How to retrive Images stored in Blob mysql using Php and Angular2

How to retrive Images stored in Blob mysql using Php and Angular2

How to retrive Image stored in Blob mysql using Php and Angular2

98
send FCM notification after submit query

send FCM notification after submit query

I want to send Firebase notification after successfully executing query below the query and the FCM codeThanks in advance

121
Symfony 4 - How to realise a download with file()

Symfony 4 - How to realise a download with file()

Regarding the documentation https://symfonycom/doc/current/controller

254