Console Error: when using php script start & srcipt end - javascript

I have the following code which throws the following error:
Uncaught SyntaxError: Unexpected identifier
I know its something simple, cant see it. Thank you in advance.
<?php $this->Html->scriptStart(array('block'=>'scriptBottom','inline' => false)); ?>
var phpRandom = "<?php echo json_encode($randomSponsor); ?>";
<?php $this->Html->scriptEnd();?>

Related

PHP selector in jquery having issue of unexpected token in console

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

variable assignment open_form() codeigniter

just a simple question, can we assigned codeigniter open_form() to javascript var?
i have a code like this:
var openForm = '<?php echo form_open("controller/some_function",
array('class' => 'class_name', 'enctype' => 'multipart/form-data'));?>';
but when i run it, i got error in my console saying:
Uncaught SyntaxError: Invalid or unexpected token
but, when i try this:
var closeForm = '<?php echo form_close(); ?>';
it didn't show any error.
though i guess it's not about syntax error, i still have no idea what is wrong and what happens. can anyone explain?
Yes. You can use like this
var openForm = `<?php echo form_open("controller/some_function", array("class" => "class_name", "enctype" => "multipart/form-data")); ?>`;
openForm += '<?php echo form_close(); ?>';
$("#your_element)id").html(openForm);
In javascript You can't split a string across multiple lines. <?php echo form_open(); ?> add \n at the end which create Syntax error.
As well <?php echo form_open(); ?> adds double quotes, which also ends into escaping issues.
To avoid issues In such cases you can use the template literals which is `
For more details you can visit this site.
I hope it justifies your query.

data.php:1 Uncaught SyntaxError: Unexpected token :

I've that very simple code
<?php
header('Content-Type: application/json');
include 'config.php';
$query = mysql_query("SELECT str_data ,occasione FROM Evento");
$arraydata = array();
$arraynome = array();
while($row = mysql_fetch_assoc($query)){
$arraydata[] = date("m-d-Y", $row['str_data']);
$arraynome[] = $row['occasione'];
}
$datanome = array_combine($arraydata, $arraynome);
echo json_encode($datanome);
?>
but when i use the chrome console on that website
http://www.ldida.altervista.org/calendario/index2.html
it gives
data.php:1 Uncaught SyntaxError: Unexpected token :
why?
You are including data.php as type text/javascript.
Therefore your browser tries to parse it as javascript.
The output of data.php is:
{"04-20-2016":"Compleanno","05-14-2016":"Compleanno","05-03-2016":"Battesimo"}
This is no valid javascript, therefore the error. This is not a php error but a javascript parsing error.
Get rid of <script type="text/javascript" src="js/data.php"></script> from your HTML/source code.
You're calling it via jQuery - you don't need to include it in the page itself.
You're also including it as text/javascript, which it is not.

SyntaxError: missing ; before statement - no obvious error?

I keep getting the above error referring to the second line of my code below and have no idea why! With the below part removed my project works ok with no problems but I keep gettig that error with it in, hence it wont run.
<?php
$myVar = $_POST['dropdown'];
?>
<script type="text/javascript">
var myVar = <?php echo '$myVar';?>
console.log(myVar);
</script>
This is probably your issue
var myVar = <?php echo '$myVar';?>
This should work, assuming you did want the single quotes around the PHP variable, and this also contains the javascript end-of-statement ; :
var myVar = <?php echo "'$myVar';";?>
If you did not want the single quotes then this should do :
var myVar = <?php echo $myVar . ';';?>

How to pass parameters with php in my case

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; ?>'}

Categories