Lassie Posted February 28, 2008 Share Posted February 28, 2008 I am trying to use a script to dynamically fetch pages of html into a web page from a database, ajax style. The script requires the php to 'echo' the javascript. at this point in the script it literally outputs to the browser. Is what I am attempting possible and/or have I got the snytax wrong. I have replaced the js script references with xxxx as I believe the post won’t be accepted otherwise The relevant part of the code is <body> <a href="xxxxx :mypages.refresh(myajaxbook)"><img src="BookJacket95x141.gif"></img></a> <div id="paginate-top"> </div> <div id="pcontent"> </div> <div id="paginate-bottom"> </div> <?php //include db_fns include_once('db_fns.php'); include('mysql_connect.php'); //get product_id $product_id=13; //connect to db $connection=db_connect(); $query="SELECT p1,p2,p3 from ajax WHERE product_id='$product_id'"; $result = mysql_query($query) or die ("Sorry, unable to execute $sql: " . mysql_error()); // execute the query $result = mysql_query($query); $num_rows=mysql_num_rows($result); $idarray=array(); for( $i=0; $i<$num_rows; $i++) { $row=mysql_fetch_row($result); array_push($idarray, array($row[0],$row[1],$row[2])); } //code out puts after this line echo "<xxxxx ='xxxxx’>"; //Dynamically output JavaScript opening tag echo "var mysettings={}\n"; //Dynamically output javascript variable echo "mysettings.page=[" . implode(",", $idarray) . "]\n"; echo "mysettings.selectedpage=0\n"; echo "xxxx\n"; //Dynamically output JavaScript closing tag ?> <xxxe=xxxxt"> var mypages=new ajaxpageclass.bindpages(myajaxbook, "pcontent", ["paginate-top", "paginate-bottom"]) </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
haku Posted February 28, 2008 Share Posted February 28, 2008 Yes you can output javascript to the browser with php. You can even set javascript/ajax variables with php so that the outputted script is dynamic. Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 28, 2008 Author Share Posted February 28, 2008 Thanks. I have tried to incorporate php variables into an ahref tag but get parse errors. This is an example that gives parse errors. Can you help please. ?> <a href="xxxx:ajaxpage('<?php ./$p1?>', 'contentarea');"onmouseover="Tip('View Synopsis')"><img src=<?php './images/{$book['pix']}'?> border='0' width='80' height='100'/></a>" Quote Link to comment Share on other sites More sharing options...
haku Posted February 28, 2008 Share Posted February 28, 2008 That PHP doesnt exist. You have an opening tag: <?php and a closing tag: ./$p1?> That closing tag doesnt exist, and even if you were to separate it into this: ./$p1 ?> Its still not any php that I have seen. You can include php variables in javascript, but it has to be valid php to work. Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 28, 2008 Author Share Posted February 28, 2008 Ok Taking your comments into account I have modified the tag thus. The ./$p1 is a var holding location of page to be pulled into the content area div. ?> <a href=":ajaxpage('<?php './$p1'?>',' 'contentarea');"onmouseover="Tip('View Synopsis')"><img src=<?php './images/{$book['pix']}'?> border='0' width='80' height='100'/></a>" Quote Link to comment Share on other sites More sharing options...
haku Posted February 28, 2008 Share Posted February 28, 2008 You didn't change the code I pointed out at all. It should look like this: <?php echo "/" . $p1; ?> And your second tag can look like this: <img src="<?php echo "/images/" . $book['pix']; ?>" border='0' width='80' height='100' /> Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 29, 2008 Author Share Posted February 29, 2008 Ok. Thank you for your help Quote Link to comment 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.