0

I have an error in my PHP on this line:

var strRooms = <?=$js_var?>;

It is saying that this has an invalid markup, what does this mean?

You can look at the application here: application

Now if you type in nothing in the textbox, then it doesn't display a message saying "Please Enter in a Room Number", if you type in a invalid room number then it doesn't show a message stating "This Room is Invalid". Why is it not working?

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

So how can this error be fixed and how can the JavaScript validation message appear as they should do?

4 Answers 4

3
var strRooms = <?php echo json_encode($js_var); ?>;

This will guarantee that it'll work, regardless of what type of variable $js_var is.

Sign up to request clarification or add additional context in comments.

5 Comments

That also works when getting rid of the error. Now the last problem is that why is it not showing a javascript validation message if textbox is empty or if wrong room number e.g. 42 is entered in a textbox. The validation message should appear after user has clicked on the prepare questions button. application url has been updated
You keep reassigning $js_var = $data.",";. Try setting $js_var=Array(); before the loop, then $js_var[] = $data; inside your loop. Now, using my answer, you can get the array directly into JS, so you don't need to use .split(",");
so if I don't need var arrRooms = strRooms.split(','), then what do I call var arrRooms? does that become var arrRooms = '<?=$js_var?>';?
Almost. You can move the strRooms and just use arrRooms = <?php echo json_encode($js_var); ?>;
Ok this is fine but I still have a problem with javascript validation not working properly. Do you know how to fix this. Validation does have to be in javascript btw, not php
2

Try this var strRooms ="<?php echo $js_var; ?>";

5 Comments

I have upvoted this answer. The last problem is that why is it not showing a javascript validation message if textbox is empty or if wrong room number e.g. 42 is entered in a textbox. The validation message should appear after user has clicked on the prepare questions button. application url has been updated
@user1163005: Two things to check: 1. Make sure that something really gets returned to $js_var from server side. check your query, do a var dump. 2. do a console.log when you do a split string on strRooms. It probably does something weird when it tries to split using ',' and therefore when you try to match it with existing room data it doesnt match..
You are talking to a web programmer who isn't very experienced, can't we go step by step to make it easier for me to follow what I need to do?
First, when you grab $data from mysql query and as it happens, put var_dump($data) and check if it does return something you want. Second, you can try debug the javascript by using alert(strRooms) or console.log(strRooms), but for now just stick to using alert.. put that right underneath where you split the strRooms with ',' make sure that the code really goes through the conditional if block too on the javascript part.
Hi, I have updated my application which you can see if you click on 'application' on my question. I have alsp updated my jsfiddle so you can see current code. At bottom of my question I have wrote down the situation as it will be too big to put in comment
2

Have you tried quotes?

var strRooms = "<?=$js_var?>";

or

var strRooms = '<?=$js_var?>';

1 Comment

That has got rid of the error thanks :). Now the last problem is that why is it not showing a javascript validation message if textbox is empty or if wrong room number e.g. 42 is entered in a textbox. The validation message should appear after user has clicked on the prepare questions button. application url has been updated
0

Use this in case of empty textbox

var strRooms ="<?php echo !empty($js_var) ? $js_var : 'Error message'; ?>";

1 Comment

I want to use javascript to display message if textbox is empty to avoid page loading. If you look in jsfiddle in question then you can see my javascript validation, can't the javascript validation I have display the message?

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.