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.)
Related
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>
I have the following problem, the following script sends a keyword a PHP file hosted in another domain (I already added the CROS headers), this PHP returns me some "echos of different variables" (title, thumbnail, url, etc.) And it works but randomly returns me "Undefined variables".
The first thing was to add an if (isset ()) to my variables in PHP and the error does not appear anymore but the results returned by my searches are much smaller (Before adding it averaged 10 to 20 results, Now I get 5 results).
Can this be a problem with my script?
My form.php
<form method="POST" action="" id="form-busqueda">
<input type="text" name="keyword">
<button id="search" name="search">Search</search>
<div id="results"></div>
<script>
jQuery(function($){
var pluginUrl = '<?php echo plugin_dir_url( __FILE__ ); ?>' ;
$('[id^="form-busqueda"]').on('submit', function(e) {
e.preventDefault();
$.ajax({
type : 'POST',
url : 'http://localhost/ladoserver/script.php',
data : $(this).serialize(),
beforeSend: function(){
$('#results').html('<img src="'+pluginUrl+'../../assets/img/loading.gif" />');
}
}).done(function(data) {
$('#results').html(data);
});
});
});
</script>
</form>
My script.php (dlPage is a function that create cURL connection):
<?php
if (isset($_POST['keyword'])) {
$search = $_POST['keyword'];
$html = dlPage("http://example.com/" . $search);
//where I search and get with simple_html_dom example:
$video = $videos->find('div.example2>a', 0);
$title = $video->innertext;
$url = $video->attr['href'];
$id = $video->attr['id'];
$thumbnail = $video->find('div.thumb', 0)->innertext;
echo $title;
echo $url;
echo $id;
echo $thumbnail[0];
}
?>
I've updated my code, I didn't put all the code because I thought that it isn't relevant, my script.php works fine with pure PHP. The problem appear when I use AJAX.
I'm getting the following error:
Notice: Undefined variable: title in C:\xampp\htdocs\webs\ladoserver\script.php on line 13
Notice: Undefined variable: title in C:\xampp\htdocs\webs\ladoserver\script.php on line 13
Notice: Undefined variable: url in C:\xampp\htdocs\webs\ladoserver\script.php on line 14
The undefined variable is coming from your PHP file (/ladoserver/script.php).
What generates the variables being returned? The most common "cause" of this, is by only setting the variables within a block of code that might not be executed (eg within an if block, or in a loop that iterates 0 times)
You could get around the error (assuming you're okay with blank values) by defining each of the variables at the top of your script.
<?php
$title = "";
$thumbnail = "";
$url = "";
$id = "";
?>
Edit: #snip1377 reminded me that you can also just use isset at the end of your script before the output as well.
Here's some sample code for your $thumbnail variable, which you could apply to all your variables being returned
<?php
if (isset($thumbnail))
{
echo $thumbnail;
}
else
{
echo "";
}
?>
Alternativaely, you can use a ternary operator:
<?php
echo (isset($thumbnail)) ? $thumbnail : '';
?>
Edit again: just to illustrate what I mean about how the variables might not get defined within a script, here is an example that could cause that undefined error:
<?php
if ($_POST['value'] == 1)
{
// This will never be reached unless $_POST['value'] is exactly 1
$return_val = 1;
}
echo $return_val;
?>
This will give the undefined warning, if $_POST['value'] is anything other than 1.
Similarly, if $_POST['value'] were 0 in the following code, it would have that undefined warning as well:
<?php
for ($i=0; $i<$_POST['value']; $i++)
{
// This will never be reached if $_POST['value'] is less than 1
$return_val = $i;
}
echo $return_val;
?>
In the examples above, you can simply define $return_val at the top of the script, and you won't get the error anymore.
You send this data as a post method.you shuld echo them with $_post['name'] but you just echo $name
Use this in script.php :
<?php
echo $_POST['title'];
echo $_POST['thumbnail'];
echo $_POST['url'];
?>
I am trying to load some select options by using a few JS functions. I want to have one option selected by default if it is equal to a PHP variable defined before.
I receive an error:
Unexpected token
and I am sure I am doing something wrong with the syntax:
This is a section of my JS functions:
for(var i = 0; i < categories.length; i++){
select.options[i] = new Option(categories[i].val,categories[i].id);
if (select.options[i].text== <?php echo '$categoriesSelect'; ?>)
{
select.options[i].selected=true;
}
}
The variable $categoriesSelect is defined before the JS. Thank you!
You need to quote your strings.
Change (from your comment):
if (select.options[i]== <?php echo "json_encode($categoriesSelect)"; ?>)
to:
if (select.options[i].text == "<?php echo $categoriesSelect; ?>")
This will validate against the text for the option. Change .text to .value to validate against value for the option.
...also removed json_encode() since the variable only contains a string.
You need to remove quotes around php function:
From
if (select.options[i].text== <?php echo '$categoriesSelect'; ?>)
To
if (select.options[i].text== '<?php echo $categoriesSelect; ?>')
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); ?>;
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>