www.webdeveloper.com
Results 1 to 6 of 6

Thread: AJAX Question - How do I turn a JavaScript variable into a PHP variable?

  1. #1
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372

    AJAX Question - How do I turn a JavaScript variable into a PHP variable?

    I am new (VERY new) to AJAX, and I know this is the best solution to this problem. I have a string variable in JavaScript. Since I need to add it to a MySQL database, I need to set that value to a PHP variable. How do I do this? What is the best way to go about this?

    I know the GET and POST solutions, but I have ample data and cannot do this this time.

    THANKS!!

    - Multimediocrity

  2. #2
    Join Date
    Nov 2008
    Posts
    2,477
    AJAX is not magic, it is just a method for sending a request without reloading the page. If you want to pass data as part of this request, you still have to use GET or POST just as if you were sending it manually. How much is "ample" data? There are Apache and PHP setting which can limit the amount of data sent by POST, but it is usually in the region of multiple megabytes. Even with a GET request you are only limited by the browser and the server capabilities (IE can only use around 4000 chars and Apache is by default configured for around the same I believe), however the HTTP protocol does not define a limit.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Yep, AJAX is not Magic, its Voodoo...

    Plenty of example are on offer in the threads if you use the search tool.

    If you have a javascript variable with data in it, this can be sent as data via a POST request and the remote script on the server then processes this request for you.

    A prime example of AJAX in action is sites that check if a username is in use when your signing up to them, this then updates the web page with data or returns an OK depending on what is found.

    You can submit complete forms via AJAX as long as the recieving script is scripted to cope with the data being pushed.

    @Mindzai - whilst that is true, you are however limited by two other factors, the POST limit as set by PHP config on web hosts and the database, if the post limit exceeds the field length then the data is truncated or returns an error depending on how graceful the set up is.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by \\.\ View Post
    @Mindzai - whilst that is true, you are however limited by two other factors, the POST limit as set by PHP config on web hosts and the database, if the post limit exceeds the field length then the data is truncated or returns an error depending on how graceful the set up is.
    Yep, as I said, "There are Apache and PHP setting which can limit the amount of data sent by POST". Good point about the database, that's certainly another consideration.

  5. #5
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372
    I may settle for using a POST submission on another page. Ample data means about 5000 characters, maybe more. I would like to learn a simple AJAX script instead of always avoiding it and working aroung my problem with GET and POST submissions to new pages. I'd check the forums here for the solution, but I can't figure out how to use these forum things, find my way around, or get the info I need... and I'm a trained usability professional!

    I've also googled how to do this with AJAX and have turned up squat. It doesn't seem like Ajax is a very published and discussed tool, which makes it difficult to learn. If anyone can give me a link to a very simple AJAX script, preferably one that can turn a JavaScript variable into a PHP, I would very much appreciate it. Thanks!

  6. #6
    Join Date
    Nov 2008
    Posts
    2,477
    5000 characters is nothing. Even with some fancy multibyte character encoding you are going to be looking at about 15KB of data at most. To put that into context, the default Apache LimitRequestBody directive is 0 (unlimited) and the default php max_post_size setting is 2MB, giving you room to increase the amount of data you are sending by well over 150 times before you need to worry, and this is a worst-case scenario.

    Not sure which search engine you've been using but a quick google for ajax -football turns up 150million+ results! However to make your life easy, i'd recommend using a library such as jQuery which has built in ajax functionality. An example script:

    Code:
    <script language="text/javascript">
    function sendData(toSend) {
        $.post(
            "your_script.php",
            {data : toSend},
            function(result) {
                // a callback function which receives data from the php script
                alert(result.response);
            },
            "json"
        );
    }
    var foo = "some data you want to send";
    sendData(foo);
    </script>
    your_script.php:

    PHP Code:
    <?php

    if (isset($_POST['data'])) {  
        
    // do something with the data
        
    echo json_encode(array('response' => "Woohoo! You sent '{$_POST['data']}' via AJAX!"));
    } else {
        echo 
    json_encode(array('response' => 'Failed - no data received'));
    }

    ?>
    Last edited by Mindzai; 05-29-2009 at 06:25 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles