Click here to Skip to main content
13,868,729 members
Rate this:
Please Sign up or sign in to vote.
See more:
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';
        case "Player2":
            var HCID = 'P2HC';
        case "Player3":
            var HCID = 'P3HC';
        case "Player4":
            var HCID = 'P4HC';
        case "Player5":
            var HCID = 'P5HC';
        case "Player6":
            var HCID = 'P6HC';
        case "Player7":
            var HCID = 'P7HC';
        case "Player8":
            var HCID = 'P8HC';
        case "Player9":
            var HCID = 'P9HC';
        case "Player10":
            var HCID = 'P10HC';
        case "Player11":
            var HCID = 'P11HC';
        case "Player12":
            var HCID = 'P12HC';
        var xhttp;
        if (window.XMLHttpRequest) {
            // code for modern browsers
            xhttp = new XMLHttpRequest();
            } else {
            // code for old IE browsers
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            var the_data = 'player='+ encodeURIComponent(player)
  "POST", "find_handicap.php" , true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.onreadystatechange = function() {
//            alert('Check Xhttp.readyState ===4');
            if (xhttp.readyState === 4) {
//                alert('Check Xhttp.status ===200');
                if (xhttp.status === 200) {
                    // OK
                    alert('Response from PHP received');
                    var hc = parseInt(this.responseText);
                    document.getElementById(HCID).value = hc;
                    // loads handicap to player handicap id
                } else {
                    // not OK

My PHP file is as follows;
<?php require_once '../../configure.php';

$Player  = $_POST["player"];

$Opponent = $_SESSION["Opponents"];

$Venue = $_SESSION["Venue"];

// Check if we have parameter that  being passed to the script through the URL

   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');
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
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!!!
Updated 20-Jan-19 10:23am
Richard MacCutchan 20-Jan-19 12:01pm
The error message suggests a problem with the line:
document.getElementById(HCID).innerHTML = this.responseText;
Which probably means that getElementById(HCID) is not returning a reference. What is the value of HCID?
Dave the Golfer 20-Jan-19 13:11pm
HCID is defined in the SWITCH loop. In this case it is P1HC.
I have inserted "P1HC" in place of HCID and I get a different error suggesting the data being returned is incorrect.
I have modified the code to
var hc = parseInt(this.responseText);
document.getElementById("P1HC").value = hc;
I now get an error stating ;
away12.php:81 The specified value "NaN" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?
The form element with ID P1HC is of type="number".
Suggest I have two problems.

Added alert(HCID); to code. Result is the alert states Undefined. So yes the HCID is not being set in the Switch loop.
Corrected SWITCH loop forgot to put CASE values in between "". Now have the Second problem to resolve.
Thanks for your help on problem one.
Changed code in PHP file from $handicap = $db_field[handicap]; to $handicap = $db_field["handicap"];
Value being correctly transferred.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Corrected Code.
Script Code change;
Corrected SWITCH loop forgot to put CASE values in between "". 

PHP code change.
Changed code in PHP file from $handicap = $db_field[handicap]; to $handicap = $db_field["handicap"];
Richard MacCutchan 21-Jan-19 3:16am
Is the problem now solved?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190214.1 | Last Updated 20 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100