1

I want to retrieve a value from a HTML selection drop-down and store it into a PHP variable. I'm sure this has to be done in AJAX, but I'm not sure on the details. Is it possible to get that value of the option field and store it into a PHP variable this way?

$("#files").change(function() {
    $.ajax({
        type: "POST",
        url: "parse.html",
        data: {$'files'= $(this).val()},
        success: function(response) {
            //lolwut?
        }
    });
});

I want to store it in a php variable $x is what I just labeled it. The selection is title files.

Thanks.

1
  • There's no such thing of storing a PHP variable via AJAX. Your Javascript code send a "Request" with a collection of values, and on the PHP side you get those values (i.e. via $_GET/$_POST superglobals) and do whatever you want with them. Besides that, your code is not syntactically valid: {$'files' = $(this).val()} is not a valid object, you may start replacing that with { files: $(this).val() } Aug 16, 2013 at 17:42

1 Answer 1

0

You're pretty close. The data portion of the AJAX call should contain a key/value pair. Example:

data: {selectVal : this.value},

Now on your PHP backend, you can retrieve the variable with:

$selectValue = $_POST['selectVal'];

Do with it what you want, and you can return whatever you want to the client-side as well with echo. Whatever you echo back can now be picked up in the AJAX success callback under your variable response.

Also, set your url to your PHP page url.

2
  • Thanks tyme, I have embedded PHP on the same page as the html because this browses a directory on the server. Is there a way I can pass it to the same page? Parse.html has php within it.
    – Seth Earby
    Aug 16, 2013 at 17:52
  • Yup, keep the url empty, it'll send to that page.
    – tymeJV
    Aug 16, 2013 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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