Results 1 to 6 of 6

Thread: Pass variable to php (ajax)

  1. #1
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,530
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default Pass variable to php (ajax)

    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 -

    Code:
    <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 -

    Code:
    <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!!!!!!!!!
    Keyboard1333
    Last edited by keyboard; 02-25-2012 at 10:45 PM. Reason: Edited code of second exaple + spelling

  2. #2
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,530
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    (BUMPED)

    Does any one have an answer?

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,134
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    http://www.dynamicdrive.com/forums/s...3&postcount=23

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

    Code:
    new loadXmlHttp('somepage.php?something=somevalue', 'target_element_id')
    Then on somepage.php:

    PHP Code:
    $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:

    Code:
    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_...using_post.php
    Last edited by jscheuer1; 02-25-2012 at 10:05 PM. Reason: add detail
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,530
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    You complete legend!!!

  5. #5
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default same problem - just the basics please?

    Quote Originally Posted by keyboard1333 View Post
    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.../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!

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.)
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •