Jump to content
MakeWebGames

Recommended Posts

Posted

is it possible to use ajax on a link? i wanted to "attempt" to try get ajax incorporated into inventory.php i tried making only the itemuse.php link ajax but it doesent work, i tried doing this

<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
   {
   document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
�  }
 }
xmlhttp.open("GET","itemuse.php?ID={$i['inv_id']}",true);
xmlhttp.send();
}
</script>
Posted
ohhh see i knew i was suppose to use that, a friend told me that i had to use ajax.

Jquery is a JavaScript library, and Ajax is a way of using JavaScript. If you wish to use Jquery search the site, Danny posted a free mod using Jquery somewhere.

Posted

You use AJAX to make the call to your external PHP script that you would use to update your inventory.

For what you want to do I would suggest,

- Use jQuery to bind to the link that you want the users to click - http://api.jquery.com/bind/

- when they click, you will have a handler function that is an AJAX call to your external PHP script that does the database updating and returns data - http://api.jquery.com/jQuery.ajax/ (view the example just above "Data Types")

- with the returned data, you will then use jQuery again to output a success message to your user ex. ("Healing Potion Used")

Posted (edited)

if the only thing you want is what is called "ajax" nowadays i am assuming you just want to call a page to a div, or maybe you want to post. Jquery is not needed for such an issue, as if you just want ajax that is a simple few lines code. However, if you are looking for effects like shrink, or blink (although i can show you how to to do these without) it is recommended by most to use a framework considering that the dial-up age is dead.

Here is a script that will call your ajax into an "id" of your choice

 

function replace(id,file){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest()
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(id).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET",file,true);
xmlhttp.send();
}

 

You will place this in the head section of your document you intend to use it on, or in the .js file you include in your header.

This is called in your page simply like this

 

<a onclick="replace("idTolLoadTo","fileToLoad.php")">Click Me</a>

 

What this will do is it will load whatever the content from "fileToLoad.php" into the element that has an id of "idTolLoadTo"

Now your probably thinking, this is too hard, but.. whether you are using a function or framework, there is no easy answer. Google these frameworks (when you have a question, ask me on here or google that question) "prototype", "jquery","scriptaculous(based on prototype)" and find one that is most easiest for you.

Edited by runthis

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.   Paste as plain text instead

  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...