I'm working with XML and PHP to populate a web form drop down box.
I have a html page
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<header>
<h1>Title</h1>
</header>
<form>
<div id ="select xml">
<?php include 'dropdown.php'; ?>
</div>
</form>
</body>
</html>
and I am hoping to include the following PHP to generate the actual box with the file names of the XML.
<?php
//echo(substr(glob("xml/*.xml")[0],4));
echo "<p>
<label>Select list</label><br>
<select id = \"selectxml\">
<option value'0'>--Please Select--</option>";
$count = count(glob("xml/*.xml"));
$files = glob("xml/*.xml");
for ($i = 0; $i < $count; $i++) {
//echo(substr($files[$i],4));
//echo "<br>";
$filename = (substr($files[$i],4));
echo "<option value=$i+1>$filename</option>";
}
echo "</select><br>
<br>
</p>";
?>
I'm aware the PHP isnt perfect, but it works.
The issue is that the Include HTML does not work when I run the page - any ideas?
Change the extension to .php and you will be okay .
When the page have .html extension, the web server doesn't recognize it as a PHP file, and you can't use PHP codes in it. and any PHP code, will be processed as a plain text .
I mean when you put :
<?php echo "hello" ?>
in a HMTL page, browser will show :
<?php echo "hello" ?>
But, if the page have .php extension, browser will show :
hello
Rename your html page from .html to .php
it;s impossible, but with jQuery.load function you can do this :
$("#select-xml").load("dropdown.php");
Related
I want to hide a div from direct users but showing the same div to those who come from example.com
eg.
example123.com/article.php have below div
<div id="main">Title</div>
(when user click on a hyperlink on example.com
Artile
then show the above div
but when user come directly to example123.com/article.php then don't show the div.
how will I do that using php?
Hi you can use the following code like this.
<?php if (isset($_SERVER['HTTP_REFERER'])){ ?>
<div style="width:200px;height:200px;border:1px solid black">
<?php } ?>
You need to make use of $_SERVER['HTTP_REFERER']
I don't totally follow your question but this code should be enough for you to adapt to your needs:
if(strstr($_SERVER['HTTP_REFERER'],'example.com')) {
echo '<div id="main">Title</div>';
}
So, if the referrer URL contains example.com then echo your div.
If the referral URL didn't contain example.com or was empty (i.e. they arrived directly at your site) then the div won't show.
You can achieve this by passing an argument from the URL. The value of the argument will be null if they access the page directly and only have a value if they use the specific URL. Then your PHP can just check the argument and handle it accordingly.
Example as follows.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 01</h1>
Regular URL
<br />
Argument URL
</body>
</html>
Then you can handle the Arguments in your PHP page containing the div
pagewithdiv.php
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 02</h1>
<div id="conditional">
<h2>Conditional Div</h2>
</div>
<?php
if (
// check if argument exists
isset($_GET["Condition"])
&&
// check if argument value is true
trim($_GET["Condition"] == true)
) {
echo '<script>';
echo 'document.getElementById("conditional").style.display = "block"';
echo '</script>';
} else {
echo '<script>';
echo 'document.getElementById("conditional").style.display = "none"';
echo '</script>';
}
?>
</body>
</html>
Keep in mind though that this is only hiding the div, it still exists on the page. If you want it to be completely gone then instead of using javascript to change the visibility you can echo the code that makes up the div if the requirements are met.
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 02</h1>
<?php
if (
// check if argument exists
isset($_GET["Condition"])
&&
// check if argument value is true
trim($_GET["Condition"] == true)
) {
echo '<div id="conditional">';
echo ' <h2>Conditional Div</h2>';
echo '</div>';
}
?>
</body>
</html>
Can some one tell me how to "include" a variable from another .php file without all its other content.
index.php
<?php
$info=file('somedir/somefile.php');
$v1=trim($info[2]);
$v2=trim($info[3]);
$v3=trim($info[4]);
?>
the somedir/somefile.php
<?php
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
All the other content there may not be runned or showed.
?>
Can anybody please help me??
Edit:
Its for my dynamic page.
<html>
<?php
include_once 'config.php';
include_once 'includes/mysqlconnect.php';
$url_slash=$_SERVER['REQUEST_URI'];
$url= rtrim($url_slash, '/');
//$url = basename($url);
$info=file('sites/'.$url.'.php');
$title=trim($info[2]);
?>
<head>
<meta charset="UTF-8">
<title>$title</title>
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/reset.css">
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/<?php echo $theme;?>.css">
</head>
<body class="body">
<div class="container-all">
<?php include_once 'includes/header.php';?>
<div class="container">
<?php include_once 'includes/navigationbar.php';?>
<?php include_once 'includes/rightsidebar.php';?>
<div class="content"><?php
if ($url==''){
include_once "sites/home.php";
}
elseif (file_exists("sites/$url.php") && is_readable('/var/www/html/sites/'.$url.'.php')){
include_once '/var/www/html/sites/'.$url.'.php';
}
else {
include_once 'sites/404.php';
}
?></div>
<?php include_once 'includes/footer.php';?>
</div>
</div>
</body>
</html>
Hope you understand my question now.
Programming is just driving your thoughts :)
So what i want to say that your question is how you can include just some part of an included file and my answer is that you can achieve that by doing a test each time the main file is included from withing this file to see if the file is included internally or not and you can be more precise in a way that you split your main file into block which are loaded due suitable variable
Take a look for this workaround and hope you will understand what i mean
Supposing we have the main file named main.php contains that contents
<?php
echo 'I am a java programmer';
echo 'I know also PHP very well';
echo 'When the jquery is my preferred toast !';
?>
now i have three external files that will include that file each file is specific for one of this 3 programming language
So i will create my 3 files in this way :
File : java.php
<?php
$iamjavadevelopper = 1;
include_once("main.php");
?>
File : phpfav.php
<?php
$iamphpdevelopper = 1;
include_once("main.php");
?>
File : jquery.php
<?php
$iamjquerydevelopper = 1;
include_once("main.php");
?>
and my main.php will be coded in this way
<?php
if(isset($iamjavadevelopper))
echo 'I am a java programmer';
if(isset($iamphpdevelopper))
echo 'I know also PHP very well';
if(isset($iamjquerydevelopper))
echo 'When the jquery is my preferred toast !';
?>
By this way each one of our three external files will show just a part of the included file :)
The only way I can think of without cookies or session's is to make an if condition in the page.
like that:
index.php
<?php include('somedir/somefile.php');?>
the somedir/somefile.php
<?php
if ($pageName != 'somefile.php') {
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
} else {
// All the other content
}
?>
Save the variables in a separate file that can be included separately. Do it the sane way. Structure your code properly, don't try to invent solutions for problems you have because your structure is messy.
I'm using this line of php in my main page
echo generateRadioButtons("fbresponse.php", "moRating1", 6);
Which when posting the following on the response file
echo $_POST['moRating1']
It works fine and displays the correct result, but! my question is how would i add text to that so..
Blah blah blah, you rated x question: 'moRating1'
I've tried doing
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $moRating1 ?></p>
</body>
</html>
inside the response file but that just doesnt load anything..
Any help please!
It's probably because this function uses eval() to execute its content (I guess it from lack of PHP tags in your first example).
If it's true, then you should be able to close PHP tag, print HTML and open it again.
?>
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $_POST['moRating1'] ?></p>
</body>
</html>
try doing:
$mRating1 = $_POST['moRating1'];
...
?>
...
<p>How well did you rate it: <?php echo $mRating1?></p>
I have a site under development with a "News" section and an "Older News" section. The news are stored individually in external PHP files which are loaded by the server by PHP (the server counts the number of PHP files and displays the last one – the current news – in the "News" sections and all the others in the "Older News" section:
<!-- ####### NEWS ####### -->
<div id="news">
<h2>NEWS</h2>
<div>
<a id="showoldnews" href="#news">OLDER</a>
</div>
<?php $directory = "assets/news/"; if (glob($directory . "*.php") != false) { $newscount = count(glob($directory . "*.php")); } else {} ?>
<?php include('assets/news/news' . $newscount . '.php'); ?>
<!-- ####### OLDER NEWS ####### -->
<div id="oldnews">
<h2>OLDER NEWS</h2>
<?php
$newscount = $newscount-1;
while ($newscount>0) {
include('assets/news/news' . $newscount .'.php');
--$newscount;
}
?>
</div>
The "Older News" section is initially hidden and only made visible by a jQuery trigger:
// Show old NEWS
$('a#showoldnews').click(function () {
$('#oldnews').show(400);
});
But I am expecting problems in long-term: since all news are loaded in the first place, this means that with more and more news coming up the website will be loading slower. And even if it's "acceptable", it's still not the ideal solution.
I was thinking on using jQuery.load() to load an external PHP file that would then load the old news only when the user asks for it.
The problem is that I don't know if its possible to load the following script by using jQuery.load() after the user clicks on a link:
<?php
$directory = "assets/news/";
if (glob($directory . "*.php") != false) {
$newscount = count(glob($directory . "*.php"));
}
else {
}
?>
<?php
include('assets/news/news' . $newscount . '.php');
?>
It this approach acceptable? I have tried, but it didn't work at all (I could load static content with the load() function, but I was not able to make the server process the PHP script. I don't know if it's possible to make the server process the PHP code, because PHP processing is done on the the server side before the website begins loading... which is not the case.
UPDATE #1
I have the following that loads the 'load.php' file from the server:
<script type="text/javascript">
$('#oldnews').load('load.php');
</script>
The content of the load.php file is:
<div class="section" id="oldnews">
<h2>OLDER NEWS</h2>
<div class="topLink">
TOP
</div>
<div class="topLink2">
<a id="hideoldnews" href="#news">HIDE</a>
</div>
<?php
$newscount = $newscount-1;
while ($newscount>0) {
include('assets/news/news' . $newscount .'.php');
--$newscount;
}
?>
</div>
The "TOP" and "HIDE" links appear without any problems. However, the PHP block seems to not be processed at all...
PHP can be run before jQuery grabs the content... Here is what I setup and tested...
test.html
<!DOCTYPE HTML>
<html>
<head>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="feeds"><b>45</b> feeds found.</div>
<script type="text/JavaScript">
$("#feeds").load("go.php");
</script>
</body>
</html>
go.php
<?php
echo 'Hi';
?>
And the result is that #feeds .innerHTML is "Hi"... I would love more information/code in replicating the situation. It might help to take a peek at the jQuery load API documentation.
As #joseph told its correct and helped me, but in mycase i wanted to hide the content of the External Php file,
Below is my code changes which had helped me !
<!DOCTYPE HTML>
<html>
<head>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<b>Db file result</b>
<div id="feeds" hidden=""></div>
<script type="text/JavaScript">
$("#feeds").load("db.php");
</script>
</body>
</html>
My Php file had database connection so i had to hide it,
db.php
<?php
session_start();
$db = pg_connect("host=ec2-xxx-x0-xxx-xx.compute-1.amazonaws.com port=5432 dbname=dxxxxxxxxx user=vxxxxxxxxxx password=1xxxxxxxxxxxxxxxxxxxxxxxxxxxxd");
pg_select($db, 'post_log', $_POST);
$query=pg_query("(SELECT id,name, FROM organization WHERE is_active = 'true' AND account_token = '".$_SESSION['account_token']."');
$json=array();
while ($student = pg_fetch_array($query)) {
$json[$student["is_user"]."-".$student["id"]] = $student["name"]."-".$student['pan'];
}
$textval = json_encode($json);
$foo = "var peoplenames=" . $textval;
file_put_contents('autocomplete-Files/'.$_SESSION['account_token'].'.js', $foo);
echo "<script>location='filename.php'</script>";
?>
I like to format all my HTML with tabs for neatness and readability. Recently I started using PHP and now I have a lot of HTML output that comes from in between PHP tags. Those output lines all line up one the left side of the screen. I have to use /n to make a line go to the next. Is there anything like that for forcing tabs, or any way to have neat HTML output coming from PHP?
If there is relative bigger blocks of html you are outputting then HEREDOC syntax would help you format the html the way you want witout bothering much about echo tabs using php.
$html = <<<HTML
<html>
<head><title>...</title></head>
<body>
<div>$phpVariable</div>
</body>
</html>
HTML;
If you use some tool to parse your html , remember it will also add an extra overhead of processing and data payload for each request so you might want to do it only for debug purposes.
There's the tidy extension which helps you to (re-)format your html output.
But it has a little price tag attached to it. Parsing the output and building an html dom isn't exactly cost free.
edit: Could also be that you're simply looking for the \t "character". E.g.
<html>
<head><title>...</title></head>
<body>
<?php
for($i=0; $i<10; $i++) {
echo "\t\t<div>$i;</div>\n";
}
?>
</body>
</html>
or you nest and indent your php/html code in a way that the output is indented nicely. (Sorry for the ugly example:)
<html>
<head><title>...</title></head>
<body>
<?php for($i=0; $i<10; $i++) { ?>
<div><?php echo $i; ?></div>
<?php } ?>
</body>
</html>
<html>
<head><title>...</title></head>
<body>
<?php for($i=0; $i<10; $i++) { ?>
<div><?php echo $i; ?></div>
<?php } ?>
</body>
</html>
Actually this is a good example but in this case it's better to use Alternative way of doing things
<html>
<head><title>...</title></head>
<body>
<?php for($i=0; $i<10; $i++): ?> // notice the colon
<div><?php echo $i; ?></div>
<?php endfor; ?>
</body>
</html>
Reference