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 9 of 9
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Passing jquery values to php

    Hi, i have a question regarding passing jquery values to php. I tried using Firebug, and my values got displayed in Firebug's post parameter/source sections, but the value was not even in my post array when i var_dump the array. Is there something wrong with my code?

    Code:
    //JS code
    function check_pwd()
    {
    	pwd = prompt("please input your password","");
    	$.post("/someurl", { jspwd: pwd } );
    	return true;
    }
    PHP Code:
    //PHP code, using CodeIgniter syntax. jspwd was not included in the post values array
    var_dump($this->input->post()); 
    I tried to set a hidden value in my html using

    Code:
    pwd = prompt("please input your password","");
    $("#pwd").attr('value', pwd);
    as well but it did not work too. If anyone has other suggestions on how to pass values from a Javascript popupbox to a php page I am willing to try it too.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by capypara View Post
    function check_pwd()
    {
    pwd = prompt("please input your password","");
    $.post("/someurl", { jspwd: pwd } );
    return true;
    }
    That is not a native JavaScript code. Do you use a JavaScript framework/library? Which one? JQuery? Prototype? MooTools? Google API? Yahoo API? Other?

    Don't make us guess, please.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    If (by any chance) you are using jQuery ... did you try to access the parameter using the PHP standard $_POST["jspwd"]?

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,474
    Thanks
    6
    Thanked 980 Times in 953 Posts
    You guys should read the thread title or the first sentence in the OP’s post where it says which framework this is. It was just posted in the wrong forum.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by VIPStephan View Post
    You guys should read the thread title or the first sentence in the OPís post where it says which framework this is. It was just posted in the wrong forum.
    Well yes... Could be... except that I have never heard about "jquery values"
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    If (by any chance) you are using jQuery ... did you try to access the parameter using the PHP standard $_POST["jspwd"]?
    Hi there, yes i have tried it and i get the same error.

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined index: jspwd

    And sorry if i did not describe the problem well enough. I am using PHP for server side and JQuery and Javascript for client side, and using the Codeigniter framework for this project.

    The main thing that i want is to pass values from a popup input box from client side to my PHP side, but both $.post and setting a value by using $("#pwd").attr('value', pwd); does not seem to be working.
    Am i using this function $.post correctly?
    Last edited by capypara; 11-24-2011 at 03:06 AM.

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Regarding the main problem where i need to pass a value from a popup input box from client side to my PHP side,

    $("#pwd").attr('value', pwd);

    This method works now after some problem solving. However I still cant get $.post to work =/ Maybe i still dont understand how $.post functions...............

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    One way of working with $.post is like this
    Code:
    $.post(url, {parameters}, callback);
    url is a string containing the (relative or absolute) URL to the server side script (PHP in your case)

    parameters is a list of parameters. Example for you jspwd parameter
    Code:
    {'jspwd' : $('#pwd').val()}
    The PHP script will then receive the parameter as $_POST["jspwd"]

    callback is a function that will be executed after the request has successfully finished.
    Code:
    $.post('path/to/your.php', {'jspwd': $('#pwd').val()}, function(data) {
       alert(data);  // this will alert the output of the PHP script
    });
    Most importantly: $.post() starts an asynchronous request which means that the "Javascript program flow" will continue to execute even if the request is not yet finished. So if you have a construct like this
    Code:
    function whatever() {
       $.post(...);
       return true;
    }
    then the "return true" will be executed before(!) the request finishes.

  • Users who have thanked devnull69 for this post:

    capypara (11-24-2011)

  • #9
    New Coder
    Join Date
    Aug 2011
    Posts
    49
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the explanation devnull69 =) I have seen several sites that include the third parameter but i had no idea what that was used for. Will take a good look at $.post again later on.


  •  

    Posting Permissions

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