at the moment I'm having a tough time with HTML, PHP and Javascript! I'm trying to learn those three scripting languages at a time but there are some issues which I cannot solve on my own.
My problem is this: I've coded a PHP file. The code in it looks like this
<?php
$variable1=300;
$variable2=600;
and so on
echo'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
blabla
else{if
(blah.blah==(something)){**FUNCTION()**;}}
</body>
</html>'
?>
Now the thing is, i want to insert right into that part where FUNCTION() is stated, something along these lines:
$phpvar=20;
HOW can i insert there my PHP variable without getting any errors?!
would be really glad if someone could help me out,
thanks in advance
EDIT:
the problme is I've used the code "ECHO blahblah" so your ideas won't really help me. I've edited my source in order to use your suggestions but still i cannot figure it out...
Now my code looks like:
<?php
$variable1=300;
$variable2=600;
and so on
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
blah
<div id="blah" style="margin:0;padding:0;position:absolute;left:' . $variable1 . 'px;top:' . $variable2 . 'px;width:148px;height:57px;text-align:left;z-index:3;">
</body>
</html>
how can i insert variables to make it really work? does not work..
Well my first suggestion would be to change the way you output your data to the browser i.e.
<?php
$variable1=300;
$variable2=600;
and so on
//instead of doing an echo and echo-ing all the stuff out you just temporarily end your code
?>
then you output your HTML here and your JavaScript code, and you insert a php block when you need to input it into JavaScript like this:
var SomeVar = <?php echo $variable1; ?>;
and so on...
hope this helps
Something like this:
<?php
$variable1=300;
$variable2=600;
// and so on
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head>
<body>
<!-- blabla, some HTML, some JavaScript, etc. -->
if (blah.blah==(something)){
<?php
$phpvar = 20;
// some other PHP code
echo $phpvar;
?>
}
</body>
</html>
There are a number of different ways to accomplish this, and somebody may be able to answer with a way that's more idiomatic in PHP (it's been a while since I've done PHP). Essentially what I'm doing here is a couple of things:
I've separated the PHP code from the HTML markup a little bit. This is often preferred over large echo statements for readability, maybe performance, etc. There's no need to echo large chunks of static HTML, just treat it as regular HTML outside of the PHP tags. (This will make it a lot easier to develop and read as well, since you won't have to escape things or deal with PHP strings in your HTML code.)
I added a second PHP tag for the code that's in the middle of the page. I imagine you'll want to do more than just assign a variable, and you can. Anything you echo in that section of code will end up in that part of the rendered HTML output. You can also write loops around your HTML, which would repeat it just like it was part of the PHP code, etc.
The main thing is to keep the PHP code contextually separate from the HTML/JavaScript that it emits. The PHP code gets processed on the server before the page renders in the browser. Once the page is delivered to the browser, the PHP code is done and effectively no longer exists. At that point it's a plain HTML/JavaScript page being rendered and executed in the browser. While it's not clear what you're actually trying to do in your resulting HTML/JavaScript in the question, you'll want to make sure in your final code you make it clear and easy to understand and don't try to mix JavaScript with PHP. (At least not without understanding the separation and using it accordingly, such as with AJAX calls.)
Not quite sure what you code is doing, but what about this?
<?php
$variable1 = 300;
$variable2 = 600;
$blah = 'something';
$phpvar = 'I am here';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php
if ($phpvar != '') {
?>
<script>
var phpvar = "<?php echo $phpvar?>";
</script>
<?php
}
?>
</head>
<body>
<p>Hello world!</p>
<?php
if ($blah == 'something'){
?>
<script>
function doStuff(){
alert('Stuff done.');
}
</script>
<?php
}
?>
<p>After stuff</p>
<?php
if ($blah != 'something'){
?>
<script>
function noStuff();
</script>
<?php
} else {
?>
<p>No stuff done this time.</p>
<?php
}
?>
</body>
</html>
http://codepad.org/4hbVRJID
Which will output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
var phpvar = "I am here";
</script>
</head>
<body>
<p>Hello world!</p>
<script>
function doStuff(){
alert('Stuff done.');
}
</script>
<p>After stuff</p>
<p>No stuff done this time.</p>
</body>
</html>
Note how I use the PHP tags (<?php ?>) to interpolate the PHP within the markup. This is often done when using PHP as a templating engine (on top of any logic done separately). This way you don't have to deal with the echo, but it can sometimes be hard to read.
Related
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;
I have two scripts that I call with two PHP include() calls. The first starts a session / sets cookies, the second loads one of two JavaScript scripts. To keep things valid, I've been using the two calls but I'd like to just combine them into one.
Current setup (simplified):
<? include "session.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<? include "scripts.php" ?>
...
What I'd like:
<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
...
But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.
So which is better: echo the DOCTYPE, use include() twice, or use include() once and have invalid markup?
EDIT - The whole script (session and javascript) should ideally be fully implementable with one line of code (e.g. the one include())
Use ob_start at first to avoid problems with session_start
<?php ob_start();?>
<!DOCTYPE HTML>
<html>
<head>
<?php include "session_and_scripts.php"; ?>
A way that uses only 1 1file and no additional instructions:
<?php include "session_and_scripts.php" ?>
<!-- more head-stuff-->
</head>
<body>
<!--more content-->
session_and_scripts.php should do the following:
<?php
//do the session stuff
?>
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
//some javascript
</script>
(But I would'nt say it's a good approach)
But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.
Assuming that you do not want to have a valid markup, there is no problem, the only restriction is that session_start is called before any kind of "echo"...
Assuming you want a valid markup using only one include and without echoing the DOCTYPE from the included file, you can save the script text into a php variable and echo it in the main page after the inclusion
//main page
<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<?php echo $script;?>
// session_and_scripts.php
<?php
session_start();
$script = '<blablabla>';
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.
I have actually discovered my problem but I am really want to know why this is an issue. I had two pages form1.php I started a session on that page and hit submit. Then I had a link to session2.php which started that session and was able to pull the information from form1.php. I am just learning about sessions and this was a very simple exercise to learn what a session can do.
Here lies the issue, I had a stylesheet link in my head and it had a blank href well it was href="#" and when that was there the session2.php would not start the session from form1.php and grab the info from the form. Without that href="#" in the style tag it worked fine, and it also worked fine if it was a fake styletag href="something.css" but href="" doesn't work either.
Why is this? I only have those in because its a template I made for workflow, maybe I cant include the css link in my template anymore to prevent future issues.
You can see this site working here, if I haven't explained myself.
form1.php
<?php
session_start();
$_SESSION['name'] = $username;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<!--//CSS STYLESHEETS//-->
<link rel="stylesheet" href="#" type="text/css">
</head>
<body>
Go to session 2
<!--form stuff is in here-->
</body
session2.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
session_start();
$username = $_SESSION['name'];
echo $username;
?>
</body>
</html>
Your second page needs to look like this:
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
$username = $_SESSION['name'];
echo $username;
?>
</body>
</html>
Note that session_start() must appear before any content is printed to the screen.
Per the note on the session_start PHP manual page:
Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
To work like you want it, you need to have started the session first. Sounds simple, because it is. When you say session_start, php then looks for an accepted session cookie first to process content.
From http://php.net/manual/en/function.session-start.php
Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
Are you trying to output stuff to the page before you send the headers? What happens if you put the stylesheet after you call session_start()?
I am a noob to PHP just wondering why are we writing the php script in the body tag of HTML.
I am just taking a word on one page and directing it to another page using the post method. below is the code.
page1.html
<form action="disp.php" method="post">
Enter a word: < input type="text" name="word" />
disp.php
this goes inside the body tag
$word=$_POST['word'];
echo $word;
All your help is highly appreciated.
One way to think of it is that the server dynamically creates an HTML page based on the result of your PHP script. For it to be valid html, you need to write the appropriate tags such as
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body>
...
</body>
</html>
The script can actually go anywhere, but the echo statement is like saying "write something here." If you had it echo something between <title></title> tags, then the result of your PHP statement, in this case $word would appear in the page title. You want it to appear in the body of the page, so you have it echo the variable between <body></body> tags.
Does that answer your question?