Select php included html element with jQuery - php

I'd like to select an included html element with jQuery. How can I achive this?
index.php
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
file.php
<?php
echo '<div class="test">test</div>';
?>
select.js
let a = $('.test');
console.log(a); // returns undefined

Try executing your script after the page has fully loaded.
$(window).on("load", function() {
let test = $('.test');
console.log(test);
});

Code should be organized as follows: <?php Php.Code.Goes.Here ?>. This can be surrounded by HTML, for instance, <b><?php print("HELLO!"); ?></b>. This will display: HELLO!
So...you see the problem then, with...
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
The HTML is IN the PHP. You want this:
<html>
<head>
...
</head>
<body>
<?php include('file.php'); ?>
</body>
<html>
This code, of course, shouldn't have executed at all, since <?php <html> isn't really valid PHP syntax.

Related

Add html inside any tag without javascript

Guys, I have a problem. Is there a way to add html inside a tag without using javascript using only php anyway ?. Thank you very much for your help in advance.
For example, there is this code:
<?php
// This part is required here, because she comes another function.
// It's generate from php server, I need to show inside tag body, for example.
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
<script>
$('#my-div').html($('#titulo').html());
</script>
</body>
</html>
The output is this in the source code:
In the browser, the output is this My String:
But, this manipulation is the gift, which uses javascript for this. I don't want it that way. This will not do, because the code will be shown at the top, before the <! DOCTYPE html> tag. Is it possible, on the server, to insert <h1 style ='display:none' id='title'> My String </h1> inside the boby tag, for example?
How I would like it to look:
Example 2
For example, I have this file with code:
file2.php
<?php
include "file2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
I want to show "<h1>My String</h1>" here.
</div>
</body>
</html>
Yes you can do it as simple as this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
<?php
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
</body>
</html>
PHP is a server side scripting language and will only parse PHP code on the server side.
Yes, you can place HTML code inside PHP variables like you have done, but that will get rendered into the client (your browser).
What you can do is place $code variable inside the target div, like this:
<div id="my-div"><?php echo $code; ?></div>
Give it a try

How can I show php variable details inside an html form which is inside an echo php?

I have a form in which some I have defined some variables in PHP, The question is I need these variables details inside an html which is inside an echo . I don't know what to do as I have tried it.
<?php $var= "LOVE" ?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php echo "<!DOCTYPE html>
<html>
<head>
<title>Details</title>
</head>
<body>
<div > <h1> echo $var ;</h1></div>
</body>
</html>";?>
</body>
</html>
You just need to write the variable on double quote. Here an example
<?php
echo "<html>$var</html>"
// OR if one quote
echo '<html>'.$var.'</html>';
?>
<?php $var= "LOVE"; ?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php echo "
<!DOCTYPE html>
<html>
<head>
<title>Details</title>
</head>
<body>
<div >
<h1>".$var."</h1>
</div>
</body>
</html>";?>
</body>
</html>
There is no need of adding <html>,<body> tags inside another. There is already one above in your code. I do not know why you have used this pattern.

how to make php variables show in loaded content

tried to ask this question earlier but made a total mess of it. so thought i'd try it again but clearer this time.
how can you get php variables to display in loaded content using JQuery?
index.php:
<!doctype html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
$(document).ready(function(){
$('#clickMe').click(function(){
$('#parent').load('loaded.php #child', {},function(){
});
});
});
</script>
</head>
<?php
session_start();
$test = "this should display php string";
$_SESSION['another'] = "Session variable String";
echo ' tests to see if they work below <br>';
echo $test."<br>";
echo $_SESSION['another']."<br><br>";
?>
<button name="clickMe" id="clickMe" class="clickMe">Click me</button>
<div class="parent" name="parent" id="parent" style="background-color:yellow; height:200px; width:200px;">
</div>
<body>
</body>
</html>
loaded.php:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div name="child" id="child" class="child">
<p1> html loads fine..</p1><br>
<?php echo $test ?><br>
<?php echo $_SESSION['another'] ?>
</div>
</body>
</html>
As stated by #Fred -ii- in the comments, you only have to fetch the session in your index.php file to do this.
If you want to get a part of another web page inside index.php :
Your index.php should contain this call :
$(document).ready(function(){
$('#clickMe').click(function(){
$('#parent').load('loaded.php'); // No need for additional parameters
});
});
You don't need to select a part of the HTML, return just what you need :
loaded.php :
<?php session_start() ?>
<p>Example text</p>
<?php echo $_SESSION['another'] ?>

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>

php calling html

can you please let me know what I am missing:
<html>
<head><title>page</title></head>
<body>
<h2><center>Welcome to the mainpage</center></h2><br />
Home page
</body>
</html>
php code is :
<?PHP
$currpage=$_GET['currpage'];
echo "Hello world $currpage";
?>
when I click the homepage I want the sample4.php script to be executed and that output which is a html page to be displayed.
But when I click the homage page : I get a file window download.
I have the php script in the same location I have the html?
Does:
<html>
<head>
<title>My Page</title>
</head>
<body>
<p>Home page<p>
</body>
</html>
Do what you want? Otherwise, you'll need to show us the PHP code, in case it's making it a download from within the PHP.
For a hello world program, try this:
<html>
<head>
<title>My Page</title>
</head>
<body>
<?php
$currpage = isset($_REQUEST['currpage']) && is_int($_REQUEST['currpage'])?(int)$_REQUEST['currpage']:1;
echo "<p>Current page is $currpage</p>\n";
?>
</body>
</html>
You may also need to make sure that PHP is installed and running on your machine/server...
<html>
<head>
<title>My Page</title>
</head>
<body>
<?php
phpinfo();
?>
</body>
</html>

Categories