I an trying to read the value and the name of a select box. The name I use within JavaScript to determine the ID of another select box. The value I send using
XMLHttpRequest() to a PHP file to search a MySQL table to determine the number I need to load to the second select box.
I am using NetBeans 8.2 IDE and have proved that the PHP file is working correctly.
Using Chrome Developer Tools I have tracked the JavaScript file and I get an error on the line
document.getElementById(HCID).innerHTML = this.responseText;
The error given is
The specified value "NaN" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?
My script code is
<script type="text/javascript">
function find_hc(player,name){
switch (name) {
case "Player1":
var HCID = 'P1HC';
break;
case "Player2":
var HCID = 'P2HC';
break;
case "Player3":
var HCID = 'P3HC';
break;
case "Player4":
var HCID = 'P4HC';
break;
case "Player5":
var HCID = 'P5HC';
break;
case "Player6":
var HCID = 'P6HC';
break;
case "Player7":
var HCID = 'P7HC';
break;
case "Player8":
var HCID = 'P8HC';
break;
case "Player9":
var HCID = 'P9HC';
break;
case "Player10":
var HCID = 'P10HC';
break;
case "Player11":
var HCID = 'P11HC';
break;
case "Player12":
var HCID = 'P12HC';
break;
default:
break;
}
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var the_data = 'player='+ encodeURIComponent(player)
xhttp.open("POST", "find_handicap.php" , true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(the_data);
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
alert('Response from PHP received');
alert(this.responseText);
var hc = parseInt(this.responseText);
document.getElementById(HCID).value = hc;
} else {
alert('failure!');
}
}
};
}
</script>
My PHP file is as follows;
<?php require_once '../../configure.php';
$Player = $_POST["player"];
$Opponent = $_SESSION["Opponents"];
$Venue = $_SESSION["Venue"];
if (isset($_POST["player"])) {
$db_handle = mysqli_connect(DB_SERVER, DB_USER, DB_PASS );
$db_found = mysqli_select_db($db_handle, DB_NAME);
if ($db_found) {
$SQL = "SELECT `handicap` FROM `".DB_AVAIL."` WHERE `player_name`= '$Player' AND `opponents` = '$Opponent' AND `venue` = '$Venue'";
$result = mysqli_query($db_handle, $SQL);
while ( $db_field = mysqli_fetch_assoc($result) ) {
$Handicap = $db_field[handicap];}
}
echo json_encode($Handicap);
}
?>
What I have tried:
In my script I have include two alerts
alert('Response from PHP received');
alert(this.responseText);
The first was to show that I had received a response from the PHP file. This was successful.
The second alert I expected to see a number between 0 and 28, in the case below I expected to see 16 instead I got a lot of code before the number. As follows;
\u003Cbr /\u003E\n\u003Cfont size='1'\u003E\u003Ctable class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'\u003E\n\u003Ctr\u003E\u003Cth align='left' bgcolor='#f57900' colspan=\"5\"\u003E\u003Cspan style='background-color: #cc0000; color: #fce94f; font-size: x-large;'\u003E( ! )\u003C/span\u003E Notice: Use of undefined constant handicap - assumed 'handicap' in C:\\wamp64\\www\\MatchManagementAvail\\find_handicap.php on line \u003Ci\u003E15\u003C/i\u003E\u003C/th\u003E\u003C/tr\u003E\n\u003Ctr\u003E\u003Cth align='left' bgcolor='#e9b96e' colspan='5'\u003ECall Stack\u003C/th\u003E\u003C/tr\u003E\n\u003Ctr\u003E\u003Cth align='center' bgcolor='#eeeeec'\u003E#\u003C/th\u003E\u003Cth align='left' bgcolor='#eeeeec'\u003ETime\u003C/th\u003E\u003Cth align='left' bgcolor='#eeeeec'\u003EMemory\u003C/th\u003E\u003Cth align='left' bgcolor='#eeeeec'\u003EFunction\u003C/th\u003E\u003Cth align='left' bgcolor='#eeeeec'\u003ELocation\u003C/th\u003E\u003C/tr\u003E\n\u003Ctr\u003E\u003Ctd bgcolor='#eeeeec' align='center'\u003E1\u003C/td\u003E\u003Ctd bgcolor='#eeeeec' align='center'\u003E0.1059\u003C/td\u003E\u003Ctd bgcolor='#eeeeec' align='right'\u003E246856\u003C/td\u003E\u003Ctd bgcolor='#eeeeec'\u003E{main}( )\u003C/td\u003E\u003Ctd title='C:\\wamp64\\www\\MatchManagementAvail\\find_handicap.php' bgcolor='#eeeeec'\u003E...\\find_handicap.php\u003Cb\u003E:\u003C/b\u003E0\u003C/td\u003E\u003C/tr\u003E\n\u003C/table\u003E\u003C/font\u003E\n\"16
Hence the reason I got
"NaN"
when I converted the text response to an integer. The Form Input for ID = HCID is type = "Number"
Can anyone suggest what I am doing wrong. I have used similar code for another part of the project and it works okay. Baffled!!!