jdock1 Posted July 29, 2010 Share Posted July 29, 2010 How can I add html tags in php as a variable? For example, for some reason my code gives me a header error if I use html before the php tags. I just need to add the style in there. I was wondering how I could add a style of any html tag within php as a variable?? Like I was thinking like <?php $style = "<style> div.error { border: 1px dashed #660000; background: #fee; color: #660000; } </style>"; echo $style; ?> But that would probably just print out the code... how can I make it so it wont print out on screen and I can actually use the style? Sorry for the noob question... im kind of an intermeddiate php programmer but the noobs questions still seem to never go away! Link to comment https://forums.phpfreaks.com/topic/209286-how-can-i-add-html-tags-in-php-as-a-variable/ Share on other sites More sharing options...
jdock1 Posted July 29, 2010 Author Share Posted July 29, 2010 Oh okay I got it, kind of. I got it to apply the style and echo the html tags. Its pretty sloppy tho. Its like this; $html = "<html>"; $head = "<head>"; $endhead = "</head>"; $endhtml = "</html>"; $style = "<style>div.errors { border: 1px dashed #660000; background: #fee; color: #660000; }</style>"; ... // blah blah blah if($current_time - $blocked_time > 3600*24) //24 hours past { $str_sql = "delete from ".$db_table." where ipaddress='".$surfer_ip."'"; mysql_query($str_sql); $str_sql = "insert into ".$db_table." (ipaddress, surf_index) values('".$surfer_ip."', 1)"; mysql_query($str_sql); } else { echo "$html"; echo "$head"; echo "$style"; echo "$endhead"; die ("<div align='center' class='errors'>You have accessed this page too many times. To regain access, purchase a license or wait 24 hours.</div>"); echo "$endhtml"; } } Is there an easier, cleaner way to do this?? Link to comment https://forums.phpfreaks.com/topic/209286-how-can-i-add-html-tags-in-php-as-a-variable/#findComment-1092850 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2010 Share Posted July 29, 2010 You can 't echo it (or anything else, for that matter) before headers are sent. Link to comment https://forums.phpfreaks.com/topic/209286-how-can-i-add-html-tags-in-php-as-a-variable/#findComment-1092852 Share on other sites More sharing options...
jdock1 Posted July 29, 2010 Author Share Posted July 29, 2010 Ok nevermind. Seems like I broke the script by doing that. Damnit!!!??? Link to comment https://forums.phpfreaks.com/topic/209286-how-can-i-add-html-tags-in-php-as-a-variable/#findComment-1092855 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2010 Share Posted July 29, 2010 Typically, this is much more easily accomplished by using include()s. You'd create a header and footer with the html (or whatever) code in it, and include those in your script. Link to comment https://forums.phpfreaks.com/topic/209286-how-can-i-add-html-tags-in-php-as-a-variable/#findComment-1092861 Share on other sites More sharing options...
jdock1 Posted July 30, 2010 Author Share Posted July 30, 2010 None of this is working. Ok, to make it simpler... how can I style a pure PHP page? Im trying to use the styles for the variable $errormsg I created that is printed through die() Link to comment https://forums.phpfreaks.com/topic/209286-how-can-i-add-html-tags-in-php-as-a-variable/#findComment-1092894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.