user2350138 user2350138 - 4 months ago 25
jQuery Question

Uncaught Reference Error : <<some value >> not defined Jquery

I know this issue is faced by many pupil. I am not sure what's issue in the code. Although i properly ordered my script tag i got the following issue.

I need to send a value from one PHP file to other PHP. Which inturn used in js. Below is the code.

delete1.php

<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="../../bootstrap/css/bootstrap.css" rel="stylesheet" >
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="../../bootstrap/js/bootstrap.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#container').load('delete2.php?os=' + 'myos');
});
</script>

<title>Cognizant's Asset Tracker</title>

</head>
<body>
<div id = "container" style = "min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>




delete2.php

<?php
$test = $_GET['os'];
echo $test;
?>
<script type="text/javascript">
$(document).ready(function () {
var jstest = <?php echo $test; ?>;
console.log(jstest);
});
</script>


When i executed i got the below output.
enter image description here

I am using bootstrap 3.3.6.
Appreciate your support.

Answer

You have to quote the printed string or it gets printed as a variable name and that undefined variable is throwing the error shown

var jstest = '<?php echo $test; ?>';
Comments