I have a javascript that works fine
<script>
$(document).ready(function(){
$('.delete').click(function()
{
alert('passed');
});
});
</script>
But when I saved all those script to a PHP variable
$phpVariable = "<script>
\$(document).ready(function(){
\$('.delete').click(function()
{
alert('passed');
});
});
</script>";
and echoed the variable (with or without backslash on dollar '$' sign inside javascript)
echo "$phpVariable";
The problem exists. Javascript doesn't work anymore.
Can we save javascript code to php variable without encountering mulfunction?
$phpVariable = '<script>
$(document).ready(function(){
$(\'.delete\').click(function()
{
alert(\'passed\');
});
});
</script>';
echo $phpVariable;
Basically change all " into ' and then escape any ' within the code.
You can not view Javascript in front end..
View source code of page, It already printed...
try this ...
echo htmlspecialchars($phpVariable);
The right answer is here
adding only
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
above the place where you are going to echo the $phpVariableScript
Sorry for some kind of duplicate question.
I found the right answer after I posted the question.
Should I delete this or link to the right answer?
Related
im trying to convert a json string from my .php file using eval() function but it doesnt work. the browser console says
SyntaxError: expected expression, got '<'...
but when i comment out the line where eval() is, and use document.write(data); the string appears...
here's my code..
<html>
<head>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
var go = function() {
$.get("testjson.php", function(data) {
var obj = eval("(" + data + ")");
document.write(obj.name + "<br />");
document.write(obj.date + "<br />");
});
}
</script>
</head>
<body>
<input type='button' value='go' onclick='go()' />
<body>
</html>
and here's the code of my testjson.php file...
<?php
$msg = array(
"name"=>"hi there Victor!",
"date"=>"Monday 21st Feb 2010"
);
$myMsg = json_encode($msg);
echo $myMsg;
?>
im using latest version of jquery..
There are other suggestions in the comments and answers here about using $.getJSON() instead of eval(), or specifying json as a parameter in $.get(), and these are all good suggestions. But, they aren't the reason this isn't working.
Simply, you're missing a semi-colon in your var go = .... function definition after the closing brace near the bottom.
the code is now fixed! lol i don't know why, but i just removed the commented html code under my .php code... how the heck it affects my code in the first place?? it's just a comment... :/
I can't seem to figure it out how to convert the following PHP variable to HTML:
$persondata = "<div id='teach'><h3>Name: " . $row["fname"]. "<br>User Name: " . $row["username"]. "</h3><p>Password: " . $row["upass"]. "</p></div><br>";
I wanted to pass that exact data to my HTML page, so that I can use JavaScript's getElementById function to insert it into a selected data field.
I asked a similar question here, which helped me work out some of logic I need, but I can't work out this part of the equation.
If you could please let me know of a simple way of going about this, it would be very much appreciated, as I don't even know the keywords to search for.
Hope below code help you:
<?php
$persondata = '';
foreach($rows as $row){
$persondata .= "<div class='teach'><h3>Name: " . $row["fname"]. "<br>User Name: " . $row["username"]. "</h3><p>Password: " . $row["upass"]. "</p></div><br>";
}
?>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#data-row').html("<?php echo $persondata ?>");
});
</script>
</head>
<div id="data-row"></div>
A javaScriptVar equals with an echo in php of your php variable.
That is to put php inside js
<script type="text/javascript">
//Your JS
var jsvar = <?php echo $persondata; ?>
//Your JS
</script>
Put PHP var like above into js.
Continuing from above answer:
That is to put php inside js
<script type="text/javascript">
//Your JS
var jsvar = <?php echo $persondata; ?>;
//Your JS
</script>
Put PHP var like above into js.
If you want to transmit data from a PHP script in a server to a client side HTML page for using in js. Go like following.
First get all your data in PHP in an array and use json_encode() and after that echo that variable.
Now in the client side HTML use jQuery.ajax() and send request to that PHP file in server.
When you have the response in Json use js to fragment it and append
wherever you want.
Above is the basic procedure to send data from PHP to HTML page using JS.
I would like to load the following page inside another page after the main page has loaded. I have the following code:
<script language="javascript">
$(document).ready(function(){
$.get('http://$_SERVER[SERVER_NAME]/X.php?logon=Y&vendorref="<?php .$row_RS_Product['id'].?>"&mode=product', function(data) {
$('#tabs-6').html(data);
});
});
</script>
I am not very familiar with this code so please excuse me if I made the obvious mistake
Could anybody help please as it does work when using file_get_contents in php, but this is slowing the page load down tremendous.
Any help welcome
I think you want to get $_SERVER[SERVER_NAME] from PHP so you need to add <?php echo when creating the url to get from. Also you need to have echo when opening the php tag to echo, not . .
Dot is used to concat string in php like this "aaa" . "bbb" (without opening the php tag)
So you need something like this:
$.get('http://<?php echo $_SERVER[SERVER_NAME]; ?>/X.php?logon=Y&vendorref="<?php echo $row_RS_Product['id']; ?>"&mode=product', function(data) {
$('#tabs-6').html(data);
});
I know there are a lot of questions covering something similar, but I've tried the answers without any luck.
Snippet of PHP:
$usernum_query = "SELECT numPersonID FROM tbllogins WHERE txtUserName='$currentuser'";
if(!$usernumber = $db1->query($usernum_query)){
die('There was an error running the usernumber query [' . $db1->error . ']');
}
while($row = $usernumber -> fetch_assoc()) {
$userIDnum = $row['numPersonID'];
$userIDnum = utf8_encode($userIDnum);
}
Snippet of Javascript:
$(function(){
$("#0").click(function(){
var userIDnum = <?php echo json_encode($userIDnum); ?>;
alert(userIDnum);
The most common answer I've come across says to UTF_encode my variable, which I think I've done correctly. I've also tried:
var userIDnum = <?php echo $userIDnum; ?>;
Which doesn't work.
In my HTML outside of the script,
<?php echo json_encode($userIDnum); ?>
return "90" (with the quotes)
<?php echo $userIDnum; ?>
returns 90 (without the quotes).
Within the script, I get null and no alert box, respectively.
Any ideas as to why the variable isn't passed into the script? Thanks!
edit: tried with quotes and got the same result
[Taken from comments as requested]
If I can make a recommendation, put your value in a data-attribute in the HTML (e.g. <body data-user-id="<?php echo $userId ?>">. This keeps you from mixing code languages together which is hard to read, and would allow your external script to run correctly without making it a PHP page.
As far as IE9, I'll take a quick look. You might want to see how jQuery manages the data attributes. You should, at the least, have access to
domObject.getAttribute('data-user-id').
Yep, just did a quick lookup, and even IE10 doesn't support the dataset feature. So, you'll need to use getAttribute for IE <= 10.
A solution based on a suggestion by calamari:
In the HTML:
<!DOCTYPE html>
<!--[if lt IE 11 ]> <html class="ie10orless"> <![endif]-->
<head>
... more code here ...
And in the script:
var oldIE;
if ($('html').is('.ie10orless')) {
oldIE = true;
}
if (oldIE) {
var x = document.getElementsByTagName("div")[0].getAttribute("data-number");
alert("in IE");
alert(x);
} else {
var userinfo = document.querySelector('#user');
alert("NOT in IE");
alert(userinfo.dataset.number);
}
Hope this helps someone else. Thanks everyone for the other suggestions as well.
There are couple of things to check. Lets say this is your script:
<?php $htmlString= 'testing'; //Lets say this is a snippet of your php
?>
<html>
<body>
<script type="text/javascript"><!-- And this is the snippet of your JS -->
// notice the quotes around the ?php tag
var htmlString="<?php echo $htmlString; ?>"; //Make sure there's double
//quotes around your php
alert(htmlString);
</script>
</body>
</html>
Make sure you have PHP and JS in a file called .php. If its an external .js file, php inside it will not work (you can rename the JS file to a .php extension if you have it set up that way)
Also make sure you have the html, php and js setup the way above. There has to be double quotes around php, for example:
var userIDnum = <?php echo json_encode($userIDnum); ?>;
change this to below:
var userIDnum = "<?php echo json_encode($userIDnum); ?>";
I'm making a website that has 3 documents. The Php document that has all html structure and script, the css document that has no interest in the question, and the javascript document.
I'm trying to input html contents of a javascript variable through .innerHTML statement. And to do that, I need (of course) to code the HTML content, and so I do the code within the javascript file itself. Like so:
document.getElementById("exp").innerHTML = "<div class=\"cell\">\
<div class=\"project_cell\"><img src=\"img.png\"></div>\
<div class=\"project_cell\">text</div>\
</div>\";
And this works. However, the code is obviously not just this. It's a complex HTML structure that I do not wish to see in the javascript file. So I would like to put the HTML content to be inside this variable into a text file, using PHP, and make that variable on the innerHTML statement instead of all the code.
My PHP file is like this.
<html>
<head>
<title>sample</title>
<?php
$filename = "thisishtml.txt";
$doc = file_get_contents($filename);
?>
<script src="javascriptfile.js" type="text/javascript"></script>
</head>
<body>
...call javascript function...
It seems ok to me, but if I do like:
document.getElementById("exp").innerHTML = "<?php echo $doc ?>"
It doesn't work. I've tried simple text and I've tried with a new project, and it works on a new project, but not in this one. What am I doing wrong? I've looked to many questions and tutorials and didn't help and that's why I'm looking for help in here.
Thank you very much for your time. I hope we could solve this.
Chances are you need to escape the contents of $doc for JS output.
document.getElementById("exp").innerHTML = <?php echo json_encode($doc) ?>;
If your javascript code is in a javascript file, PHP does not process that so you cannot execute PHP code there.
So you can either put the javascript code directly into the PHP page
<script type="text/javascript">
function someFunction()
{
document.getElementById('exp').innerHTML = "<?php echo $doc ?>";
}
</script>
or you can do the follow:
<script type="text/javascript">
var doc = "<?php echo $doc ?>";
</script>
Then in the javascript file do:
function someFunction()
{
document.getElementById('exp').innerHTML = doc;
}