I have used below script sometimes it's work but sometimes it's gives error in console that is Uncaught SyntaxError: Unexpected token < on first line.
$(<?php echo '"#'.$itemid.'"'; ?>).click(function() {
$("#options").modal(options).modal('openModal');
});
Even in inspect element in html the result is showing
$("#2735").click(function() {
$("#options").modal(options).modal('openModal');
});
Not getting what is the issue here..i used this script Script is here
i don't know what's happen but in a simple way you can write
$("#<?php echo $itemid; ?>")
is what itemid is always an integer ?
maybe is more secure
$("#<?php echo intval($itemid); ?>")
maybe in some case $itemid is null or empty and the client code will be
$("#")
or maybe an invisible char like \n ?
but i don't think your problem is located here
"Uncaught SyntaxError: Unexpected token < " in first line
Related
I have an array defined in PHP:
$outofstock = array ( 0, 0, 0, 0, 0, 0, 1, 0 );
$_SESSION['outofstock']=$outofstock;
I try to pass it to a JS script this way:
var outofstock = <?php echo json_encode($_SESSION['outofstock']); ?>;
and I get an error with execution stop:
Uncaught SyntaxError: Unexpected token '<' VM563 newhed14.39.js:1
The same statement works in another script within an HTML file.
What am I doing wrong?
Thank you!
The fact that your file is named newhed14.39.js suggests to me that you're serving this particular file as a .js file statically, rather than through PHP. This means that PHP will not run - the PHP commands will end up literally in the file and will thus obviously end up as syntax errors. It's easy enough to check if this is the case - just open up the developer console on your browser and take a look at the file which threw the error; the PHP commands should be visible there (whereas they shouldn't be if PHP had processed the file).
The solution would be to move the variable into the dynamic output generated by a PHP script in a <script> tag, and to edit your JS to consume that variable (for example, by having the <script> tag call a function defined in the .js file).
the reason is that "PHP scripts do not run in .js files". you need to do this:
in your HTML document:
<input type="hidden" id="myArray" value="<?php echo json_encode($_SESSION['outofstock']); ?>">
then you can access this array in js like this:
var array = document.getElementById('myArray').value;
console.log(array)
from this way you access your PHP array in your js. hope it helps ...
I am having Unexpected token b in position 0 from the json that I returned in my php file to my javascript.
$file = array(
array(
'src' => 'http://localhost/grapesjs-dev/uploads/'.$_FILES['files']['name'][0],
'width' => '150',
'height' => '150',
)
);
//var_dump(json_encode($file));
return json_encode($file);die;
I have read something that is related to this and could be the solution but I don't know how to change my php code to match the solution that is in this stackoverflow question. link here guys to the post
image to network in debug tool
UPDATE AND SOLUTION
I deleted all the var_dumps which gave me the error.
Now I'm getting a new error which is Unexpected end of JSON input.
I changed my json format(the arrays).
And used echo rather than return.
Just change return to echo. That should solve the problem
echo json_encode($file);die;
Looking at network screen shot it is clear that you are doing var_dump somewhere in your PHP code which is printing data types rather than actual json object, which javascript is unable to read.
Remove the var_dump and put echo json_encode(your_desired_data) and check it. It should fix the issue.
So lately I've been running a lot of jQuery on my Wordpress sites. Ive been getting the error of "Invalid or unexpected token" a lot, and found what causes it, just maybe looking for a way to fix it.
So basically this doesnt work:
jQuery(".someClass").prepend("
<h2>Some text</h2> ");
But this does:
jQuery(".someClass").prepend("<h2>Some text</h2>");
What makes it a bit hard for me is that my code is sometimes being echo'ed by php, so the above code is a result of this:
$descriptionHeadJQuery = '<script>
jQuery(".portfolio_images").prepend("' . $descriptionHead . '");</script>';
I'm sure I can get all the spacing right and the line breaks to get it to work properly, but I'm also sure that there is someone out here with a better solution for me :)
Thank you
VoidZA
In your PHP, removes all line breaks before you echo:
$lookNoLineBreaksMom = preg_replace( "/\r|\n/", "", $yourString );
as a nifty little function, too:
function look_mom_no_line_breaks($brokenString) {
echo preg_replace( "/\r|\n/", "", $yourString );
// or just return it:
// return preg_replace( "/\r|\n/", "", $yourString );
}
I have tried to get value from file.js and got error:
file.js: this is how i send it:
$.post("site.php",{
whatever: ec
});
site.php: And this is how I tried to get that value:
<button onclick="justdoit();">Click my</button>
<script>
function justdoit() {
var whatever = "<?php echo $_POST['whatever']?>";
alert(whatever);
}
</script>
Got error in console: Uncaught SyntaxError: Invalid or unexpected token on site.php
Edit2: (on comment)
If you have everything in your one page, you never need $.post to go to server
and get back it at from same requesting page. However if you need some server processing and need to fetch the processed data from server then use $.post i.e. ajax request like i answered in edit1
Edit1: (on comment)Changed answer
your site.php (a file just for server side processing) should contain just
$someparam = echo $_POST['whatever'];
Now you should know about $.post
your client side file index.php (a file which you are opening in browser and which shows you your page) should contain following
<button onclick="justdoit();">Click my</button>
<script>
function justdoit()
{
$.post("site.php",{whatever: 'Hi I am a string'; }).done(function(data){
alert(data);
});
}
</script>
Previous answer
site.php
<?php
$someparam = echo $_POST['whatever'];
?>
//off-course above code should not have any error
If it still gives youy problem then there could be somewhere else => In that code which you have not shared. If it works on removing this code. Then you should edit your question and tell the single line which gives error
try this
<button onclick="justdoit();">Click my</button>
<script>
function justdoit() {
var prize = "<?php echo $_POST['prize'];?>";
alert(prize);
}
</script>
u forgot token behind php :P ';'
I am getting an "Uncaught SyntaxError: Unexpected token ILLEGAL" error when try to load a page with a php variable.
$(document).ready(function() {
$.get("index.html", {token:<?php echo $token; ?>}, function(data) {
//codes..
});
});
I can't seem to echo $token in my javascript and I am sure $token is valid.
Can someone help me solve this issue? Thanks a lot!
You're echoing text from PHP into a Javascript context, which means you have to generate valid Javascript code. Simplest solution: json_encode()
{token : <?php echo json_encode($token) ?>}
json_encode() will take care of any quoting/escaping which needs to be done.
e.g. if $token = 'foo', then you'd be producing
{token: foo}
and be producing an undefined variable error.
you need to put quotes around the php output otherwise it will be treated as a javascript variable rather than a string
{token:'<?php echo $token; ?>'}