www.webdeveloper.com
Results 1 to 5 of 5

Thread: Posting a variable to a PHP file

  1. #1
    Join Date
    Jul 2013
    Posts
    3

    Posting a variable to a PHP file

    Using JQuery, The below code works perfect for me:

    $.post("myphpfile.php",
    {
    username:"Brian"

    }

    And even this works:

    var user = "Brian";

    $.post("myphpfile.php",
    {
    username:user

    }

    But I need this to work as well
    (it'll end up saving me a ton of coding and help me scale my project)

    var variable1 = "username";
    var user = "Brian";

    $.post("myphpfile.php",
    {
    variable1:user

    }

    But I can't figure out how to make javascript or jquery make that variable to the left of the colon work..
    Thanks for the help in advance!

  2. #2
    Join Date
    Jun 2008
    Posts
    106
    It doesn't work in object literals.
    You may have to do something like this:

    Code:
    var variable1 = "username";
    var user = "Brian";
    
    var obj = {};
    obj[variable1] = user;
    
    $.post("myphpfile.php", obj);


    Or if you're using ECMAScript-5 (check for the Object.defineProperty method), you can do this:

    Code:
    var variable1 = "username";
    var user = "Brian";
    
    $.post("myphpfile.php", Object.defineProperty({}, variable1, {value : user, enumerable : true}) );
    Last edited by tenfold; 07-29-2013 at 04:15 AM.

  3. #3
    Join Date
    Jul 2013
    Posts
    3
    Hmm I can't seem to get that to work.. I'm not sure if I'm using ECMAScript-5 (do I automatically have it since i'm using javascript?) - but I couldn't get that bottom code to work for me either..

  4. #4
    Join Date
    Jul 2013
    Posts
    3
    've been trying to get it to work like this for hours now.. I must be doing something else wrong?

    I've got my problem simplified to just these lines of code:


    HTML Code:
    var variable1 = "username";
    var user = "Brian";
    var temporaryObject = {};
    temporaryObject[ variable1 ] = user;
     $.post("myphpfile.php", {temporaryObject}, 
    function(data){
          $("#lbl-data").html(data).show();
        });
     );



    and myphpfile.php

    PHP Code:
    <?php
    $username
    =$_POST['username'];
    echo 
    "the variable the php file got and returned is " $_POST["username"];
    ?>
    This doesn't return anything from the php file.. however when I do:


    HTML Code:
    $.post("myphpfile.php", {username:user}, function(data){
          $("#lbl-data").html(data).show();
        });
    It works (and my element with the ID of lbl-data shows that echoed text from the php file)

    So the moment I change it to:

    HTML Code:
    $.post("myphpfile.php",
        {
        temporaryObject
          
        },
        function(data){
          $("#lbl-data").html(data).show();
        });
    I then don't get anything returned back form the php file.. Any ideas what I could be doing wrong?

  5. #5
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by deposition View Post
    Hmm I can't seem to get that to work.. I'm not sure if I'm using ECMAScript-5 (do I automatically have it since i'm using javascript?) - but I couldn't get that bottom code to work for me either..
    No, unfortunately. There's no one "JavaScript". There are different versions and standards. I'm assuming you're using a web browser, so it depends on what that browser has implemented. I know Firefox, Chrome, and IE10 use ES-5, not sure about the others. You shouldn't need it though. My first example should work, and it's more compatible with browsers.


    Quote Originally Posted by deposition View Post
    So the moment I change it to: (code) I then don't get anything returned back form the php file.. Any ideas what I could be doing wrong?
    You seem to be putting "temporaryObject" inside of another object. Why?
    "temporaryObject" is already an object, so no need to put it inside of another one.

    I'm not familiar with jQuery's post method, but try sending the object itself.
    Code:
    $.post("myphpfile.php", temporaryObject, function(data) {
         $("#lbl-data").html(data).show();
    });
    Last edited by tenfold; 07-29-2013 at 03:04 PM. Reason: it came out badly formatted, somehow

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