将Javascript值发送到MySQL数据库

时间:2018-03-03 16:18:21

标签: javascript php jquery mysql ajax

我正在尝试将值P(ive指定点到单选按钮选择,P是这些选择的总和)发送到php文件然后发送到MySQL数据库但是我遇到了麻烦。我知道它不能直接完成。我怎样才能做到这一点?任何帮助将不胜感激。

//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;
?>

1 个答案:

答案 0 :(得分:-1)

您必须将// The parameters of a function essentially become local variables // to that function. The names you give will exist throughout that // function's scope. function readFile(dirName, ext, callback) { fs.readdir(dirName, function (err, data) { if (err) { return callback(err, null); } console.log(ext, data); // To avoid shadowing/hiding name the nested function's // arguments something that doesn't conflict with existing // identifier names in the higher scope. function filterData(ext2) { // Now, there is no collission: console.log(ext2, data); return data.filter(function(files) { return arr = files.includes(ext2); console.log(arr); }); return callback(null, arr); } }); } 添加到两个页面,其中包含变量var x = 10; var y = 20; function foo(){ var x = true; // <-- This x shadows the one from the higher scope console.log(x,y); // true 20, not 10, 20 } foo(); console.log(x,y); // 10 20 because the scope of foo is not available heresession_start()文件以将变量传递给。

然后你可以将变量传递给p文件,如此

.php

.php页面包含变量后(您可以通过回显.php页面中的变量并检查它是否给出正确的值来确认),然后可以将其传递给数据库