PHP error and JavaScript validation - php

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?

var strRooms = <?php echo json_encode($js_var); ?>;
This will guarantee that it'll work, regardless of what type of variable $js_var is.

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

Have you tried quotes?
var strRooms = "<?=$js_var?>";
or
var strRooms = '<?=$js_var?>';

Use this in case of empty textbox
var strRooms ="<?php echo !empty($js_var) ? $js_var : 'Error message'; ?>";

Related

accessing local var value from JS function in PHP

After searching for internet and even here for 5 hours, i am still stuck at getting value of a local variable in a function and sending it to PHP.
I have tried different syntaxes but none of them seems to be working. This code below, takes an input field from PHP and assign some value to it (having problem send this value back to PHP)
$(document).ready(function() {
//select all the a tag with name equal to modal
$('form[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
//Dont go until value is not 7
console.log('i am Now waiting for input');
var s = $('#serial').val();
console.log('searching for ' + s);
while(s.length != 7) return;
//When code value reaches 7
var code = $('#serial').val();
console.log('Value is reached ' + s);
});
});
In PHP
echo "<script>document.write(code);</script>";
Uncaught ReferenceError: code is not defined
please help
You should do somthing like this in javascript when $('#serial') is your input field.
$.ajax({'url':'your_php_file_url', 'type':'post', 'data':{'str':$('#serial').val()}}).done(function(data){
console.log(data.code);
});
And in php
$output = Array('code' => 'not set');
if (isset($_POST['str']))
{
//do your search and assigne value to output
$output['code'] = 'some value';
}
echo json_encode($output);
In short - you send your search string via ajax to php and make your searches. Then you echo it out as json, then ajax will read it as an object and you can use those values you want.
ok, i have almost tested every possible solution but all in vain... so i am off and try to use some normal method rather than javascript. Thanks for all your answers guys.

Error 80020101 on .val() [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could not complete the operation due to error 80020101. IE
I'm facing a little problem with my jQuery code.
I have a first page with some fields like :
[Code 1] [Code 2] [Code 3]
The user type something in one of the input, I get the result that I load in another page with .load method. Here's the code :
$(function() {
$("#code_cip").autocomplete({
source: tab,
minLength: 6,
delay: 1000,
select: function(event, ui) {
var selectedObj = ui.item;
$("#code_cip_ind").val(selectedObj.value);
$("#test").load("test.php?id="+selectedObj.value);
}
});
});
This, works great. It correctly load the "test.php" page with the good values.
BUT... In the test.php page I have a form, and I try to fill it like this :
test.php :
<?php
$gest_medecin = new gest_medecin($db);
$medecin = $gest_medecin->returnValues();
$adresse = $medecin->Adresse;
echo $adresse // This, output the good value of the attribut
?>
<script>
adresse = <?php echo $adresse;?>;
alert(adresse);
$("#adresse").val(adresse);
</script>
<input name="adresse" id="adresse" type="text" />
The problem is :
When I try to alert the "adresse" var, or use it with my .val method, I get the error 80020101 in IE, and in Firefox, nothing is happening...
I tried to fill the variable with some test string like this :
adresse = <?php echo "123456" ?>
And that works great...
As mentioned I checked that the $medecin->Adresse was correct with my echo and it is. It contains something like "word1 word2"
I really have no clue why my code isn't working...
Any help ?
Always look at the final result to spot the error.
You are missing quotes around
adresse = "<?php echo $adresse;?>";
strings always need quotes.
it worked with 123456 because that is an integer value that needs no quotes.
Firefox will tell you this in its error console with a better error message.

javascript typeof doesn't work with PHP variables?

Just wondering, does "typeof" javascript verification (you know, what you do to check for undefined variables in javascript) not work the same way with PHP vars?
I do
alert(typeof <? echo $_SESSION['thing'] ?>);
and this doesn't even execute (I'd figure it would at least pop up as blank)
But if I do
alert(<? echo $_SESSION['thing'] ?>);
the alert says "undefined"
So I'm wondering if the correct way to check for undefined PHP vars using javascript is just to echo them? If it's not please correct me, but if it is I thought it'd be good to share.
Thanks
There's no such thing as PHP variables in JavaScript.
What PHP is doing is "composing" the output text of the JavaScript.
Hit "View source" and look at the final text that PHP put together.
Adding to the answer #Chief17 gave you:
var x_string_val = "<?php echo ((isset($value) AND !empty($value)) ? $value : $default) ?>";
var x_any = <?php echo ((isset($value) AND !empty($value)) ? $value : $default) ?>;
var x_type = typeof(<?php echo ((isset($value) AND !empty($value)) ? $value : $default) ?>);
Be sure to either output quotes from PHP or quote the value in javascript if it's a string. Otherwise it will likely cause more errors.
if(isset($_SESSION['thing'])) {
echo 'thing is set';
} else {
echo 'thing is not set';
}
This is the PHP way to check if a variable has been set.
http://uk.php.net/isset
Edit:
I guess I would do this if I wanted to check a PHP variable in JavaScript:
<?php
echo '<script type="text/javascript">alert("'.(isset($variable) ? $variable : 'Undefined Variable').'");</script>';
?>
The above code checks if the variable is set in PHP, if it is, it echos it out into the alert statement, if it isn't, a blank value is alerted instead.
Just think what would be happening. PHP is executed on the server. The result of the PHP statement is inserted in the HTML/JavaScript that is sent to the client.
So if your session variable contains Foo, the result would be
alert(typeof Foo);
and
alert(Foo);
As you may understand, Foo is not defined in the context of your client side JavaScript.
You need to assign the variable to a javascript variable. If you look at the HTML source after your php has rendered you'll probably see something like:
alert(typeof some value for thing);
which would be a syntax error.
Do this instead
var thing = "<?php echo $_SESSION['thing']; ?>";
alert( typeof thing );
alert(typeof <? echo $_SESSION['thing'] ?>);
This has a bad syntax - it should be alert(typeof(some_variable))
alert(<? echo $_SESSION['thing'] ?>);
This should be like
alert("<?php echo $_SESSION['thing']; ?>");
And what you are trying to do makes no sense because how is js supposed to use variables from different language? o.O

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

PHP jQuery value problem

I have problem to send value from php to jQuery script.
PHP looks like that:
echo "<a id='klik' value='".$row['id']."' onclick='help()' href='http://www.something.xx/tag/".$row['link']."'>".$row['name']."</a><br>";
and script jQuery:
function help(){
var j = jQuery.noConflict();
var zmienna = j('#klik').val();
alert(zmienna);
j.post('licznik.php',{id:zmienna}, function(data) {
alert(data);
});
}
licznik.php
$p=$_POST;
$id=$p['id'];
echo $id;
$wtf = "UPDATE tag_content SET wyswietlenia=wyswietlenia+1 WHERE id='$id'";
$result = mysql_query($wtf);
And as I tested, it has problem at the begining (alert(zmienna); doesn't work, shows nothing). How to fix it?
Thx for help and if u want more informations (like more code etc.) let me know.
{id:zmienna} is not JSON, {'id':'zmienna'} is. Fix that.
The a tag can't have a value. What you can do is pass the id as a parameter:
echo '<a id="klik" onclick="help(\''.$row['id'].'\')">...</a>';
and in Javascript:
function help(zmienna) {
alert(zmienna);
}
That's probably because the anchor tag has no value attribute (http://www.w3.org/TR/html4/struct/links.html). Try this instead:
var zmienna = j('#klik').attr('value');
I would also advice against using value. If I need additional data, I use a tag like data.

Categories