TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Radio buttons - fetching query? (http://www.talkphp.com/javascript-ajax-e4x/2543-radio-buttons-fetching-query.html)

marxx 03-28-2008 10:46 PM

Radio buttons - fetching query?
 
Hi all.. Here's my scenario.

four radio buttons
Each have own data in database
When user make hes choose, data would change automaticlly in that page..

eh.. I hope you understand what I'm after? Hard to explain while english isn't my home language! ;)

Don't know much javascript/frameworks so thats why I here (again and again.. ;))

I'm already using prototype in my page so, if there is sollution that would be great! But doesn't have to.. I don't know what to look so even if someone knows something similar, could you share link or something so I could go and look.

Thanks for all help!

serversphere 03-29-2008 04:49 AM

Try this link - many, many great ajax starter tutorials that should push you in the right direction:

Tutorials Round-Up: Ajax, CSS, PHP and More | Tutorials | Smashing Magazine

marxx 03-29-2008 11:48 AM

Thanks serversphere, I'm familiar that page quite a bit! ;)

Anyway, I manage to get some help from another page and here's result. (if someone needs?!)

Ajax.js:
Code:

var rdiv;
var xmlHttp;
var cb;
function request (url, div, parameters, callback)
{

        if(xmlHttp)
        {
                //alert("aboting current xmlhttp");
                //xmlHttpd.abort();
        }
        rdiv = div;
        var queryString = url + "?" + parameters;
        cb = callback;

        createXMLHttpRequest();
        xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.open("GET", queryString, true);
        xmlHttp.send(null);
}

function createXMLHttpRequest()
{
        if(window.ActiveXObject)
        {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest)
        {
                xmlHttp = new XMLHttpRequest();
        }
}

function handleStateChange()
{
        if(xmlHttp.readyState == 4)
        {
                if(xmlHttp.status == 200)
                {
                        parseResults();
                }
        }
}
function parseResults()
{

        document.getElementById(rdiv).innerHTML = xmlHttp.responseText;
        if(cb) cb();

}

Code:

function fetchQueryPage(pid)
{
    request("/doquery.php", "doquery_box", "id=" + pid)
}

Place div whit class name "doquery_box" page where you are using this. And call fetchQueryPage function onClick event in your radio buttons.

Code:

<input type="radio" name="myradio" onClick="fetchQueryPage(11);" />
and finally, in doquery.php you have your code ie.
PHP Code:


$pid 
$_GET['id'];

$q mysql_query("SELECT result FROM table WHERE id = '$pid'") or die(mysql_error());
$do mysql_fetch_array($q);

print(
"Here is your result:" $do['result']); 

Ok.. I'm happy now when finally got it BUT, now I have different problem.
How can I define that if one of those radio buttons are already checked when user arrive that page, then run that fetchQueryPage function?

Because I have option on different page when user can choose one of those radios.

Code:

if (radio.[i].checked == true)
{
  run that function whit that pid
}
else
{
  wait while user clicks one of those radios
}


Thanks for all help! =)

serversphere 03-29-2008 01:20 PM

Very nice! Just be sure to sanitize the ID passed to doquery.php or else it could be subject to sql injection.

marxx 03-29-2008 01:26 PM

Oh yes offcourse that was just example and type as was posted.. =)


All times are GMT. The time now is 07:07 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0