1

I'm trying to pass two javascript variables to PHP. When there's only one then it's working but I have no clue how to send two at once. Here's my code

    function save(nr) {
     var xr = new XMLHttpRequest();
     var url = 'saveColor.php';
     var text = document.getElementById('color'+nr).value;
     var vars1 = "newText="+text;
     var vars2 = "variable="+nr;

     xr.open("POST", url, true);
     xr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     xr.send(vars1);
}

So sending the "vars1" variable works fine, but where or how I should put "vars2" to send them at once? Something like

xr.send(vars1, vars2);

Doesn't work.

2 Answers 2

2

This will work fine:

var data = new FormData();
data.append('var1', 'var1value');
data.append('var2', 'var2value');

and then use it like you tried before

xr.send(data);
Sign up to request clarification or add additional context in comments.

6 Comments

If I use this will I be able to get those separately or will it be treated as one variable in PHP?
It will be converted to string like that var1=var1valuee&var2=var2value. First check, then post a comment
I checked it and something is still not working. If I followed your example should I get those variables in PHP via $someVar = $_POST[var1]; etc? Because that's not working ($someVar doesn't show anything when I echo it)
dump entire $_POST variable
echoing $someVar = var_dump($_POST[var1]); doesn't output anything either if that's what you meant.
|
0

It can also be implemented this way

var strURL = "auto_ppm_plan_generation_table.php?ppm_frequency=" + ppm_frequency + "&ppm_month=" + ppm_month + "&ppm_week=" + ppm_week;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.