-1

You can look at the application here: application

I know the code doesn't work in jsfiddle but I have included my code in the jsfiddle so that you can see the whole code and the way it is laid out. jsfiddle

Now if textbox is empty, it displays "false" in alert and displays a message which is fine.

The problem though is this:

If I type the correct or incorrect room number value in the textbox, it always states it is "false" and does not come with a javascript message.

What should happen is if textbox value matches database, then it should alert "true" and display a javascript message "room is Valid", if textbox value doesn't match database then it should alert "False" and display a javascript message "room is Invalid" How can I achieve this?

You can test the application, enter in these figures in the textbox if you wish for testing:

Valid room number CW5/10 , Invalid room number CN2/10.

Below is where error occurs:

 var js_var = [];
<?php while($data = mysql_fetch_assoc($roomquery)){ ?>
js_var.push(<?php $data['roomChosen']; ?>);
<?php }?>

Jsfiddle application url updated above

3
  • This question is difficult to understand, and thus difficult to answer. The clearer your question, the more likely it is that you'll receive good answers. Commented Jan 25, 2012 at 7:26
  • js_var.push(<?php echo $data['roomChosen']; ?>); I think you're lacking the echo. Commented Jan 25, 2012 at 8:04
  • Still displays error, I am also having error on validation not defined everytime I click on "Prepare Questions" button. Please help, I have really been struggling on this for ages, I am really desperate for help Commented Jan 25, 2012 at 8:13

2 Answers 2

2

I think you are lacking echo:

<script>
  var js_var = [];
  <?php while($data = mysql_fetch_assoc($roomquery)){ ?>
  js_var.push(<?php echo $data['roomnumber']; ?>);
  <?php }?>
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

use it as

<script>
var js_var = [];
<?php while($data = mysql_fetch_assoc($roomquery)){ ?>
js_var.push(<?php echo $data['roomnumber']; ?>);
<?php }?>

function checkroomnumber(){
 var roomnumber = document.getElementById("roomnumber").value;
 for (i = 0 ; i<js_var.length; i++){
   if (js_var[i]==roomnumber){
     alert("room is correct");
     return true;
   }
 }
 alert("room is incorrect");
 return false;
}
</script>

<input type="textbox" name="roomnumber" id="roomnumber" />
<input type="button" name="checkroom" value="checkroom" onclick= "checkroomnumber();" />

1 Comment

I am getting an error on a particular code stating invalid regular exression flag w, roomquery is undefined, I have posted the code I am getting an error in on bottom of my question, I have updated jsfiddle so you can see the current code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.