php get a html element to be a php variable - php

i have an h1, which i want to get the text from and then exacute it as an echo. for example something like this - http://jsfiddle.net/mZGH7/.
<html>
<head>
<title><?php echo (".example"); ?></title>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
</body>
</html>​

While you could do that with PHP's DOM extension and XPath:
<?php
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile(__FILE__);
$xpath = new DOMXPath($dom);
?>
<html>
<head>
<title><?php echo $xpath->evaluate('string(//h1[#class="example"])'); ?></title>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
</body>
</html>
or with a third party lib implementing Selectors, like ZF's DOM Query component:
<?php
libxml_use_internal_errors(true);
$dom = new Zend_Dom_Query(file_get_contents(__FILE__));
?>
<html>
<head>
<title><?php echo $dom->query('.example')->current()->nodeValue; ?></title>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
</body>
</html>
it is completely pointless to do it that way, because it takes much more time and is much more complicated than what you really should do, namely
<?php $title = 'this is going to be the title'; ?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1 class="example"><?php echo $title ?></h1>
</body>
</html>​
If that is not an option, use JavaScript and make sure to read:
http://www.developer.com/tech/article.php/923111/Client-side-Versus-Server-side-Coding---Part-1.htm

I think you are mixing php with javascript. Something like this works:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
<script>
var salida = $(".example").html();
$('title').html(salida);
</script>
</body>
</html>

Quick Short answer
Create a html hidden field, can be accessed, both, by JavaScript, and PHP, at the same time.
Extended Boring answer
Developing a web page is different than developing a desktop application.
When a html page has controls, like text, radiobuttons, checkboxes. And the web page was generated dinamically using PHP. And the user sends them back to the server, the values can be accessed like variables in php.
There is something called "hidden field" that acts like a variable, or control, but, its contents, its not displayed, as it happens with a textbox control.
Remember that a webpage is executed 2 times, first, generated at the server, using PHP, HTML or another web language, and later, at the client's browser, with HTML, and JavaScript, if there is any.
Summary
You may want to learn about how html controls are generated and read back int PHP, and access with Javascript.

as everyone else already said, it's not the best way to do it (using PHP, you should rather use javaScript if there aren't more complexities involved). that being said, PHP allows you to do it, by use of output control functions, like so :
<?php
function callback($buffer)
{ //using Gordon's answer ( http://stackoverflow.com/a/9245074/487891 )
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($buffer);
$xpath = new DOMXPath($dom);
$buffer = str_replace("{{my_title}}",$xpath->evaluate('string(//h1[#class="example"])'),$buffer);
return $buffer;
}
ob_start("callback");
?>
<!-- and then your html -->
<html>
<head>
<title>{{my_title}}</title>
</head>
<body>
<h1 class="example">this is going to be the title</h1>
</body>
</html>​
<?php ob_flush(); ?>
i haven't tested this, so there might be errors in this code, but this would be the basic idea

It appears to me as if you are trying to run PHP client side. PHP is server side. It generates the HTML and passes it to the browser. Once the HTML is generated, you then can use Javascript to manipulate the DOM. For that, you'll really want to use jQuery.

I can't think of a way to do that in PHP, but some some simple Javascript should do it. Something like this:
document.title = document.getElementsByTagName("h1")[0].innerHTML;​

Related

PHP not putting into head?

I'm trying to make a simple php script that puts a list in the header, here is what I have
<html>
<head>
<?php
function menu1($link1, $button1) {
echo "<ul><li><a href = '$link1'>$button1</a></li></ul>";}
menu1('index.php','Home')
?>
</head>
<body>
<p>Home</p>
</body>
</html>
Yet for some reason when I run the script it outputs this
<html>
<head>
</head>
<body>
<ul><li>Home</li></ul>
<p>Home</p>
</body>
</html>
Is there any way I can get this to post in the head?
Thanks in advance!
The <head> element can only contain certain special elements, such as <title>, <link>, and <meta>, among others. It cannot contain any elements which are rendered on the page.
If you view source, you'll see that the markup is being output the way you requested. However, this markup is invalid, and is being "repaired" by the browser by moving these elements into the <body>.

Process PHP and print to string

I've got a PHP page (content.php), containing plain HTML and content delivered by PHP variables
<html>
<head>
</head>
<body>
<h1><?php echo $title; ?></h1>
</body>
</html>
Then there is another PHP page, where I need the contents of content.php in a String, but already processed by the PHP parser. I already tried the file_get_contents() function, but this gives the raw output (PHP not processed). What I'm looking for, is this:
$var contents = somefunction('contents.php') with content:
<html>
<head>
</head>
<body>
<h1>Title</h1>
</body>
</html>
Any help much appreciated!
Try:
ob_start();
include ('contents.php');
$html = ob_get_clean();
ob_end_clean();
Have you tried include? Alternatively, if you need to get it from "outside" (like if you loaded it in your browser) use file_get_contents with the full http://example.com/filename.php URL.
either i am missing something badly here or else instead of using a function, in your second page why not just directly assign the contents to a string like this:
$my_String = "<html>
<head>
</head>
<body>
<h1>". $title ."</h1>
</body>
</html>";

Using better includes for pages?

As of right now, the way I use includes is to bring the header, footer, and some content for other pages.
This leads to more includes then I really want, because I need to add more content for the includes.
For example:
<!DOCTYPE html>
<?php include('header.php'); ?>
<body>
<?php include('body-top.php');
custom html
</?php include('footer.php');
</body>
It would be nice if I could add variables to the includes and on the pages I want the includes to show.
I am not good at PHP at all, so is there a better way to use Includes?
This can be easily done:
index.php
$title = 'Hello World!';
include 'content.php';
content.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body></body>
</html>
The problem with this approach is, you'll soon run into problems keeping track what went where, so using functions as suggested in other answers might be a good idea. However, for small projects it's IMHO good enough.
sounds like a job for Smarty
It looks like this
<?php
require 'Smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->assign('title','Hello World');
$smarty->assign('hello','Hello World, this is my first Smarty!');
$smarty->display('test.tpl');
?>
test.tpl
<html>
<head>
<title>{$title}</title>
</head>
<body>
{$hello}
</body>
</html>
Or even better way, use some of the PHP MVC frameworks, which will give you even more stuff (not just template system)
Your includes are already very few, no need to optimize them.
Also don't pay attention to people suggesting Smarty or MVC's because that will increase dramatically the number of includes (in exchange for other benefits, of course)-
You can turn your included files into functions. PHP has a neat trick where anything between curly-brackets (i.e. { and }) is only executed when that part of the code is reached. This includes the HTML code outside of your PHP tags.
This could be our 'header.php' file, where we wrap our current code in a function.
<?php function doHeader($title) { ?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<?php } ?>
Then we make a tester for it. Whatever our tester/caller chooses to pass as $title shows up in our output.
<?php
// All included here
include_once('header.php');
?><!DOCTYPE html>
<?php doHeader('My page title'); ?>
<body></body>
</html>
This produces the output,
<!DOCTYPE html>
<html>
<head>
<title>My page title</title>
</head>
<body></body>
</html>

How to change HTML title in PHP without breaking the XHTML markup validation?

Here is the structure of the web site:
PHP index file
//my class for analyzing the PHP query
$parameter = new LoadParameters();
//what this does is it accesses the database
//and according to the query, figures out what should be
//loaded on the page
//some of the things it sets are:
// $parameter->design - PHP file which contains the design
// HTML code of the page
// $parameter->content - Different PHP file which should be loaded
// inside the design file
$parameter->mysqlGetInfo($_SERVER['QUERY_STRING']);
//load the design file
include($parameter->design);
PHP design file
Just the generic structure. Obviously it has a lot more design elements.
<html>
<head>
...
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
Question
So here is the problem I experience. The $parameter->content file is a dynamic PHP file, meaning the content also changes according to the query.
For instance if I have a image pages with queries like ?img=1 and ?img=2, my LoadParameter class will only look at the img part of the query and will know that the content of the page should be image.php. image.php however will look at the query again and figure out exactly what image to load.
This causes issues for me because I want to have a different <title></title> for different images. So my solution was just to set the <title></title> element in the content page. This works but it breaks the XHTML markup validation at W3C because it makes the structure of the site to be the following:
<html>
<head>
...
</head>
<body>
...
<title>sometitle</title>
...
</body>
</html>
And having <title></title> within <body></body> is not allowed.
So how can I change the title without breaking the XHTML markup validation?
Note: I can't use javascript because then Search engines would not be able to see the title of the page. I need to do it directly in PHP.
Thanx in advance.
why not do a second include to perform the title in the proper place?
<html>
<head>
<?php
inlcude($parameter->title);
?>
...
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
Can't you just change the PHP code so that you can do something like:
<html>
<head>
<title><? print($parameter->title); ?></title>
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
I'd move all of the <head> code into a 'common function' called something like html_head($title) and then have it put the title where it belongs.
Then simply call that function from within the pages and it's fixed.
Don't forget to include the <body> tag in that function, otherwise it won't work!
Elaborating ;)
function html_head($title) {?>
<html>
<head>
<title><?=$title?></title>
<!-- Put whatever you want... here! -->
</head>
<body>
<?}
Then in $parameter->content, call html_head("Title")
It would be easier if $parameter->content could be included without displaying its HTML code, but instead have a $parameter->display (or similar) function that displays the HTML code. That way, you can include the PHP code at the beginning of the file and not worry about being unable to access the title.
<?php
require_once($parameter->content);
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title><?php echo $parameter->title; ?></title>
</head>
<body>
<?php
echo $parameter->display;
?>
</body>
</html>
This is how I solved the issue.
I changed the PHP design to something like:
//get the content PHP file
//inside the file I set the following variables
//which are used below:
//$parameter->title - the string which contains the title
//$parameter->html - the string which contains the HTML content
include($parameter->content);
//string which will contain the html code of the whole page
$html = <<<EndHere
<html>
<head>
<title>
EndHere;
//add title
$html .= $parameter->title;
$html .= <<<EndHere
</title>
</head>
<body>
EndHere;
//add the content of the page
$html .= $parameter->html;
$html .= <<<EndHere
</body>
</html>
EndHere;
//output the html
echo $html;
And here is the basic structure of the Content PHP file. Since the only page which can possibly include the file is the my design page, I can reference $parameter in it.
//set the title
$parameter->title = "sometitle";
//set the content HTML
$parameter->html = "some HTML here";
It's not a very clean solution but it works fine.

Is it possible to write the contents of b.aspx on a.aspx?

Here is the scenario: I have two asp pages. a.aspx is layout and b.aspx is content. I want to display the contents of b.aspx inside a <div> on a.aspx. I know with PHP you can do it like so:
//a.php
<html>
<head>
<title>test</title>
</head>
<body>
<?PHP
include "b.php";
?>
</body>
</html>
//b.php
<?PHP
echo "Content String";
?>
//result
<html>
<head>
<title>test</title>
</head>
<body>
Content String
</body>
</html>
Thanks!
This scenario is handled by masterpages and or composing the page out of (user)controls in ASP.NET. As described at for instance here.
Probably Server.Execute will help.
//a.aspx
<html>
<head>
<title>test</title>
</head>
<body>
<% Server.Execute("b.aspx"); %>
</body>
</html>
//b.aspx
Content String
//result
<html>
<head>
<title>test</title>
</head>
<body>
Content String
</body>
</html>
By the way, I do not recommend this approach. It's just to show it can be done. Master pages and user controls are normally the way to go.
create a B.ascx that does everything you need, and then both B.aspx and A.aspx can include that control.
It sounds like MasterPages will accomplish this for you. Is this not an option for you?
you can go old-skool and use an IFRAME
alternatively, could use a WebRequest in a.aspx.cs to open b.aspx, store the results in a string, and return that string inside a div on a.aspx

Categories