简体   繁体   中英

How to change two variables value in PHP from a JavaScript onChange event

I have a form with the following variables:

$amt and $f

$a1 and $f1
$a2 and $f2
$a3 and $f3
$a4 and $f4

$amt and $a are input numbers, while $f are label text

<input id="a1" name="a1" type="text" value="<?php echo $a1; ?>" onChange="????, document.e.submit();" />
(the same to a2, a3, and a4)

What I want to do is onChange, I need to replace $amt and $f.

$amt = $a1 (the user enters new) $f = $f1

How can I do that?

PHP variables and javascript are two different things.

PHP is server side, javascript client side(browser).

If you only want to swap the data locally (you didn't explain why you needed to swap the variables so I can only assume things), I suggest you to declare Javascript variable initialised with php variables.

What you need to known is that you use php to generate a page (containing html/css and javascript) with various variables. It generates text. So there is no link between PHP variables and generated output.

您可以使用ajax从服务器获取新的变量值,并使用javascript来完成所需的操作。

You will need to use AJAX for this, i you need them processed on the back end. Assuming you are using pure JavaScript and not a library, you'll want to do something along these lines:

//for cross browser support
var XMLHttpFactories = 
[
    function () {return new XMLHttpRequest()},
    function () {return new ActiveXObject("Msxml2.XMLHTTP")},
    function () {return new ActiveXObject("Msxml3.XMLHTTP")},
    function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

//find an xmlhttpfactory that will work
function createXMLHTTPObject() 
{
    var xmlhttp = false;
    for (var i=0;i<XMLHttpFactories.length;i++) {
            try {
                    xmlhttp = XMLHttpFactories[i]();
            }
            catch (e) {
                    continue;
            }
            break;
    }
    return xmlhttp;
};

//process post variables for your AJAX request
function parsePostData(postData)
{
    var appendString = '?';
    for(key in postData)
        {
            appendString += key + '=' + postData[key] + '&';
        };
        appendStrTrim = appendString.substring(0, appendString.length-1)
    return appendStrTrim;
};

//this is where the magic happens
    function sendRequest(url,callback,postData) 
{
var req = createXMLHTTPObject();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method,url,true);
if (postData){
    req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    if (postData.passwd){
        req.withCredentials = true;
    }
}
req.onreadystatechange = function () {
        if (req.readyState != 4) return;
        if (req.status != 200 && req.status != 304) {
            var errorCode = 'HTTP error: ' + req.status;
            callback(errorCode);
        }

        var response = JSON.parse(req.response)
        if (response.jquery){
            console.log(req.getAllResponseHeaders());
        }
        if(response.data && response.data.modhash){
            reddit.control.setModHash(response.data.modhash )
        }
        if(response.data && response.data.children && typeof callback =='function'){
            callback(response.data.children);
        }else if(response.data && typeof callback=='function'){
            callback(response.data);
        }else if(typeof callback== 'function'){
            callback(response);
        };
}
if (req.readyState == 4) return;
var postString = parsePostData(postData);
req.send(postString);
};

So then, when you want to change the PHP variable, you would call sendRequest() like so:

`sendRequest('http://example.com/changeThosePHPVariables.php', function(){/*do this when done*/}, {amt: 'value', f: 'value'})

then you just need to reasign the variables using changeThosePHPVariables.php , where you'll find them as $_POST['amt'] and $_POST['f'] .

This all gets much easier if you're using a library like JQuery where you can just use $.ajax() to do the dirty work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM