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 Coder
    Join Date
    Jul 2011
    Posts
    24
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How to pass a javascript variable value to a PHP variable without refreshing the page

    Hi, experts, I want to pass a javascript variable value to a PHP variable at the same page without refreshing the web page, how could I do it?

    For your reference, here is a part of coding:

    <javascript...>

    function leftarrow(){
    var e = event.srcElement;
    var kname = e.name;
    var key = kname.substr(3, kname.length-1);
    location.replace("index.php?kk="+key+"&direct=left");
    }

    </javascript>


    I want to pass the value of 'key' and 'direct' to PHP variables.


    Thanks for your help.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,365
    Thanks
    61
    Thanked 530 Times in 517 Posts
    You need to look up ajax which can run an invisible http request for you.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    24
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Dear Tangoforce,

    Thank you very much for your quick response.

    I ever used Ajax to check whether a member's ID and password are right when he login. Generally, through javascript 'getDocumentById' to the values of ID and password, and through Ajax XMLHttpRequest() transfer to a PHP file to check their availabilities in the database, and 'responseText' to get the feedback message from the PHP file. The message will be presented in a textfield by Javascript.

    However, I never use the ajax to transfer the vaules of the variables back to some variables at the same PHP page.

    Could you kindly provide some detail steps on how to transfer the values to the variables back to a PHP variable in the same PHP page?

    Thank you very much for your help.

  • #4
    New Coder
    Join Date
    Jul 2011
    Posts
    24
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Dear Tangoforce,

    I think your suggestion over and agian.

    Maybe, you mean, I write javascript and ajax coding in a php file (for example, index.php), and also use this PHP file (index.php) as the receiver of the data transferred by XMLHTTPRequest(), and the feedback message sent by the same PHP file (index.php) will be put into a textfiled by, for instance, msg.innerHTML=.., but the feedback message will be set as 'hidden'.

    Does it make sense?

    Thank you.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,365
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Quote Originally Posted by sam9461 View Post
    Does it make sense?
    No not really.

    You've asked how to pass javascript variables to PHP without refreshing the page. The only way to do that is by using javascript and using ajax / xmlhttp.

    That is the only way.

    AJAX works just like the browser - it sends and receives data by get or post just like a normal http call. Therefore you write your php in the same way as you would for any normal page.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    sam9461 (01-23-2012)


  •  

    Posting Permissions

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