Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I apologise if you think this might be a duplicate but I've tried everything I've come across and the problem persists - it's driving me mad!

I basically want to pass a JS variable to a PHP variable whenever a key is hit.

This is a simplified version of how my PHP file is set up (located at http://localhost/quiz/index.php)

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
    <head>
    <body>
        <p>
            <?php
                $uid = $_POST['userID'];
                echo $uid;
            ?>
        </p>
        <script src="js/site.js"></script>
    </body>
</html>

Then in the site.js file I have the following:

jQuery(document).ready(function($) {

$(document.body)
.keyup(function() {
    var userID = "Jim";
    $.ajax({
        type: "POST",
        url: 'http://localhost/quiz/index.php',
        data: { userID : userID },
        success: function(data)
        {
            alert("success!");
        }
    });
});

});

Of course, I am wanting the JS variable userID value ('Jim') to output in the paragraph when a key is pressed, but it only ever shows:

Notice: Undefined index: userID in C:\xampp\htdocs\quiz\index.php on line 66

However, when a key is pressed the "success" alert pops up.

Does anyone know where I'm going wrong here? I'm using Xampp if that's relevant!

Thanks!

share|improve this question
    
Try ` data: { "userID" : userID },` instead of ` data: { userID : userID },`. –  Vikram Deshmukh Dec 13 '13 at 11:01
    
No difference :( –  user2586455 Dec 13 '13 at 11:03

1 Answer 1

You need two PHP files (or one with some branching logic).

  • One to provide the HTML document that loads the JS
  • One to provide the Ajax response

You are getting the error because the userID is not submitted in a POST request to load the initial page.

Then you need to modify your JavaScript so that it does something with data instead of just alerting.

share|improve this answer
    
Thanks for your reply! Although I am even more confused now! What goes in the file to provide the Ajax response? (Sorry, I am very inexperienced with Ajax) –  user2586455 Dec 13 '13 at 11:11
    
It should be a program that reads the data you post to it, does whatever you want to do with that data, and then responds with whatever you want to tell the browser in response to that data. (in this case, it would just output $_POST['userID'], which is pointless because the browser already has that information, but you did say you were simplifying) –  Quentin Dec 13 '13 at 11:13
    
I apologise for my noob questions but I'm still at a loss as to what to do next. I just want to send that JS variable value to the PHP variable. How do I submit userID in a POST request as you suggested? –  user2586455 Dec 13 '13 at 11:22
    
The code you already have for submitting userID is fine. –  Quentin Dec 13 '13 at 11:23
    
I understand why the error appears initially (because the PHP variable isn't set initially), but when a key is pressed, shouldn't it then receive the JavaScript value? Can you point me in the right direction for what I need to do? –  user2586455 Dec 13 '13 at 11:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.