Jump to content

Calling Php From Php Generated Html


maximeGir

Recommended Posts

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.


  •  

Link to comment
Share on other sites

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'
});

Link to comment
Share on other sites

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 by maximeGir
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.