Jump to content

calling php within a javascript code


wmguk

Recommended Posts

Hi,

 

I have a dynamic script that creates and deletes my pages etc from a CMS system.

 

Basically I need to check my database and show all results for pages where parent = 1

 

Now, I can do this in PHP, however I dont know how to build this in to the JS code for a dropdown menu...

 

var anylinkmenu1={divclass:'anylinkmenu', inlinestyle:'width:150px; background:#0066cc', linktarget:''} //First menu variable. Make sure "anylinkmenu1" is a unique name!
anylinkmenu1.items=[
["title", "slug"],
]

 

The title is called title in the DB and the page name is called slug (this is what you use in the address bar.

 

I dont know how to build this up, and can I have my connection and query script in the .js file?

 

Thanks

Link to comment
Share on other sites

I have tried this:

 

<?php
include_once("http://www.xxxxx.eu.com/wolfcms/config.php");

if (!$myconn)  
{  
die( 'Could not connect: ' . mysql_error() );  
} 

$pagesql= "SELECT * FROM page WHERE parent = 1"; 
$pageres= mysql_query( $pagesql ) or die( ); 
while($pagerow= mysql_fetch_array($pageres)) 
{ 
?>
var anylinkmenu1={divclass:'anylinkmenu', inlinestyle:'width:150px; background:#0066cc', linktarget:''} 
anylinkmenu1.items=[
["<?php echo $pagerow['title']; ?>", "<?php echo $pagerow['slug']; ?>"],
["", ""]
]

 

But I get a JS error on load now :(

 

Any Ideas?

Link to comment
Share on other sites

Hi,

 

I remembered using one before with some help from this site, so I just pulled up the site that is running it...

 

http://www.witsuk.co.uk has a latest news scroller on it, which uses the same code idea as this new site...

 

I've used the same structure as the working code, (below) but still getting an error

 

<?php
include ("http://www.taxbusters.eu.com/wolfcms/config.php");
$pagesql= "SELECT * FROM page WHERE parent = 107"; 
$pageres= mysql_query( $pagesql ); 
?>
var anylinkmenu1={divclass:'anylinkmenu', inlinestyle:'width:150px; background:#0066cc', linktarget:''} 
anylinkmenu1.items=[

<?php
while($pagerow= mysql_fetch_array($pageres)) 
{ 
?>
["<?php echo $pagerow['title']; ?>", "<?php echo $pagerow['slug']; ?>"],
<?php } ?>
["", ""]
]

Link to comment
Share on other sites

You cannot use php code in JavaScript. However, you can make a request to a php file using AJAX, get the response and use the data however you want.

 

Um, Yes you can.

AddType  application/x-httpd-php .js

 

Can you provide a link with some info on that because after I googled it I couldn't find anything useful? Thanks in advance.

Link to comment
Share on other sites

You cannot use php code in JavaScript. However, you can make a request to a php file using AJAX, get the response and use the data however you want.

 

Um, Yes you can.

AddType  application/x-httpd-php .js

 

I just added that using my CPanel MIME controls, however my whole site now gets a 500 error?? Any ideas?

Link to comment
Share on other sites

      <tr>
        <td height="250" align="center" valign="top" class="nav"><script language="javascript">new pausescroller(pausecontent, "scroller", "main", 3000)</script></td>
      </tr>

 

<script src="http://www.witsuk.co.uk/Scripts/scroller.js" type="text/javascript"></script>
<script type="text/javascript">
/*Example message arrays for the two demo scrollers*/
var pausecontent=new Array()
pausecontent[0]='<a href="news.php#">2008-11-03</a><br />Moved to new offices in Market Place Dereham!'
pausecontent[1]='<a href="news.php#">2008-12-01</a><br />New Large Client Aquired!'
pausecontent[2]='<a href="news.php#">2009-01-02</a><br />New Sales Force Recruited!'
pausecontent[3]='<a href="news.php#">2009-02-02</a><br />New Website Launched!'
pausecontent[4]='<a href="news.php#">2009-02-09</a><br />New email marketing software launched!'
pausecontent[5]='<a href="news.php#">2009-02-14</a><br />New photography software completed!'
pausecontent[6]='<a href="news.php#">2009-02-25</a><br />Launch of New Online Shop for Local Client!'
pausecontent[7]='<a href="news.php#">2009-03-04</a><br />Completed Bliss Tiling Website!'
pausecontent[8]='<a href="news.php#">2009-03-12</a><br />Completed Pitch Clean Website!'
pausecontent[9]='<a href="news.php#">2009-03-20</a><br />Completed iwphoto.co.uk Website!'
pausecontent[10]='<a href="news.php#">2009-03-27</a><br />New CMS system designed and implemented!'
pausecontent[11]='<a href="news.php#">2009-04-01</a><br />Completed Pride of Bourne Awards Website!'
pausecontent[12]='<a href="news.php#">2009-04-16</a><br />Completed Landscape Machinery Hire Website!'
pausecontent[13]='<a href="news.php#">2009-04-21</a><br />Completed Ouch Online Ordering Website!'
pausecontent[14]='<a href="news.php#">2009-04-29</a><br />Sales Force Expansion!'
pausecontent[15]='<a href="news.php#">2009-05-01</a><br />Regional Sales Force in place for Norfolk & Suffolk!'
pausecontent[16]='<a href="news.php#">2009-05-05</a><br />Large UK National Client Aquired!'
pausecontent[17]='<a href="news.php#">2009-05-12</a><br />Completed TR Bookkeeping website!'
pausecontent[18]='<a href="news.php#">2009-05-18</a><br />Completed Road Runner Couriers Website!'
pausecontent[19]='<a href="news.php#">2009-05-19</a><br />Completed Unique Building Website!'
pausecontent[20]='<a href="news.php#">2009-05-26</a><br />Completed Bruce Howell Waterfowl Website!'
pausecontent[21]='<a href="news.php#">2009-06-03</a><br />Completed Simple Solutions Website!'
pausecontent[22]='<a href="news.php#">2009-06-09</a><br />Completed Stuston Farm Shop Website!'
pausecontent[23]='<a href="news.php#">2009-06-18</a><br />Completed Anglia Skips Website!'
pausecontent[24]='<a href="news.php#">2009-06-24</a><br />Completed The Falcon Public House Website!'
pausecontent[25]='<a href="news.php#">2009-07-01</a><br />Completed Baggleys Machinery Website!'
pausecontent[26]='<a href="news.php#">2009-07-06</a><br />Completed Housemate Cleaning Website!'
pausecontent[27]='<a href="news.php#">2009-07-08</a><br />Completed MAP Recovery Website!'
pausecontent[28]='<a href="news.php#">2009-07-09</a><br />Completed Replace Your Plates Website!'
pausecontent[29]='<a href="news.php#">2009-07-10</a><br />Completed Eastern Beansprouts Website!'
pausecontent[30]='<a href="news.php#">2009-07-13</a><br />Completed Norfolk Energy Consultancy Website!'
pausecontent[31]='<a href="news.php#">2009-07-17</a><br />Completed Kaandaro Event Videographer Website!'
pausecontent[32]='<a href="news.php#">2009-07-23</a><br />Completed New Local Recruitment Website!'
pausecontent[33]='<a href="news.php#">2009-07-31</a><br />Completed New Balloon Designer Website!'
pausecontent[34]='<a href="news.php#">2009-08-01</a><br />Expansion to Newer Larger Offices!'
pausecontent[35]='<a href="news.php#">2009-08-04</a><br />New Call Centre Opens Recruiting Now!'
</script>

 

Is that what you needed?

Link to comment
Share on other sites

Is that what you needed?

 

Hi no sorry, I already have that code in raw php / js form, I was trying to get a different website to access php in js pages, it seems my server is upset now though lol, I'm getting a 500 error

Link to comment
Share on other sites

You cannot use php code in JavaScript. However, you can make a request to a php file using AJAX, get the response and use the data however you want.

 

Um, Yes you can.

AddType  application/x-httpd-php .js

 

I just added that using my CPanel MIME controls, however my whole site now gets a 500 error?? Any ideas?

 

Don't use cPanel. Just use your FTP and remove everything from .htaccess, cPanel is stupid and doesn't put an option on a new line, thus the error.

 

If you look at the file it most likely lists the new mimetype on the same line as another entry.

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.