sub1 is not working it is being called on the click of the button can use please help me how can i get values from php variables to javascript variable,
<html>
<head>
<script language="javascript">
function sub1()
{
var x=<?php echo $present; ?>;
var y=<?php echo $min; ?>;
var z=<?php echo $max; ?>;
change(x,y,z);
alert("hello");
change();
}
function change(var no,var min,var max)
{
alert("hello1");
var x=document.getElementById("show");
x.src='<?php
if($con==true)
{
$cmd="select * from showcase where item_no=3";
if($res=$con->query($cmd))
{
if($res->num_rows>0)
{
while($rw=$res->fetch_array())
{
echo "$rw[1]";
}
}
else
{
echo "no record found";
}
}
else
{
echo "query problem";
}
}
?>';
}
</script>
</head>
</html>
i am working on a slider which changes pic on the click of the button
1 Answer 1
Edit based on suggestion from @DCoder:
You should use json_encode() which is able to output arrays, objects and complex strings safely:
var x=<?php echo json_encode($present); ?>;
You could also just wrap the PHP output in quotes, but you would have to have full control over the contents of the variable. If you know that the variable is always going to be a number, a boolean value or a simple string without quotes, that should be safe too.
var x="<?php echo $present; ?>";var x = <?php echo json_encode($present); ?>;. Not wrapping it in quotes.true" and "a string that does not contain special characters such as newlines or double quotes". As I see it, it's easier to usejson_encodeall the time instead of thinking "do I echo this or encode it first?".