Ajax and php

Discussion in 'PHP' started by paul_so40, Jan 18, 2011.

  1. #1
    Hello Fellow developers!

    I am trying to do something that i am not in the knowing of if its even possible.

    I want to have ajax text links in my site, that will load php script in the site.

    Im trying to achieve this through an example found here

    It grabs the page content from a file called loader.php

    page 3 is my attempt of getting php code to load, however it does not work.

    <?
    
    switch($_GET['page']) 
    
     {
    
    	case '#page1' : $page = '<b>Introduction</b><ul><li>History support, solved ajax Back Button limitation with jquery.history.js</li><li>Bookmark support. Using hash value to identify a specific page and load it</li><li>It\'s AJAX/AHAH! It\'s cool and cutting edge!</li><li>This tutorial can be used as a kickstart for your AJAX based web development project. Small ajax based content like autocomplete and check username are basically using the same concept.</li></ul><br/>'; break;
    
    
    	case '#page2' : $page = '<b>Portfolio</b><br/>You can put whatever you want. For advance programmers, you can create your own simple backend that store web pages, and then display it by using PHP to retrieve it from database. Also, you can integrate a Rich Text Editor for your backend too! I have made a post regarding <a href=#">Rich Text Editor</a>.<br/><br/> Starting from a simple guideline, you can build your own Content Management System.'; break;
    
    
    	case '#page3' : $page = echo date("m/d/y");
    
    
    	case '#page4' : $page = '<b>Contact</b><br/>This form is just a demostration of what content you can put.<br/><br/><form>Name:<br/><input type="text"/><br/>Email:<br/><input type="text"/><br/>Message:<br/><textarea></textarea><br/><input type="button" value="Send"></form>'; break;
    
    
    }
    
    echo $page;
    
    ?>
    PHP:
    Can anyone show me how to make it work or show me a working way of doing achieving what i need?

    Thanks

    Paul
     
    paul_so40, Jan 18, 2011 IP
  2. paul_so40

    paul_so40 Peon

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is there a way i can use ajax/jqueary to update a div with a php include?
     
    paul_so40, Jan 18, 2011 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    You'll need to first submit an HTTP request using Ajax to a PHP script, which runs its calculations and returns a result. Using Javascript, you can then decide what to do with the returned data. So the following is a simple example using your code, which will use Ajax to update the div with data returned from the PHP file:

    ajax.html
    <!doctype html>
    <html>
    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    	$("#change").click(function() {
    		var Page = $("#page").val();
    		$.ajax({
    			url: 'ajax.php?page=' + Page,
    			success: function( data ) {
    				$("#contents").html( $(data).text() );
    			}
    		});
    		return false;
    	});
    });
    </script>
    </head>
    <body>
    <div id="contents">
    	Original contents
    </div>
    <input type="text" id="page" value="page1" /><a href="#" id="change">Change me</a>
    </body>
    </html>
    HTML:
    ajax.php
    <?php
    
    switch($_GET['page'])
    {
        case 'page1' : $page = '<b>Introduction</b><ul><li>History support, solved ajax Back Button limitation with jquery.history.js</li><li>Bookmark support. Using hash value to identify a specific page and load it</li><li>It\'s AJAX/AHAH! It\'s cool and cutting edge!</li><li>This tutorial can be used as a kickstart for your AJAX based web development project. Small ajax based content like autocomplete and check username are basically using the same concept.</li></ul><br/>'; break;
        case 'page2' : $page = '<b>Portfolio</b><br/>You can put whatever you want. For advance programmers, you can create your own simple backend that store web pages, and then display it by using PHP to retrieve it from database. Also, you can integrate a Rich Text Editor for your backend too! I have made a post regarding <a href=#">Rich Text Editor</a>.<br/><br/> Starting from a simple guideline, you can build your own Content Management System.'; break;
        case 'page3' : $page = date("m/d/y");
        case 'page4' : $page = '<b>Contact</b><br/>This form is just a demostration of what content you can put.<br/><br/><form>Name:<br/><input type="text"/><br/>Email:<br/><input type="text"/><br/>Message:<br/><textarea></textarea><br/><input type="button" value="Send"></form>'; break;
    }
    
    echo $page;
    
    ?>
    PHP:
     
    Alex Roxon, Jan 19, 2011 IP
  4. paul_so40

    paul_so40 Peon

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi, Thanks for the reply.

    The main thing i am trying to achieve is to echo php script into the div.

    If you have a look at case3 you will see that i have put php script, as a test echoing the date. but instead it just shows case 4

    Does anyone know how to make this work?

    Thanks
     
    paul_so40, Jan 19, 2011 IP
  5. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #5
    That's because there's an error with the PHP code. You're missing the break;

    case 'page3' : $page = date("m/d/y"); break;
    PHP:
    Should be that.
     
    Alex Roxon, Jan 19, 2011 IP
    paul_so40 likes this.
  6. paul_so40

    paul_so40 Peon

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok thanks :)

    + Rep
     
    paul_so40, Jan 19, 2011 IP
  7. solidsoul2010

    solidsoul2010 Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    hmm, that was a clever way of doing that alex.. nice job!
     
    solidsoul2010, Jan 19, 2011 IP