user1163005
user1163005

Reputation: 61

javascript and php not quite working

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

Upvotes: 0

Views: 68

Answers (2)

Dau
Dau

Reputation: 8848

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();" />

Upvotes: 0

jerjer
jerjer

Reputation: 8760

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>

Upvotes: 2

Related Questions