Jason28 Posted June 2, 2008 Share Posted June 2, 2008 I have seen a few tutorials about Ajax working with php and mysql but they always use form values. Could anyone provide an example of using a url with $_GET statements and have AJAX display the results from the DB? Such as: blah.php?action=view&user=5 and AJAX would pass the action and user info to php and display the result? A working example would be great. Thanks. Quote Link to comment Share on other sites More sharing options...
Jason28 Posted June 2, 2008 Author Share Posted June 2, 2008 Anyone please? Just a link that when clicked displays AJAX generated info from the DB would be great. I used a few tutorials but they only gave errors from the javascript. Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted June 2, 2008 Share Posted June 2, 2008 if you could wait a minute i will write an example... Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted June 2, 2008 Share Posted June 2, 2008 here's ajax script that calls your php-file (in this example: example.php) which does your queries (or whatever). it sends data via POST to your php file (if you want): var request = false; function postRequest(url, vars) { request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... request = new XMLHttpRequest(); if (request.overrideMimeType) { set type accordingly to anticipated content type request.overrideMimeType('text/xml'); request.overrideMimeType('text/html'); } } else if (window.ActiveXObject) { // IE try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!request) { //alert('Cannot create XMLHTTP instance'); return false; } request.onreadystatechange = alertContents; request.open('POST', url, true); request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.setRequestHeader("Content-length", vars.length); request.setRequestHeader("Connection", "close"); request.send(vars); } function alertContents() { if (request.readyState == 4) { if (request.status == 200) { result = request.responseText; document.getElementById('area').innerHTML = result; } else { alert('There was a problem with the request.'); } } } you call the function post(filepath, post_vars) with: postRequest("example.php", "var1=whatever..."); the function postRequest() sends your variables to example.php where you do your queries. example.php returns an output to your ajax-function. The Ajax-function inserts the returned code in a specified textarea (with the line: document.getElementById('area').innerHTML = result; ). Thats it. EDIT: I know this is $_POST but you can enter your $_GET variables in the $_POST string and use it with $_POST. If you need an AJAX-function with $_GET badly i can change it for you! Or you SIMPLY deliver your url with your get-parameters (like: example.php?var1=test) and leave the POST-string empty. Quote Link to comment Share on other sites More sharing options...
Jason28 Posted June 2, 2008 Author Share Posted June 2, 2008 I need to use GET so when I click on a link it simply displays the rows from my DB. Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted June 2, 2008 Share Posted June 2, 2008 did you read my edit? insert this in your link: javascript:postRequest("example.php?var1=whatever...", ""); and your example.php will be requested with "GET-variables" (var1 in this case). (if you don't want to write the empty post-string all the time, change the function-declarations) Quote Link to comment Share on other sites More sharing options...
Jason28 Posted June 2, 2008 Author Share Posted June 2, 2008 No sorry I didn't see your edit until you mentioned it now Sure I will try it but if I cannot get it to work can I simply pay you for this? I have this script written from scratch on a test site it is very clean and easy to read. Thanks. EDIT In fact I am so busy I don't even feel like attempting to figure this out can you do this for me? Thanks. Quote Link to comment Share on other sites More sharing options...
Jason28 Posted June 2, 2008 Author Share Posted June 2, 2008 Please let me know whenever you come back online Quote Link to comment Share on other sites More sharing options...
Jason28 Posted June 2, 2008 Author Share Posted June 2, 2008 BTW the ajax code is displaying errors: http://img231.imageshack.us/img231/6195/ajaxerrorqj7.jpg missing ; before statement set type accordingly to anticipated content type\n I hope that you come back soon. Thanks. Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted June 2, 2008 Share Posted June 2, 2008 Yes I can do it for you, but no need to pay for it ... i contact you via PM. Quote Link to comment Share on other sites More sharing options...
haku Posted June 3, 2008 Share Posted June 3, 2008 I agree. The forums are generally for people who need help with code, not for people who need code written for them. I am happy to help people who need help, but if people want me to write their code I expect a financial incentive. Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted June 3, 2008 Share Posted June 3, 2008 i will not make somebody pay for such a little code. If somebody needs something bigger we can talk, but I'm not greedy. Quote Link to comment Share on other sites More sharing options...
haku Posted June 3, 2008 Share Posted June 3, 2008 That's fair enough. And even my rule isn't 100% inflexible - I have written short scripts for people here on a few occasions. But, I come here more from the stance of a teacher - many people helped me to learn what I know, so I am willing to help others figure out what they want to know. But just writing a script for someone isn't the best way to teach them, I would rather help someone get over a hurdle that they are having troubles getting over, than jumping over the hurdle for them. Quote Link to comment Share on other sites More sharing options...
kbh43dz_u Posted June 3, 2008 Share Posted June 3, 2008 i didn't just write it for him i explained it and told him what i did and why and also left some code over for him Quote Link to comment Share on other sites More sharing options...
Jason28 Posted June 3, 2008 Author Share Posted June 3, 2008 Yes you were very helpful but too bad I couldn't figure it out to make it work Quote Link to comment 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.