Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Javascript variable -> php variable

    My question is pretty simple but I am such a noob to ajax.
    My problem is I have a variable in my javascript that I want to pass to my php code in order for one dropdown list to load a list of information depending on what the user selects on the first list

    How am I supposed to pass my variable from javascript to my php?

    <!--javascript code-->
    document.getElementById('IPAID').onchange = function changeIPAValue(){
    var IPAID = $('this').value;
    $.ajax({
    url: 'member_add.php',
    type: 'get'
    data: IPAID
    success: alert("Test Value #" + IPAID)
    }

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    When using GET you add the variables in the querystring of the URL. It is only when you use POST that the data gets passed separately from the URL.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    aclagarde (10-23-2013)

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    @felgall: For jQuery $.ajax() it doesn't matter. You can specify the same data option for both request methods.

    But: It should always be a key/value pair like
    Code:
    data: {myvalue: IPAID}
    so that on server side you can access the value by key

    In PHP: $_GET["myvalue"] or $_POST["myvalue"]

  • Users who have thanked devnull69 for this post:

    aclagarde (10-23-2013)

  • #4
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I tried both of your guys suggestions (changing to post and changing my syntax and assigning a key value to the actual value) still no luck.

    I'm starting to think whether or not its because I'm not implementing my ajax correctly?
    I only have a script statement at the verrry top of my page

    <script src="../../js/jQuery v1.10.2 (Update20131017).js" type = "text/javascript"></script>

    this is my script for the ajax call now...

    <script>

    document.getElementById('IPAID').onchange = function changeIPAValue(){
    var IPAID = this.value;
    $.ajax(
    {
    url: 'member_add.php',
    type: "post"
    data: {myvalue: IPAID},
    success: function(data){
    }
    }
    );
    </script>

    this is my call to the function....

    <?php
    $IPAID = $_POST['myvalue'];
    echo $IPAID; ?>

    ** my javascript is at the very bottom of my page, before the <?php?> portion of my code if thats going to make a difference...

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Please read my PM answer to you

    Generally, you need to make sure that your script code is either after the HTML (right before the </body> tag) or you have to use a construct that will force Javascript to wait for the HTML to be loaded before executing your code.


  •  

    Posting Permissions

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