Load Html content into jQuery variable - php

I am trying below code to load html content to jquery variable.
<html>
<body>
<div id="static-content"></div>
<?php
$static_content = file_get_contents('cms/content.php');
?>
<script>
$(document).ready(function($){
var static_content = '<?php echo $static_content; ?>';
$('#static-content').load(static_content);
});
</script>
</body>
</html>
I am reading the html from content.php and try to append to div.
Its not working for me. Can anyone please suggest me how to achieve this. Thanks

Try this ajax load concept. Link
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="static-content"> </div>
<script>
$(document).ready(function($) {
$('#static-content').load("content.php");
});

Please try this
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="static-content"></div>
<?php
$myfile = fopen("content.php", "r") or die("Unable to open file!");
$content = fread($myfile,filesize("content.php"));
fclose($myfile);
?>
<script>
$(document).ready(function($){
var static_content = '<?php echo $content; ?>';
console.log('htmltext ',static_content);
$('#static-content').html(static_content);
});
</script>
</body>
</html>

Related

how to jquery select $("#div_id").html(' php echo?

How to change div content by jquery with php echo content?
I have tried :
<script>
$("#div_id").html("
<?php echo "asdddd"; ?>
");
</script>
It show error : Uncaught SyntaxError: Invalid or unexpected token
You Have to try this and its working 100% .
<script>
$("#div_id").html('<?php echo "asdddd"; ?>');
</script>
Try this
<script>
$("#div_id").html('<?php echo "asdddd"; ?>');
</script>
below is my working code
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
</head>
<body>
<div id="div_id"></div>
</body>
<script>
"$("#div_id").html('<?php echo "asdddd"; ?>');"
</script>
</html>

How to get a string from external PHP file and set it as var in JavaScript

I thought this would be simple enough, but I can't get it to work.
I have two files. main.html and data.php
The two files are in the same folder on a server.
I want to get a string from the PHP file and use it in jQuery. In the example below, I want the browser to create a pop-up, with the text "xxxx". I get no pop-up.
data.php
<?php
$var = "xxxx";
echo json_encode($var);
?>
main.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="scripts/jquery-1.11.1.js"></script>
<script>
$(document).ready(function() {
var testString = <?php $var = json_decode(file_get_contents('data.php'), true); echo $var; ?>;
alert(testString);
});
</script>
</head>
<body>
<h1>Test</h1>
</body>
</html>
Any takers?
<!DOCTYPE html>
<html>
<head>
<script>
$(document).ready(function() {
$.ajax({
url:"data.php",
success:function(result){
alert(result);
}
});
});
</script>
</head>
<body>
<h1>Test</h1>
</body>
</html>
The first problem with this is that on many servers .html is not parsed for PHP
The second problem is that the file_get_contents will actually display the full contents of your .php file (including the <?php) which is likely not what you're looking for.
Instead I would use an AJAX request such as http://api.jquery.com/jquery.get/
For this you will need to include jQuery in your <head>
You should get the php data before html initiates, like below
<?php
$var = json_decode(file_get_contents('data.php'), true);
?>
<!DOCTYPE html>
<html>
<head>
<script>
$(document).ready(function() {
var testString = <?php echo $var; ?>;
alert(testString);
});
</script>
</head>
<body>
<h1>Test</h1>
</body>
</html>

Ajax doesnt send data to php script

I want to send id to php script.I think my code is ok, but when i click on tag i get this error:
Notice: Undefined index: id in C:\xampp\htdocs\domaci\cao.php on line 4
here is my html + ajax code :
<html>
<head>
<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$('a').click(function(){
$.post($(this).attr('href'), { id : $(this).attr('id') } );
});
});
</script>
</head>
<body>
<a href="cao.php" id="Barselona" >Barselona</a>
</br>
<a href="cao.php" id="Beograd" >Beograd</a>
</body>
</html>
and this is cao.php:
<html>
<body>
<?php
$id = $_POST['id'];
echo $id;
?>
</body>
</html>
i really need this to get to work, please help me :)
Your $.post ajax is fine. The issue is when the anchor is clicked, you don't preventDefault or return false, to stop the browser redirecting to cao.php. When it redirects to cao.php, it is a GET request, and so triggers the undefined index notice because there is no post data.
Add return false or preventDefault:
$('a').click(function(e){
e.preventDefault();
$.post($(this).attr('href'), { id : $(this).attr('id') } );
});
Look at your browser's console (F12) on the Network tab, to see the response of the ajax.
Try it now.
$(document).ready(function(){
$('a').click(function(){
$.post($(this).attr('data-url'), { id : $(this).attr('id') } );
});
});
</script>
</head>
<body>
<a href="#" data-url="cao.php" id="Barselona" >Barselona</a>
</br>
<a href="#" data-url="cao.php" id="Beograd" >Beograd</a>
</body>
</html>
Your link redirects you, so simpe do this:
e.preventDefault();
Also its better to have variables (for future), here is EXAMPLE
Try this one will work for you:
<html>
<head>
<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$('a').click(function(){
$.post('cao.php', { id : $(this).attr('id') },function(data){
alert(data);
});
});
});
</script>
</head>
<body>
<a href="#" id="Barselona" >Barselona</a>
</br>
<a href="#" id="Beograd" >Beograd</a>
</body>
</html>
AT cao.php page
<?php
$id = $_POST['id'];
echo $id;die;
?>
just try print_r($_POST) in cao.php & check the values are posting
$id=$_POST["id"];

How to assign a PHP variable value to JavaScript variable

In this code which I am posting i have one problem. I want my PHP variable to be stored in JavaScript variable but it shows error. The code is below.
<?php
$name="anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1=.$name.";"
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
Now when I am assigning $name to the variable name1 than I get an syntax error. Please let me know what changes I have to make. So that I get the value of the PHP variable $name stored in JavaScript variable name1.
In javascript with echo version: var name1= "'.$name.'";
<?php
$name = "anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1= "'.$name.'";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
And you can use like var name1= "<?php echo $name; ?>"; seperated html and php
<?php
$name="anurag singh";
?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1= "<?php echo $name; ?>";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
<?php
$name="anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1='.$name.'";"
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
echo it into a script tag
echo '<script> var name = '.$name.';</script>';
You can do it this way -
<?php $name="anurag singh"; ?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1="<?php echo $name ?>";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
You are pasing string $ame not variable as because you have used '...' this let the php know that its string no more variables inside this string.
<?php
$name="anurag singh";
?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1=<?pgp echo $name ?>;
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
try this
$(document).ready(function(){
var name1="'.$name.'";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
you can assign this value like var name= "'. $name.'";

How can incorporate session variable in jQuery

I want to incorporate a session variable $_SESSION['LI'] into the jQuery with an if statement.
So if($_SESSION['LI'] == 'falsus') hide the #feedback else show #feedback.
Thanks
<?php
session_start();
$_SESSION['LI'] = 'falsus';
?><html>
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#feedback').hide();
});
</script>
</head>
<body>
<div id="feedback">Hello</div>
<? print_r($_SESSION);?>
</body>
</html>
Could you just do something like this:
<?php
session_start();
$_SESSION['LI'] = 'falsus';
?>
<html>
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<?php if($_SESSION['LI'] == 'falsus'): ?>
<script type="text/javascript">
$(document).ready(function() {
$('#feedback').hide();
});
</script>
<?php endif; ?>
</head>
<body>
<div id="feedback">Hello</div>
<? print_r($_SESSION);?>
</body>
</html>
This will only add the script to hide the #feedback div when the session variable is "falsus". If it is NOT "falsus", then the whole script block is omitted.
<?php
session_start();
$_SESSION['LI'] = 'falsus';
?><html>
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
<?php if ($_SESSION['LI'] == 'falsus') { ?>
$('#feedback').hide();
<?php } else { ?>
$('#feedback').show();
<?php } ?>
});
</script>
</head>
<body>
<div id="feedback">Hello</div>
<? print_r($_SESSION);?>
</body>
</html>
You can weave your code with php wherever you want.

Categories