i have a concept but nowhere to start really as far as ideas. I have an php object. lets call it a gun object. this object has attributes and a description, including a description of it firing. how would i use ajax to output to a page using the php i've already coded? for instance to write to a page the description of a 9mm handgun and how many bullets it holds. thanks in advance!
Your description is somewhat confusing, since the way you described it you wouldn't even require AJAX since you would be outputing the data from PHP. Can you be much more clear?
create a php file and include your php class in it. when you call the php file, you can send parmeters to it via the querystring. Lets say the php page is test.php. you could do something like test.php?a=1&b=2. you just passed 2 variables to the test page. In the php page, use the variables passed to get the response you need out of your php class. Then simply print out the php class response using print or echo commands. Then call the php page and pass variables to it using an ajax function. You'll then be able to do whatever you want with the result that is returned.
okay i follow what your saying. can you give a small demonstration or explanation of how to use the parameters? and also do parameters have to be numbers or can they be anything such as words? or could you do something like if(a==1) { echo stuff here. } Code (markup): also to elaborate for the other reply. i have a javascript code that will update the webpage without refreshing(called addNode) it by adding a text node to the page. since the update to the page is written in javascript i'm looking for a way to transfer the information from the php object to be outputted when the addNode() function is called. what i'm trying to avoid is writing multiple versions of the script. for instance : i have a button called "describe" that when clicked would simply output the description. but it would refresh the whole page upon doing so. by using addNode i would like to update the page using the describe button. Does that give you a better more clear idea of what I'm trying to do?
in your php page, you first need to retrieve the variables sent: $a = $_GET["a"]; $b = $_GET["b"]; then do whatever you want: if($a==1) echo "1"; else echo "2"; variables sent via querystring can be either numbers or text. you can check: if(is_numeric($_GET["a"])) $a = $_GET["a"]; else $a = false;
okay. i got everything you said. i tried it but it didn't work for me. maybe it's because I want this to work on click. either way I'm going to post my php code. that seems to be the problem since everything else works. <script type="text/javascript"> function addNode(inText) { newGraf=document.createElement("p"); newText=document.createTextNode(inText); newGraf.appendChild(newText); docBody=document.getElementsByTagName("body").item(0)//change body to td for table docBody.appendChild(newGraf);//and item to 6 return false; } Code (markup): function Xp() { var xmlhttp;//create a var for the obj if (window.XMLHttpRequest)//if requesting an obj... { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } else { alert("Your browser does not support XMLHTTP!"); } xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) { return addNode('You suck'); } } xmlhttp.open("GET","testatt.php?a=1",true); xmlhttp.send(null); } </script> Code (markup): <html> <head> </head> <body> <button onclick="return Xp()">Comment</button></br> </body> </html> HTML: <?php $a=$_GET["a"]; if($a==1) { echo "Hi"; } else { echo "STFU UP!!"; } ?> Code (markup):
try this: function Xp() { var xmlhttp;//create a var for the obj if (window.XMLHttpRequest)//if requesting an obj... { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } else { alert("Your browser does not support XMLHTTP!"); } xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) { //return addNode('You suck'); var text = xmlhttp.responseText; alert(text); } } xmlhttp.open("GET","testatt.php?a=1",true); xmlhttp.send(null); } Code (markup):
thanks it works like a charm. i was even able to modify it so that i could control WHERE the text shows up on the page. this has been a BIG help. using this simle script i can take a big step towards fixing my website. i thank you for your assistance