Jump to content

script not working when included in different file-


ryanmetzler3
Go to solution Solved by mac_gyver,

Recommended Posts

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?

Link to comment
Share on other sites

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 by ryanmetzler3
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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";
Link to comment
Share on other sites

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! 

post-164885-0-44180100-1388444300_thumb.png

Edited by ryanmetzler3
Link to comment
Share on other sites

  • 2 weeks later...

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.