简体   繁体   中英

I get a notice error when I open up my page

Hopefully this question makes sense. I want to display a message using javascript if a room number entered in a textbox is in the database or not. If it is in the database then display nothing (""), if it isn't in the database then display a message "Room is Invalid".

The problem is though when I open up the page where the textbox is stored, it comes up with a notice saying:

Notice: Undefined index: roomChosen in /web/stud/Mobile_app/create_session.php on line 287

Now I believe the reason it is stating this error is because nothing is being posted before this page is opened. This is suppose to post the room number entered in the textbox but obviously the textbox for the room number is on the same page so it can't post it before the page is open. What I want is the user to do this:

step 1: Enter in a room number in the textbox

step 2: Click on the submit button

step 3: Check to see if room number in textbox is in database

step 4: display message depending if room number is in database or not

So what I want to know is how can I store the php coding so that it checks to see if the room number in textbox is in database or not after the submit button is clicked?

Below is html of the room number textbox and submit button:

//textbox where room number is entered in

<p><strong>10: Room:</strong> <input type="text" id="room" name="roomChosen" roomthere="<?php echo $room_there; ?>" />

//message displayed if room number is in database or not

<br/><span id="roomAlert"></span></p>  


// submit button 

<p><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" onClick="myClickHandler()"/></p>

Below is php which checks to if room number in textbox is in database or not:

$room_there = true;

$roomresult = mysql_query( "SELECT Room FROM Room WHERE Room = " . (int) $_POST['roomChosen']);

if( mysql_num_rows( $roomresult ) == 0 ) $room_there = false;

Below is the handler which handles the submit button when it is clicked:

function myClickHandler(){
    if(validation()){
        showConfirm();
    }
}

Javascript validation for room number textbox:

var roomTextO = document.getElementById("room"); 

var errRoomMsgO = document.getElementById("roomAlert");

          if (roomTextO.value == ""){
          errRoomMsgO.innerHTML = "Please Enter in a Room Number";
          isDataValid = false;

            }else if (!trimmedRoomText.length){
          errRoomMsgO.innerHTML = "Please Enter in a Room Number"; 
          isDataValid = false;  

      }else if(roomTextO.getAttribute("roomthere") == false){
          errRoomMsgO.innerHTML = "This Room is Invalid"; 

        }else{
                errRoomMsgO.innerHTML = ""; 
            }

Just check to see if the value is there first before doing all of your logic.

if (!empty($_POST['roomChosen'])) {
    // Check database, and output whatever you need here
}

Also, while you are secure by casting user input to an int now, you might learn to do prepared queries with PDO, so you don't accidentally open up a security hole.

This is a PHP issue that you'll find happening in create_session.php on line 287.

What's hapenning is that you're using an array without initializing it as such.

For example, I could throw this error by typing:

$arr = '';
$arr[ 'notgoingtohappen' ] = 'arr var is a string here, not an array';

It also tends to happen on multidimensional arrays. For example:

$arr = array();
$arr[ 'notgoingtohappen' ][ 'error' ]; 'the first part here is ok, second is not.'

To correct either instance, you just call the var. For instance, from the last example, the following line would have resolved the problem if called before line 2:

$arr[ 'notgoingtohappen' ] = array();

When using a multidimensional array with $variables as keys sometimes you don't know if the array has been initialized or not. In these cases, do something like this to prevent from squashing any array:

if( !is_array( $arr[ 'notgoingtohappen' ] ) ) {
     $arr[ 'notgoingtohappen' ] = array();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM