Log in

View Full Version : Resolved Pass variable to php (ajax)



keyboard
02-23-2012, 05:33 AM
Hello everyone,
Could someone please post some cross-browser compatiable ajax code that will allow me to send a javascript variable to a php page (WITHOUT using jQuery). I want it to look like this -



<script type="text/javascript">
function ajaxfunction(element) {
//ajaxystuff to pass element
}
</script>
<tagname onclick="ajaxfunction('pass me');



Could you also please post a second code (an adaption of the first) that can recieve a respone from the page, like this -



<script type="text/javascript">
function ajaxfunction(element) {
//ajaxystuff to pass and recieve variable element and the response.

var ajaxrespone = "ajaxystuff";
}
</script>
<tagname onclick="ajaxfunction('pass me');


I also need to know to assign the information sent to the php page, as a php variable.

Any help would be Terrific!!!!!!!!!:):D:):D
Keyboard1333

keyboard
02-25-2012, 08:45 PM
(BUMPED)

Does any one have an answer?

jscheuer1
02-25-2012, 09:35 PM
http://www.dynamicdrive.com/forums/showpost.php?p=190183&postcount=23

Oh, and it's a GET request, so you can send a query string, like:


new loadXmlHttp('somepage.php?something=somevalue', 'target_element_id')

Then on somepage.php:


$something = isset($_GET['something'])? $_GET['something'] : '';

The results will populate an element with the id specified as the target_element_id.

If you have a lot of data to send to the PHP page, it can be adapted to do a POST (you have to add a few headers and send the data*). The return function:



loadXmlHttp.prototype.stateChanged = function(){
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re))
this.el.innerHTML = this.xmlHttp.responseText;
}

can be modified or set individually for each request.

In my opinion though, it's only worth bothering with and learning about as background - to help one in understanding the AJAX process. jQuery is much easier to work with and can help with so many other things on a page/site - why reinvent the wheel?



* see:

http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

keyboard
02-25-2012, 10:43 PM
You complete legend!!!:)

kenax
05-08-2013, 03:54 AM
You complete legend!!!:)

hi, i've spent days trying to figure this out and have a similar problem. I've learned html, css, php and others, but ajax baffles me somewhat. Starting to get a hang of it, but all the tutorials I've come across are typically convoluted and not so clear. I'm working on
http://001yourtranslationservice.com/kenax/Translators/Resources/TimeZones.htm
and trying to change the "Your time" at the top (of the alarm clock) based on the user, which requires javascript. I assume the procedure is for js to first determine their local time (probably easiest just from their computer time settings, although I've found some pretty fancy scripts based on internet connection), then to save this little bit of information to an xml file via ajax, then for the js to launch the php script, which will draw the data from the xml file and do its wonders (right now it's php only and the time at the top is based on the server's location, somewhere in the US). The js would be set to refresh the page once a minute. For now the php script is doing that by html page refresh within the iframe. There must be a simpler way to do this then to use jQuery. i'd like to know the basics and not create a monster to do this simple task. Thank you very kindly for your help!

djr33
05-08-2013, 04:01 AM
Kenax, please start a new thread unless you specifically want to use the code discussed here. There are many ways to do that, and generally speaking a "normal" ajax method should work fine, plus the detail of how to work with times (which would be an independent issue in JS and/or PHP).

Additionally, there are several AJAX scripts available here that probably could do what you need.


I'm going to close the current thread so that you can start a new one. (If you really think it should be a continuation of the current discussion, feel free to link here, or even in your post ask for them to be merged together, but I don't think that's the case.)