存储jquery变量,转移到php并检查

Ok, so I'm trying this again. I'm trying to validate a users age by country - all selected from dropdowns.

First I use the dropdown to select country, the value of each is 16/17/18 etc so I'm using this script to capture that:

function displayVals() {
    var singleValues = $("#country").val();
    }

then I'm sending that variable to php:

$countryAge = $_GET['singleValues'];

next I want it to be the result of this function:

    function age_required() {
    return absint($countryAge);
}

It's just reloading the page, so not throwing any errors that I can see, but also not displaying the notifications that the person is too young etc. It was working before I started meddling.

Any ideas of why it doesn't work? What am I doing wrong? I'm NOT a php/js guru.

$_GET retrieves the value from a querystring, so when you are posting or some other way going to your PHP page, you need to make sure the url includes "?SingleValues=(YOURVALUEHERE)".

http://php.net/manual/en/reserved.variables.get.php

It sounds to me like you may be a bit confused over the role that both JavaScript and PHP play in web applications.

JavaScript runs in the client (i.e. the browser). The source code lives on your webserver, and then gets downloaded in whole to the browser before it gets executed.

By contrast, PHP runs on the server. Its purpose is to create X/HTML markup as output, which the web server then sends to the browser.

In order to get data from the browser to PHP and then back again you need to generate a request. This can be done on the fly (read: without a new page load) using AJAX. There's lots of questions here on SO about AJAX, so I suggest that you begin exploring those questions/answers.

To help you off, here's a few tutorials:

AJAX Using Native JavaScript

AJAX Using jQuery Framework