Uncaught ReferenceError not defined at HTMLAnchorElement.onclick - javascript

I am getting error in console:
Uncaught ReferenceError: check is not defined
at HTMLAnchorElement.onclick
<a onclick="check();">Payment</a>
In JS I am getting error due to this line, whats wrong in this ?
<script type="text/javascript">
function check() {
{
var label<?php echo $k; ?> = "<?php echo $View; ?>";
}
}
</script>

I just noticed that you have extra set of {} remove the duplicate {}, so your code looks like:
<script type="text/javascript">
function check() {
var label<?php echo $k; ?> = "<?php echo $View; ?>";
}
If the problem is still persistent, would be worth checking if you have defined the function before or after the onclick call, because sometimes the js might not have fully loaded up (should not be the case, but is worth the check).
Hope this helps!

function check() {
var label<?php echo $k; ?> = "<?php echo $View; ?>";
}
Remove double time used {} brackets inside Check() function.
And Check again

You can debug and find solution anyways. But recommended way of attaching an event to a DOM element is using addEventListener
That way you'll not be worried about to declare function before or after the HTML element.
document.querySelector('.foo').addEventListener('click', function(ev){
/*
* ev.preventDefault();
* as we are doing someething "unusual", preventing
* default behaviour might be right option
*/
this.innerHTML = 'Checked';
});
<a class="foo">Foo</a>

Related

Uncaught SyntaxError:Invalid or unexpected token

Here is my code:
<script type="text/javascript">
var logged = "<?php echo $_SESSION['LoginValidation'] ?>"; // check loging or not for voting, favoriting and ...
$(document).ready(function(e) {
$(document).scroll(function () {
var y = $(this).scrollTop();
if (y > 37) {
$("#header").addClass("header_shadow");
}
else
$("#header").removeClass("header_shadow");
});
});
</script>
And it throws this error:
The image seems I'm missing semi-colon in the end of line, But as you see in the code, I've written it.
So what's the problem? How can I fix it?
The error says "Undefined index: LoginValidation", so you'll need to make sure it exists before referencing it. I don't know how you intend your code to work, but you can do something like:
var logged = "<?php echo isset($_SESSION['LoginValidation'])
? $_SESSION['LoginValidation']
: ''; ?>";
This will use the value of $_SESSION['LoginValidation'] if it exists, otherwise it will set an empty string ("").
You have missing semi-colon in php tag echo statement .
var logged= "<?php echo $_SESSION['LoginValidation'] ?>"
Just change
var logged= "<?php echo $_SESSION['LoginValidation']; ?>"
Resolved
Check the session has value or not first like
var logged= "<?php echo (isset($_SESSION['LoginValidation'])?$_SESSION['LoginValidation']:''); ?>"
It will work for you.
If $_SESSION['LoginValidation'] isn't always going to be present, you'll need to at least return null or something to assign to logged.
Fortunately for you, if you'd output this variable properly with json_encode() (which makes it compatible with JSON), this happens automagically!
var logged = <?php echo json_encode($_SESSION['LoginValidation']) ?>;
I'll leave it to you to decide how you want to handle when that variable isn't there. You really should test for it (isset(), !empty(), etc.) so that you don't end up with warnings in your logs. (Also, turn off warnings and error output in your output... dump them into logs instead.)

Change CSS of element selected by class

I have a bit of code to check if the URL of the site is a specific brand name, and then to change the colors of CSS styles depending on the brand. This is what I've got so far:
<?php
$URL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (strpos($URL, 'brandName') !== false){
echo "<script>
jQuery( document ).ready( function () {
var navbar = document.getElementsByClassName('navbar-default');
for(i=0; i<navbar.length; i++) {
navbar[i].style.border-bottom = '#ff6600 solid 5px';
}
} );
</script>";
}
?>
I keep getting back the error:
Uncaught ReferenceError: Invalid left-hand side in assignment
and I'm not really sure what's wrong.
Just for the sake of neatness - assuming this is in a "view" type file. It's far easier to read if you break out of PHP before getting into the JS:
<?php if(strpos($_SERVER['REQUEST_URI'], 'brandName') !== false): ?>
<script>
$(document).ready(function() {
$(".navbar-default").each(
$(this).css({"border-bottom" : "5px solid #f60"});
);
});
</script>
<?php endif; ?>
PHP provides an alternative <?php if(): ?> ... <?php endif: ?> syntax ideally for templates.

jquery + php - Call Function From Another Javascript Wrapper

How could I call function in different jQuery wrapper like this:
<?php
if($trigger != ""){
?>
<script type="text/javascript">
jQuery(document).ready(function($){
var delay = 5000;
setTimeout(rmv, delay); // call the function here
});
</script>
<?php
}
if($triggered != ""){
?>
<script type="text/javascript">
jQuery(document).ready(function($){
function rmv(){ // function to be called
$(".adt_front_wrapper<?php echo $id; ?>").remove();
}
});
</script>
<?php
}
?>
I am trying to call a function which should be separated from the caller like the rmv function in the example code above. How could it be done?
You would need to put that function into a higher (shared) scope. For this example, you could use the window object itself.
window.rmv = function() {
$(".adt_front_wrapper<?php echo $id; ?>").remove();
};
In order to make that work, the declaration and definition of rmv needs to happen beforehand of course.

javascript syntax error, missing ";"

I have a form with potential errors in the form defined in php. I'm used javascript to change the form action depending on whether errors are present or not. I've converted the php error variable, $errors using json_encode so I can use it in javascript. Running the file in Firefox I get the following error in Firebug:
Syntax error: missing ;
before statement var errors = "{"firstnameErr:......etc}, with the pointer at the letter f in firstnameErr. It looks like I have the errors in the json_encode object.
Here is the javascript:
<script type = "text/javascript">
function switchFormAction() {
var errors = [];
var errors = "<?php echo json_encode($errors); ?>";
if(!empty(errors)) {
alert("Please correct these errors");
}
else {
var element = document.getElementById("regForm");
element.setAttribute("action", "serraInsertForm.php");
return true;
}
}
window.onload = function() {
document.getElementById("regForm").onsubmit = function()
switchFormAction();
}
</script>
Probably something simple but I can't work it out. Javascript and json are new to me.
Appreciate any help stackoverflow can offer.
var errors = "<?php echo json_encode($errors); ?>";
^--- ^--
The indicated quotes are not necessary and are in fact causing the problem. json_encode() will produce whatever quotes/brackets are necessary to turn the data in $errors into syntactically valid Javascript. You're producing:
var errors = "{"somekey":"somevalue"}";
^--start string
^--end string
^^^^^^^ undefined variable
All you need is
var errors = <?php echo json_encode($errors); ?>;
Losing the quotes around here ought to do it.
var errors = "<?php echo json_encode($errors); ?>";
Should probably be:
var errors = <?php echo json_encode($errors); ?>;

Uncaught ReferenceError: testString is not defined

This is really weird..
I need to send a couple of variables through to jquery from PHP.. one is an INT and the other a string.
When $a is an INT it works fine but when i use a string, i get this error.. Uncaught ReferenceError: testString is not defined
Here is my code.
<?php $a = 'testString'; ?>
<script type="text/javascript">
var a = <?php echo $a; ?>;
alert(a);
</script>
I assumed that i needed to stick a (int) or (string) before the variable, but i wasn't entirely sure how to and unsuccessful in my googles/attempts.
Any ideas?
You forgot the quotes to make the value of var a a string:
var a = "<?php echo $a; ?>";
What you're writing into the document is:
var a = testString;
so javascript is looking for a variable called testString. Instead, you want the result to be:
var a = "testString";
so make sure you include the quotes around what php is writing in.
There are quotes missing in javascript code:
<script type="text/javascript">
var a = '<?php echo $a; ?>';
alert(a);
</script>

Categories