Jump to content

[SOLVED] Retreving mysql info with AJAX links?


Jason28

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.