Error while using valiable in Jquery.val() funciton - javascript

I got no output when I use variable in val() function instead of fixed value. How can I fix this error.
Example: In the below example, when I use variable ($data="Glenn Quagmire"), I get no output.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val($data);
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>
Regards

Just use like this:
$("input:text").val("<?php echo $data; ?>");
Full code
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?php echo $data; ?>");
});
});
</script>

Don't use "$" for variables (that's PHP) in jQuery.
<?php echo 'data="Glenn Quagmire";'; ?>
$("input:text").val(data);

JavaScript running in the browser cannot access variables from a PHP program (which has finished running on another computer before delivering the page to the browser so the variable does't even exist any more).
You have to make PHP output the data to the JavaScript.
You can encode it using json_encode (because JSON is (more or less) a subset of JavaScript literal syntax). This will add quotes as needed and escape any special characters in your data (you don't have any in your example, but this is a safe, general approach that protects you from XSS).
$("input:text").val(<?php echo json_encode($data); ?>);

Use <?= $variable ?> to embed variable values in HTML, e.g.:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?= $data ?>");
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>

Related

Hide output from echo json_encode

I was passing an array in php to Javascript using json_encode and ajax. It seems that the only way is to use
echo json_encode($var)
which also prints out the information of $var on the page because of echo. Is there an easy way to hide the output? My code was like the follows,
main.php
<?php
include_once('testing.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="testing.js"></script>
</body>
<html>
testing.php
<?php
$var=array('1','2','3','4','5');
echo json_encode($var);
?>
testing.js
$.ajax({
url : './testing.php',
type : "GET",
dataType : 'json',
success : function (result) {
showstaff(result);
}
});
function showstaff(x){
console.log(x);
}
Running main.php print out the array('1','2','3','4','5') on the page and show the array in the console, but what I need is to just show the array in the console (i.e. hide the results from the page).
You don't need include_once('testing.php') in main.php. When main.php is sent to the browser, it will load testing.js, which will then execute testing.php via the AJAX call.
You can try this:
<script type="text/javascript">
var object = <?php echo json_encode($var); ?>;
</script>
Hope it helps, thanks!

Why PHP not working with Javascript?

A very popular answer in StackOvwerflow suggested that in order to get any php variable within a javascript, simply use php withi angular brackets.
var x= <?php echo "val"; ? >
. However I see that the script given below entirely fails to run If I use this method.
Once I comment out that line script works fine.
<script>
var test=<?php echo "hi"; ?>;
alert(test);
document.title=test;
</script>
Even this simple script is not working.
The page is saved as .php
The page resides in WAMP server and is accessed as localhost/test.php
.
.
EDIT: Full code:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Test</title>
<script>
var test=<?php echo "hi"; ?>;
alert(test);
document.title=test;
</script>
</head>
<body>
<div id="test-canvas" style="float:left;width:100%;height:100%;"></div>
</body>
</html>
I believe your omitted the ' ' in the script
<script>var test = '<?php echo $variable; ?>';</script>
Hope this helps, good luck.
Try this :-
var test = '<?php echo "anything"; ?>';
alert(test);
The result of your php is:
var test=hi;
And that will make a problem because javascript needs quotes too when dealing with strings.
You can do this to solve the issue:
var test=<?php echo "'hi'"; ?>;

PHP code will not execute in script tags

I can put php code anywhere in my php file, but if i put the php code within the tags it simply will not work.... What am I doing wrong?
<html>
<head>
<?php
$hello3 = 'hello3';
echo 'hello1';
?>
<script>
<?php echo 'hello2'; ?>
$(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
</script>
<?php echo $hello3; ?>
</head>
<body>
<button type="button" id="target">Press Me</button>
</body>
</html>
when I execute this page I get this displayed on my screen:
hello1hello3 (then the button)
the echo within the script tags will not execute... at all.
This is the HTML equivalent of your code:
<html>
<head>
hello1
<script>
hello2
$(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
</script>
hello3
</head>
<body>
<button type="button" id="target">Press Me</button>
</body>
</html>
What's wrong with your output?
The output of your PHP, within the script element, looks like this:
hello2 $(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
It immediately falls over (with a reference error) because you try to use the variable hello2 without declaring it.
Were that not the cause, it would then fall over because you try to use the variable $ without declaring it (i.e. by including jQuery in your script).
The PHP is executing fine. You are just writing PHP that generates completely broken JavaScript.
Yes this does execute. But items in the <script> tag are not displayed on the screen unless done so by a function or method. You just have items like hello2; in JavaScript. For example what would you expect so see if you had this:
<script>
hello1;
</script>
Well, nothing would be on the screen. You are just creating global variables, which are not defined, so what would happen? Try adding some code for example a simple alert:
<script>
<?php echo 'alert("hello")'; ?>
</script>
When writing PHP the items you echo would be exactly the same as you were writing JavaScript normally. So the same rules for writing JavaScript apply. The above item would look like so after PHP executes:
<script>
alert("hello")
</script>

How to disable back button in php

<html>
<head>
<script type="text/javasscript">
window.history.forward();
function noback()
{
window.history.forward();
}
</script>
</head>
<body onload="noback();" onpageshow="if(event.persisted) ;" onunload="">
aa
</body>
</html>
This is the code in HTML which forwards you to same page when you click back, but how to include this script in PHP code.
You can for example do something like that:
<?php
Your PHP Code
?>
Your Html code
<?php
Your PHP Code
?>
Your Html code
<?php
Your PHP Code
?>
Or
<?php
Your PHP Code
...
echo "Your HTML code";
...
?>
just change the extension of your file
.html/.htm
to
.php
And put your php code in this .php file in head section

using a php variable in a JS funcion

i previously asked this question but the example i provided did not provide my real problem.
I want to get the input from a form using a php variable and use it in a javascript function. I do not use javascript directly because the content of the variable in question is obtained from a mysql database and i do not think that there is a way for me to access the database using javascript.
below its the code that i have so far:
<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var slon1 = '<?php echo (json_encode($siteLon1)); ?>';
self.alert(slon1);
//some code goes here.
}
</script>
</head>
<body>
<div align="center">
<form method="get">
<label>Choose Site</label>
<select name='ps' id="ps" data-inline="true">
<?php
$use = $fgmembersite->getsites($fgmembersite->UserFullName());
$fields_num = mysql_num_fields($use);
while($row = mysql_fetch_row($use))
{
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
?>
</select>
<input type="submit" name="submit" value="View">
</form>
</div>
<div id="map-canvas"></div>
</body>
</html>
Suprisingly enough, the result displayed on self.alert(slon1) is null; be it when i just load the page or when choose from the drop down box and click on View. Please someone help me on this.
Thanks.
JSON encoder should quote variable, so using single quotes manually would trigger JavaScript parse error. Check JS console. This should generate valid JS code:
var slon1 = <?php echo (json_encode($siteLon1)); ?>;

Categories

Resources