ryanmetzler3 Posted December 30, 2013 Share Posted December 30, 2013 I have written code that is a web poll/survey. It consists of 4 files: index.php poll.php jquery-1.3.2.js styles.css These are all saved in a folder called ajax-poll. I tried to include that entire folder on the index.php home page of my website but the css and animation does not work when I do this. If I open the ajax-poll folder independently on localhost then everything works great! It is just when I include the whole folder on another page it fails. The functionality of the poll still works, just the css and animation does not work. Any idea why? Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/ Share on other sites More sharing options...
trq Posted December 30, 2013 Share Posted December 30, 2013 What do you mean by "when I include the whole folder on another page" ? Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1463248 Share on other sites More sharing options...
ryanmetzler3 Posted December 30, 2013 Author Share Posted December 30, 2013 (edited) What do you mean by "when I include the whole folder on another page" ? Thanks for the response. I did this: <?php foreach (glob("ajax-poll/*.php") as $filename) { include $filename; } ?> Basically it loops through to get all the files inside the "ajax-poll" folder and includes them on the homepage of my website. Edited December 30, 2013 by ryanmetzler3 Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1463250 Share on other sites More sharing options...
trq Posted December 30, 2013 Share Posted December 30, 2013 That makes little sense. Why would you include js and css files? include is designed to include php code. Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1463253 Share on other sites More sharing options...
ryanmetzler3 Posted December 30, 2013 Author Share Posted December 30, 2013 That makes little sense. Why would you include js and css files? include is designed to include php code. yea actually I do not know what I was thinking. I will just place the style code from the poll folder into the mail css sheet for the site and see if it works. Thanks, I was just thinking really illogially Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1463258 Share on other sites More sharing options...
ryanmetzler3 Posted December 30, 2013 Author Share Posted December 30, 2013 I have written code to make a poll. You make a choice, then it takes your vote and displays the results. I have it all in a folder called ajax-poll. Like this: Main directory for website: index.php Folder ajax-poll: -main.php -poll.php -jquery.1.3.2.js -styes.css If I go to localhost and open the ajax-poll folder specifically it works great. I then did this on index.php to actually display it on the homepage. <head> <link href="../ajax-poll/styles.css" rel="stylesheet"> <script type="text/javascript" src="../ajax-poll/jquery-1.3.2.js"></script> </head> <body> <?php include "../ajax-poll/main.php"; include "../ajax-poll/poll.php"; </body> Basically I just tried to include all the files inside ajax-poll on the homepage. It works somewhat. The functionality of the poll still works, but the animation I have written into the script does not work. Also the css looks really messed up and it is not sitting inside its border like it should. It still works great if you just open the ajax-poll folder directly. Any idea why or how to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1463324 Share on other sites More sharing options...
requinix Posted December 30, 2013 Share Posted December 30, 2013 Don't use relative paths in your HTML - that means no ".."s. <head> <link href="/ajax-poll/styles.css" rel="stylesheet"> <script type="text/javascript" src="/ajax-poll/jquery-1.3.2.js"></script> </head>Try to avoid relative paths in PHP too, as sometimes the way it locates files may not match what you expect. You can use a leading slash like in HTML, but you need something like DOCUMENT_ROOT before it (otherwise you're telling PHP to look in the root of your entire filesystem, not just the root of your website). <?php include $_SERVER["DOCUMENT_ROOT"] . "/ajax-poll/main.php"; include $_SERVER["DOCUMENT_ROOT"] . "/ajax-poll/poll.php"; Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1463345 Share on other sites More sharing options...
ryanmetzler3 Posted December 30, 2013 Author Share Posted December 30, 2013 (edited) Don't use relative paths in your HTML - that means no ".."s. <head> <link href="/ajax-poll/styles.css" rel="stylesheet"> <script type="text/javascript" src="/ajax-poll/jquery-1.3.2.js"></script> </head>Try to avoid relative paths in PHP too, as sometimes the way it locates files may not match what you expect. You can use a leading slash like in HTML, but you need something like DOCUMENT_ROOT before it (otherwise you're telling PHP to look in the root of your entire filesystem, not just the root of your website). <?php include $_SERVER["DOCUMENT_ROOT"] . "/ajax-poll/main.php"; include $_SERVER["DOCUMENT_ROOT"] . "/ajax-poll/poll.php"; Thanks for the info. I tried this and adapted things like you said, but it is still messed up. I will attach a screen shot of what it looks like. Normally the poll sits inside the square box with the title "User Poll". But for some reason the box contains nothing and it just says loading... Then the poll displays below it outside of its div. If you have any quick ideas, I thank you for them ahead of time! Edited December 30, 2013 by ryanmetzler3 Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1463357 Share on other sites More sharing options...
requinix Posted December 30, 2013 Share Posted December 30, 2013 What is the HTML outputted by all this? Do a View Source with your browser. Would also help to see the CSS and Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1463359 Share on other sites More sharing options...
ryanmetzler3 Posted January 10, 2014 Author Share Posted January 10, 2014 I have a folder containing an ajax poll script. I have the folder saved as "ajax_poll_1". It takes the poll question, potential answers, and votes from a database called "poll1". I included this on my homepage like so: <div class="poll1"> <?php include $_SERVER['DOCUMENT_ROOT'] . '/ajax_poll_1/index.php'; ?> </div> This displays the poll on the homepage and it works great. I copied the entire folder because I wanted 2 polls on my homepage. I renamed the copied folder "ajax_poll_2". I created a second database to contain the data for the second poll and made slight adaptations in the code since the folder was renamed and it was linked to a new DB. The new poll does work when you open the folder directly like so: localhost/ajax_poll_2. If I try to include the second poll on the homepage it does not show up correctly. It shows a grey box that the poll is supposed to sit in, but does not display the text. I know the poll works, and I am essentially including it the exact same way as the first poll so I can't figure out why its failing. <div class="poll2"> <?php include $_SERVER['DOCUMENT_ROOT'] . '/ajax_poll_2/index.php'; ?> </div> Is there some dumb mistake I don't see? Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1464655 Share on other sites More sharing options...
requinix Posted January 10, 2014 Share Posted January 10, 2014 Make sure you have error_reporting = -1 display_errors = onin your php.ini (change and restart if not). Then look for error messages. If you don't see anything then the problem might lie in index.php's code... Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1464657 Share on other sites More sharing options...
Skewled Posted January 10, 2014 Share Posted January 10, 2014 What does index.php contain and were the changes made correctly within that file to reflect the new database? Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1464661 Share on other sites More sharing options...
ryanmetzler3 Posted January 10, 2014 Author Share Posted January 10, 2014 What does index.php contain and were the changes made correctly within that file to reflect the new database? I am confident that the files have been adapted correctly for the new database because when I open the file directly (localhost/ajax_poll_2) everything works great. Are you asking what index.php contains, like the absolute homepage? Or the index.php inside the ajax_poll_2 folder? Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1464671 Share on other sites More sharing options...
ryanmetzler3 Posted January 11, 2014 Author Share Posted January 11, 2014 I have two folders, one is called "ajax_poll_1" and the other is "ajax_poll_2". They are basically a script for a quick poll. I wanted two polls on my site so I copied the first one and made some slight adaptations to it. They both work great. If I type in localhost/ajax_poll_1 or localhost/ajax_poll_2 they both work well. Its when I try to include them on the home page they go awry. I did this: <div id="contentwrap"> <div id="content"> <div class="poll1"> <?php include $_SERVER['DOCUMENT_ROOT'] . '/ajax_poll_1/index.php'; ?> </div> <div class="poll2"> <?php include $_SERVER['DOCUMENT_ROOT'] . '/ajax_poll_2/index.php'; ?> </div> </div> </div> If I only include one at a time it works great. If I include them both one overwrites the other. For example when I run this code ajax_poll_2 actually displays over the top of ajax_poll_1 in the poll1 div. Its driving me crazy. The only errors I have are undefined variables because they are not set until the user submits their vote. I just suppressed them because it works fine and I think it has nothing to do with this problem. Anyone know whats going on? Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1464884 Share on other sites More sharing options...
Solution mac_gyver Posted January 12, 2014 Solution Share Posted January 12, 2014 we cannot specifically tell you why your code doesn't work because we don't know what your code is. you likely have repeated id's in the html, conflicts in some javascript, ... in general, trying to copy/pasted together web pages using include statement only works for very simple pages. there's no telling what sort of interaction you are having without seeing the code. you have got a complete web page that doesn't function. you would need to post code that reproduces the problem to get help with it. Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1464888 Share on other sites More sharing options...
ryanmetzler3 Posted January 12, 2014 Author Share Posted January 12, 2014 we cannot specifically tell you why your code doesn't work because we don't know what your code is. you likely have repeated id's in the html, conflicts in some javascript, ... in general, trying to copy/pasted together web pages using include statement only works for very simple pages. there's no telling what sort of interaction you are having without seeing the code. you have got a complete web page that doesn't function. you would need to post code that reproduces the problem to get help with it. Thanks, since I copied the first file to make the second I have some div's with the same name. I did not think that would matter since they are sitting inside folders with different names but I will change that and see how it works. I appreciate the suggestion! Quote Link to comment https://forums.phpfreaks.com/topic/284985-script-not-working-when-included-in-different-file/#findComment-1464894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.