PHP javascript class variable value in php echo - javascript

this is simple example i want java script class variable to php echo please fix the following example to php echo value out put
<!DOCTYPE html>
<html>
<body>
<div class="example">The Book</div>
<? echo '<div class="example">The Book</div>'; ?>
<script>
function myFunction() {
var x = document.getElementsByClassName("example");
x[0].innerHTML = "Hello World!";
}
</script>
</body>
</html>
out put of java-script "The book" but php echo is '<div class="example">The Book</div>' Targeted out put of php The book

Javascript is run on the client, PHP is run on the server. If you want PHP to use the values calculated in javscript you first have to send it to the server, either through something like AJAX, or perhaps just a query string if you don't mind loading a new page or loading the same page again but with a query string appended to the URL.

Related

Javascript function triggers on page refresh

I have a hyperlink and clicking on that link will run a JavaScript function. I also have a PHP variable $counter. Inside the JavaScript function, the value of $counter is increased by 1 i.e., $counter++. It works fine. But the same function also runs whenever the page is refreshed. So the value of the $counter is increased by 1 whenever the page is refreshed. I tried all the solutions available on the net like preventDefault(), clickevent handler etc., But nothing works. Please help to fix this. In the below code, I have set $counter as 0. But it loads with 1 as output. I want it to count only when the hyperlink is clicked. Here is my sample code.
<?php
session_start();
require("dbconfig.php");
$counter=0;
?>
<html>
<head>
<script>
function onclick() {
<?php
$counter++;
?>
}
</script>
</head>
<body>
link text
</body>
<?php
//tracing counter value
echo $counter;
?>
</html>
TL;DR:
You can't mix PHP and JS code expecting that JS function will execute PHP code.
First PHP prepares output to browser, then Browser parses your JS and HTML. JS never knows about PHP code.
Click CTRL+U to view source as browser sees it - there is no PHP code.
JS function is not run on page refresh. PHP code is run on page refresh.
First goes PHP parser:
session_start();
require("dbconfig.php");
$counter=0;
$counter++;
echo $counter;
Then goes JS/Html parser.
At this point your JS code looks like this:
function onclick() {
}
Content of function is empty because you output nothing from PHP side.
To fix it move PHP var to JS var:
<?php
session_start();
require("dbconfig.php");
$counter=0;
?>
<html>
<head>
<script>
var jsCounter = <?= $counter; ?>
function onclick() {
jsCounter++;
console.log(jsCounter);
}
</script>
</head>
<body>
link text
</body>
</html>
Note never output after closing body tag.

How to convert and transmit PHP Variable data Using JSON to HTML page's Javascript

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.

how to convert javascript variable to php variable in the same file

I have file named test6.php having javascript variable i need to convert this variable to php variable but when i run it give me an e error (PHP Notice: Undefined index: variable in /Applications/MAMP/htdocs/test6.php on line 14)
<!doctype html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var variableToSend = 'foo';
$.post('test6.php', {variable: variableToSend});
</script>
</head>
<body>
<?php
$x = $_POST['variable'];
echo $x;
?>
</body>
</html>
note that i have only one file (test6.php) containing javascript code and php code and i am trying to convert javascript variable to php variable in the same file and i need to use post not get or submit form
Why error
you should know the processing of a php file rendering from the server to the client , it is :
1. client request the page
2. PHP parser (in server) -> php 2 HTML -> send to client
2. web browser load HTML
3. JavaScript running ~
so you can't get variable of "variable" when you run your php file . Becuase Js has not been running ~ . you should print the value of php echo at JS after ajax .
how about change the JQuery code
$.post('test6.php', {variable: variableToSend});
to
$.post('test6.php', {variable: variableToSend} ,
function(returnValue){
console.log(returnValue) ;
});
I think it will be ok , but i am sorry i can't test it ~
Check to see if it is set before you output it
<?php
if( isset($_POST['variable']) ) {
$x = $_POST['variable'];
echo $x;
}
?>
But I do not think this is what you actually want. The Ajax call is not going to update the current view.
Bring the php section to the top and use isset($_POST['variable']) to check if the variable exist or not.
<?php
if( isset($_POST['variable']) ){
$x = $_POST['variable'];
echo $x;
return;
}
?>
<!doctype html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var variableToSend = 'foo';
$.post('test6.php', {variable: variableToSend});
</script>
</head>
<body>
</body>
</html>

Facing trouble to insert php variable in javascript code

I'm trying to insert php variable in a javascript code but unable to get the result. Code is:
<?php $twitterusername = get_option_tree( 'twitter_name', '', 'true' );?>
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=$twitterusername&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
Actually my text editor is not marking $twitterusename inside the script as valid php code. Please help me out.
Try this:
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?php echo $twitterusername;?>settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
As it's a PHP variable, you need to echo it out with PHP, not javascript.
So you need to replace
$twitterusername
with
<?php echo $twitterusername; ?>
Even though it's inside the <script> tags, it's going to echo out whatever $twitterusername is as long as it's in PHP tags.
If your server supports shortcode, you could use
<?=$twitterusername;?>
making it slightly shorter.
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?=$twitterusername;?>settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
It is not valid php code, you need to mark it as so using <?php echo $twitterusername; ?>.
Your final code would look like:
<?php $twitterusername = get_option_tree( 'twitter_name', '', 'true' );?>
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?php echo $twitterusername; ?>&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
You need to use php tags (<? ?>) to insert variable.
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?=$twitterusername?>&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
It's because you are using your PHP variable outside PHP tags.

javascript not calling php variable

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

Categories