maximeGir Posted October 7, 2012 Share Posted October 7, 2012 Hi guys and gals, I am setting up an interactive web site for practice (and fun, of course...) that uses some php. Structurally it looks like this : index.php requires another .php file that contain a function that displays the basics elements of the main web page (index) in a DIV. So basically when index.php is loaded, a <div> containing <?php echo functionToDisplayMainContent(); ?> is filled with the returned html markup. which is good. within that returned html there are some href links that make the content fade out and fade in new content (jQuery). So the returned html is to be changed at some point depending on user input (clicking).The new html that appears when user click is generated by another php page that an href links to . My main question is : How do I get php generated html to return in an existing(php generated) html tag,since it returns me a whole new page with the html that is echoed from the php page.I call the php url from an http request written in javascript (jQuery) with .click event handler to the proper #id. I know it's kind of a mess but I could not explain it better. Any help would be appreciated, Cheers. your bad ass canadian friend. Quote MultiQuote Edit Quote Link to comment https://forums.phpfreaks.com/topic/269201-calling-php-from-php-generated-html/ Share on other sites More sharing options...
berridgeab Posted October 7, 2012 Share Posted October 7, 2012 You need something called AJAX, similar to what this other guy has just asked. http://forums.phpfre...e/#entry1383550 Plenty of stuff on google too. Below is a basic example of what you have to do with jQuery. $.post({ url: url, data: data, success: function(data, textStatus, jqXHR) { //Do something with PHP response here, assign it to DIVs in page, whatever }, dataType: 'html' }); Quote Link to comment https://forums.phpfreaks.com/topic/269201-calling-php-from-php-generated-html/#findComment-1383556 Share on other sites More sharing options...
maximeGir Posted October 7, 2012 Author Share Posted October 7, 2012 (edited) I did that : let me post the code I have ! on the id "word" : on click I fade out the list elements and initiate an ajax http request with a php url that echo more html (just another ul il list elements) here is my javascript. $('#word').click(function(){ var pageToLoad = 'prog'; $('li').fadeOut('slow'); var requestResponse = envoyerRequete(pageToLoad); $('#redirect').replaceWith(requestResponse); }); function envoyerRequete(pageToLoad){ if (pageToLoad == 'prog'){ var url = 'getSelectedPage.php?page='+pageToLoad; url.toString(); var response = ''; $.ajax({ type: 'GET', url: url, async: false, success : function(text){ response = text; } }); } return response; } And here is my php <?php $pageToLoad = null; if(isset($_GET['page'])){ $pageToLoad = $_GET['page']; } if($pageToLoad == 'prog'){ echo "<ul id='newStyle'>"; echo "<br/>"; echo "<li class='linkFromProg' id='contactMe'><a href='mailto:g@gmail.com' >"; echo "contact me</a>"; echo "</li>"; echo "<br/>"; echo "<li class='linkFromProg' id='comment'><a href='http://blog.org'>Leave a comment</a>"; echo "</li>"; echo "<br/>"; echo "<li class='linkFromProg' id='subscribe'><a href='http://blog.org'>Register</a>"; echo "</li>"; echo "</br>"; echo "</ul>"; } ?> And it returns a new html page with the content echoed in the php I just mentionned. It does not replace a div id in the main php generated html i am calling this from. I'm not at validation yet im testing that particular case first. What do you think ? Please let me know man. I really want to understand that concept. Edited October 7, 2012 by maximeGir Quote Link to comment https://forums.phpfreaks.com/topic/269201-calling-php-from-php-generated-html/#findComment-1383567 Share on other sites More sharing options...
berridgeab Posted October 8, 2012 Share Posted October 8, 2012 Have you dumped the contents of variable text in the anonymous function to make sure the data is there? Quote Link to comment https://forums.phpfreaks.com/topic/269201-calling-php-from-php-generated-html/#findComment-1383831 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.