Hello I am a noob in php. But I have something like this:
<div id="signupbox">
<!--A lot of stuff-->
</div>
so I have a php script just above this is check if a $get variable is activation_success, so I would echo a javascript to find #signupbox and change innerHTML, but I also want to include a file called login.php inside #signupbox.
The question is if I changed the div's innerHTML using javascript, the included .php file will also disappear.
In what way can I change the innerHTML and include a .php file inside?
I wanted to include login.php file because it is like a snippet so I can include it somewhere else and if I wanted to change, I can change the included file.
What I kinda what to do is echo('<script>$("#signupbox").html("Thank you!");</script>' . include("login.php"));
Use the load() jQuery method :
html = $('#signupbox').html(); // save the previous html
$('#signupbox').load('login.php', function(){
$(this).prepend(html); // add the previous html in the beginning
});
So, if I'm understanding correctly, you want to change the innerHTML of teh #box div but leave teh code generated by your include?
Why don't you make another, smaller div inside of #box and put only the stuff you want to disappear inside of it, then change just the inner div...
<div id="box">
<div id="innerBox">
<!--all the stuff that was in the #box, except for the include -->
</div>
<?php include "myinclude.php"; ?>
</div>
include.php is not disappearing, php is server side language..
you code is erasing existing content and rewriting.
'.html()' will erase all contents inside the target eleiment, include.php contents will be erased while executing .html() function in clientside
while adding more content in javascript,
$('#signbox').append('Thank you!');
instead of
$('#signbox').html('Thank you!');
hope this will help.
Related
<?php include('header.php'); ?>
<div data-role="page" data-theme="a" id="site-wrapper">
//Here I want to keep appending the information without having to include
//header and footer files in every external page
</div>
<?php include('footer.php'); ?>
Here's where you first land when entering the site:
<div data-role="main">
<div class="center-wrapper" id="landing-container">
<h2>Välkommen</h2>
<p><b>Befintlig användare?</b></p>
Logga in
<p><b>Ny användare?</b></p>
Registrera dig
</div>
</div>
Now if I click on "registrera dig" (register) I want to grab the contents of the file and append it to the #site-wrapper div above. As it works now it grabs the contents and loads a new page without the header and footer files being included since I don't have them as includes in the file. From my understanding I thought that Jquery Mobile would do this automatically but it doesn't, do I have to manually make an ajax call with Jquery and then append the content to the div or is there a way to make this happen automatically using already built-in Jquery Mobile functions? Haven't been able to find an answer from my searches so hopefully I can get one here.
You need AJAX to achieve this.
$( "#site-wrapper" ).load( "folder/page.html", function() {
// Callback function
});
This snipped would solve your problem. Replace the url with your file!
Is there any way to store PHP code in a text file (for a navbar that may change) and access this text file via file_get_contents and add it into a div in a PHP/HTML document?
What you have to do is
<div class="navbar">
<?php include("nav.php"); ?>
</div>
You can use the built in include() function in PHP.
See docs here: http://www.php.net/manual/en/function.include.php
Using this means you essentially read in the contents of the file, and insert it directly where you are making the call. In your case it might be something like:
<div id="menu">
<?php include('partials/menu.php'); ?>
</div>
Whatever you have in the menu.php file (located in partials folder) will be inserted inside the menu div.
As a side note, since it's basically injecting whatever is in the file, you can also use any variables that you set in the menu.php file if need be
Use one of these
www.php.net/include
www.php.net/require
www.php.net/file_get_contents and www.php.net/eval
I was hoping someone could help. I have just started to dabble with PHP includes for time saving in the future. For example I want to change the footer and header on a web page once (using include) instead of copying and pasting the code 30 or 40 times - oh no... a typo start again.
Which brings me to the question(s) where is it best to place this script?
<?php include("includes/headernav.html"); ?>
Can it be placed in a div, or should it be placed at the top of your code under the body?
If I want to make an image/banner include module. Can I
<?php include("includes/image.jpg"); ?>
Or is best to wrap the image in html and apply like this?
<?php include("includes/imagewrapped.html"); ?>
Do not include .jpeg files directly, use a wrapper. Only use include with other PHP files.
As for including the header, do it any way that feels natural as long as it produces valid html. There is no particular reason to declare another div element.
Hope this helps:
<?php include("includes/ui_header.php"); ?>
My page content between header and footer
<?php include("includes/ui_footer.php"); ?>
You can probably save this as a function and call that function wherever you want to display.
It doesn't matter whether you put include in any place. However, it's better to put include in the top or bottom of your code
While including headers/footers/menus on the site, please keep in mind following things:
1) Your header/footer includes(blocks) should be wrapped inside a div.
2) This way then can be differentiated and any new change to them can be done easily.
3) Its always a good practice to include a wrapper div around an element as CSS can use it for styling.
4) Your header/footer includes (blocks) should have a flexibility that even we place them in header,footer or any sidebar, they should not disturb the UI.
1) Because you are including the HTML file, you probably need to include it where you want to display it.
2) Create a function in php where you send only image URL (maybe some other parameters) and function returns the HTML code (String) which you only echo on page where you want to display it. This way you can ensure, that all images will have the same code and styling.
for example
function generateImage($url=null) {
if (isset($url)) return '<img src='.$url.' style="width: 100px; height:100px; border: 1px;" />';
else return false;
}
The better way is to include always a php file.
apology for this newbie question.
I have created an html page (dash.html) that uses the same header as the other pages.
it calls this PHP function <?php include 'header.php'; ?>
the dash.html contains a special <div> made specially for that page; and it must be placed inside the header.php
im trying to figure out how to enable/disable a certain div on a certain html page.
will it require a PHP conditional statement?
Yes. The easiest way is to include a conditional line in the header, and pass the checked variable from each page that calls header. So, in your dash.php (it can't be a .html if it calls php, can it?):
<?php
$includediv = true; // set to false, or leave out, if you don't want the div
include('header.php');
?>
and in the header.php:
<?php
...some other code...
if($includediv){
...code to include div...
}
?>
This will continue to work as before for all other pages that call header.php.
You are trying to show the div only if header.php is present, right?
So, just set a variable inside header.php and use a conditional inside the HTML page.
Try this code
<?php
$path=explode('/',$_SERVER['REQUEST_URI']);
$page=end($path);
if($page=='dash.php')
{
?>
<div>your div for dash.php page</div>
<?php } ?>
I have a page that uses another page via the include() function. The page that requests and uses this external page has some html embeded as well as some output using the echo function. Here is an example of what I am referring to.
page Apple.php contains:
<html>
<head></head>
<body>
<h1>I have some content here.</h1>
<div id="format" style=" width:20px; height:20px;"><?php include("Orange.php") ?></div>
<h2>Some more content here.</h2>
</body>
<html>
page Orange.php contains:
<?php
$astringA='blue';
$astringB='berry';
include("someExternalPage.php");
echo "fruit is ".$astringA.$astringB.$externalVariable;
?>
Whenever the Apple.php is viewed in browser, only the top part of its body is shown(the part above the include function) and everything below the div box is lost and filled with whitespace. In addition the included file ignores all style that the div box has. The same is true for the Orange.php; all output below the include() function is lost. How can I fix this so that the included file will conform to the style I want for it and also to continue to output the rest of the content on the pages?
I also notice that even if I try outputing the rest of the page through php such as
echo ">h2>Some more content here."; (I deliberately inverted the first ">" in the line above so that I illustrate the code here)
the included still takes precedence over the current page and thus the output is never shown.
Smells like you have a hidden fatal error here.
Try using error_reporting(E_ALL) and run the page to see if you have an error...
There's something wrong either with someExternalPage.php or $externalVariable.
If someExternalPage.php is actually an external file (ie you are including a file such as http://example.com/test.php) you may need to check that you have enabled allow_url_include in your php.ini
Alternatively, please make sure someExternalPage.php exists.
Looks like you're missing a semicolon after the include in Apple.php. Can we see the output when you try to load Orange.php? It's helpful to see exactly where the flow is breaking.