I keep getting an undefined index error

I have two php pages. One controls the form while the other displays a table. What my question is that I have spinner in the form which determines the number of questions, whatever number is displayed on that spinner, when the user submits it should display that same number of rows in the table.

for example if the Spinner = 25,then it should display 25 rows in the table and stating in the rows 1,2,3,4…up to 25 for the Question Id (<td class=“qid”>).

My problem is that I am getting an undefined index error everytime stating Notice: Undefined index: txtQuestion… on line 42. I don’t know why I am getting this error though as it should be able to find the index but for some strange reason it can’t find the index. Do you know why?

Below is code for the spinner and submit button (create_session.php)

 <form action="QandATable.php" method="post">
  <th>Number of Questions:</th>
                <td class="spinner"><textarea class="spinnerQuestion" id="txtQuestion" name="txtQuestion" cols="2" rows="1"></textarea></td>
                <td><button class="scrollBtn" id="btnQuestionUp" type="button"><img src="Images/black_uppointing_triangle.png" alt="Increase" /></button>
                <button class="scrollBtn" id="btnQuestionDown" type="button"><img src="Images/black_downpointing_triangle.png" alt="Decrease" /></button></td>
                </tr>
                </table>
                <div id="numberAlert"></div>
                <p><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" onClick="myClickHandler()"/></p>      <!-- Prepare Questions here-->
    
        </form>

Below is code for the table (QandATable.php)

<table border=1 id="qandatbl" align:center;>
    <tr>
    <th class="col1">Question No</th>
    <th class="col2">Option Type</th>
    <th class="col1">Duration</th>
    <th class="col2">Weight(%)</th>
    <th class="col1">Answer</th>
    <th class="col2">Video</th>
    <th class="col1">Audio</th>
    <th class="col2">Image</th>
    </tr>
    <tr>
    <?php
    $spinnerCount =(int) $_POST['txtQuestion'];
if($spinnerCount > 0) {
   for($i = 1; $i <= $spinnetCount; $i++) {
     echo "<td class='qid'></td>";
   }
}
?>
    
    <td class="options"></td>
    <td class="duration"></td>
    <td class="weight"></td>
    <td class="answer"></td>
    <td class="video"></td>
    <td class="audio"></td>
    <td class="image"></td>
	</tr>
	</table>

Undefined index: txtQuestion… on line 42.

You’ve posted only bits and pieces of your code.

Post the code in line 42.

But like I mentioned in one of your other threads, in the past you have copy and pasted your posts on multiple websites so you probably have already got the solution if you have again posted a copy of your post on other websites.

I described in your thread here how to go about debugging your code.

Which one is line 42?

You also have a misspelling in QandATable.php which is going to cause issues

for($i = 1; $i <= $spinnetCount; $i++) {

Hi,

Yeah I knew about the typo, line 42 is $spinnerCount =(int) $_POST[‘txtQuestion’];, It comes up with no error in the error console

Why are you using a textarea for a single, integer-expected value? Use an input type text (or even a Select box) instead.

What does myClickHandler do?

myClickHandler() would check the validation for the form first and if the validation has no errors, then it would open up the pop up window:

The full javascript code below (u don’t need to look at validation code, it is just there so u kno wats in validation() function:

 function validation() {
	 
	 			var isDataValid = true;
	 
	 			var randomStringsO = document.getElementById("randomStrings");
                var btnRadioO = document.getElementsByName("sessionNo");               
                var isbtnRadioChecked = false;;
                var hoursO = document.getElementById("txtHours");
                var minsO = document.getElementById("txtMins");
                var secsO = document.getElementById("txtSecs");
    			var questionNumberO = document.getElementById("txtQuestion");
    			var dateTextO = document.getElementById("datepicker");
    			var timeTextO = document.getElementById("timepicker");
    			var moduleTextO = document.getElementById("modulesDrop");
    			var roomTextO = document.getElementById("room");
    			
    			var errMsgO = document.getElementById("radioAlert");
    			var errDurationMsgO = document.getElementById("durationAlert");
    			var errQuesMsgO = document.getElementById("numberAlert");
    			var errDateTimeMsgO = document.getElementById("dateTimeAlert");
    			var errDateMsgO = document.getElementById("dateAlert");
    			var errTimeMsgO = document.getElementById("timeAlert");
    			var errIdMsgO = document.getElementById("idAlert");
    			var errModuleMsgO = document.getElementById("moduleAlert");
    			var errRoomMsgO = document.getElementById("roomAlert");
                
    if (randomStringsO.innerHTML == ''){
	      errIdMsgO.innerHTML = "Please Select a Session ID";
	      isDataValid = false;
    }else{
	      errIdMsgO.innerHTML = ""; 
    }
    			
    			for(i=0; i < btnRadioO.length; i++){
                    if(btnRadioO[i].checked){
                        isbtnRadioChecked = true;
                    }
                }
                
                if(!isbtnRadioChecked) {
                   errMsgO.innerHTML = "Please Select the Number of Sessions you Require";
                   isDataValid = false;
                } else {
                    errMsgO.innerHTML = "";
                }
                
                   if(hoursO.value == 00 && minsO.value == 00 && secsO.value == 00 ){
        errDurationMsgO.innerHTML = "Please Set a Duration for your Session";
        isDataValid = false;
    } else {
        errDurationMsgO.innerHTML = "";
    }                                

    if(questionNumberO.value == 0){
        errQuesMsgO.innerHTML = "Please Set the Number of Questions";
        isDataValid = false;
    } else {
        errQuesMsgO.innerHTML = "";
    } 
    
     if (dateTextO.value == ''){
	            errDateMsgO.innerHTML = "Please Select a Date";
	            isDataValid = false;
            }else{
	            errDateMsgO.innerHTML = ""; 
            }
            
     if (timeTextO.value == ''){
	            errTimeMsgO.innerHTML = "Please Select a Time";
	            isDataValid = false;
            }else{
	            errTimeMsgO.innerHTML = ""; 
            }
    
    var dateParts = document.getElementById("datepicker").value.split("-");
	var timeParts = document.getElementById("timepicker").value.split(":");

	var valueDate = new Date(dateParts[2], (dateParts[1] - 1) ,dateParts[0], timeParts[0], timeParts[1]);

	if( (new Date).getTime() > valueDate .getTime() ){
   errDateTimeMsgO.innerHTML = "This Date and Time has Passed";
   isDataValid = false;
	}else{
   errDateTimeMsgO.innerHTML = "";
            }
            
          if (moduleTextO.value == ""){
	            errModuleMsgO.innerHTML = "Please Select a Module";
	            isDataValid = false;
            }else{
	            errModuleMsgO.innerHTML = ""; 
            } 
            
            var trimmed = roomTextO.value.replace(/^\\s+/, '').replace(/\\s+$/, '');
            
          if (roomTextO.value == ""){
	      errRoomMsgO.innerHTML = "Please Enter in a Room Number";
	      isDataValid = false;
            }else if (!trimmed.length){
	      errRoomMsgO.innerHTML = "Please Enter in a Room Number"; 
	      isDataValid = false;      
        }else{
	            errRoomMsgO.innerHTML = ""; 
            }
            
            return isDataValid;

            }
            
            function openSessionPopup() {
 			window.open("QandATable.php",
 			'window');  
		} 

document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
    			
    			function myClickHandler(){
     if(validation()){
        openSessionPopup();
     }
}  

No, but it shows me what your problem is.

        function openSessionPopup() {
		window.open("QandATable.php",
		'window');  
	} 

You’re not SUBMITTING the form data. You’re just opening a new window. So of course you’re getting an undefined index when looking for form data… there’s no form data being sent.

But I do have this tag in my php form:

 <form action="QandATable.php" method="post">

Shouldn’t that post the form?

It will post the form, when it’s told to submit. You dont have a submit button, and your javascript on the button you have does not submit the form, so… how exactly is the page supposed to know when you want to submit data?

Hi,

Ok I understand now, Thank You :), but problem is though if I change it from a button to a submit, whenever I click on the submit button, it checks for validation and submits the form and opens up the page togeether, the reason it was a “button” type was so that if the button was clicked on, it will check for validation first, if validated then it will open up window else if not it will stay on same page and dsplay validation messages where neccesary

Use the javascript to submit the form after validation.

Ok then I will do that, thank you :slight_smile:

How can you even ask that when the undefined index error you posted is telling you no form data is being submitted. :lol:

You need a submit button and an onsubmit event handler on your <form>. The event handler validates the form data. If any form data is invalid, the event handler needs to return false to the form and so supress the form submission. If all form data is valid, then the event handler returns true to the form thus allowing the form data to be submitted.