on the click of a button change a MYSQL query locally - possible?

Discussion in 'JavaScript' started by twistedspikes, Mar 11, 2009.

  1. #1
    I'll try explain this the best as I can.

    What I want is a link that will change the data displayed in another part of the webpage (and this data is extracted from a MYSQL database using PHP) by sending a variable (a number between 1 and 10) - but I don't want the page to reload (so I want to do this locally).

    Is it even possible? I'd assume it is - either using JS or AJAX. If it is then can someone please put me out of my missery and tell me how to do it? :)

    Thanks,
    TS
     
    twistedspikes, Mar 11, 2009 IP
  2. bhagwant.banger

    bhagwant.banger Active Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    60
    #2
    Hi ,
    This can be easily done using AJAX.

    All you have to do is assign an ID to the element which contains this data and then you can of course change its inner content dynamically by passing the values to a javascript function.

    AJAX becomes very easy if you use a library like prototype. If you use prototype.js then the following function will make your life easier

    function changeData(id){
    	
    	var id=id;
    	
    	$('youdivname').innerHTML='';
    	$('youdivname').innerHTML="<div style='text-align:center'><img src='images/wheel.gif'><h1>Loading Data...</h1></div>";
    	
    	
    	var url = 'ajax.php'; //this is the page which returns data it should accept the id in get array and change mysqlquery mapped to the id
    	var params = 'id='+id;
    	var ajax = new Ajax.Updater(
    					{success: 'yourdivname'},
    					url,
    					{method: 'get', parameters: params, onFailure: 'youhaveanerro'}
    				);
    }
    Code (markup):

    that's all
    revert back if you have any doubts...
     
    bhagwant.banger, Mar 11, 2009 IP
    twistedspikes likes this.
  3. twistedspikes

    twistedspikes Notable Member

    Messages:
    5,694
    Likes Received:
    293
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Thanks,

    I'll try this out in a minute, see if I can get it to work.
     
    twistedspikes, Mar 12, 2009 IP