A
A
Alexander2015-10-11 21:28:33
PHP
Alexander, 2015-10-11 21:28:33

How to pass data from js to html to php?

There is a dynamic variable that is displayed in the html button, which serves as the submission of the html form:

<button type="submit" id="submit"  name="submit" class="btn btn-block btn-lg btn-primary pull-right">
          Заказать
          <span id="ppp" class="badge">0</span>
        </button>

In this case, it's ppp .
The value is assigned using this script:
function myFunction() {
    var x = document.getElementById("s2").value;
    var y = document.getElementById("s1").value;
        document.getElementById("i2").src= "assets/img/ranks/"+document.getElementById("s2").value+".jpg";
                
zx=prise(x);
zy=prise(y);
i=zx-zy;
if(i<0){i=0};
document.getElementById("ppp").innerHTML= i+" рублей";
}
function myFunction1() {
    var y = document.getElementById("s1").value;
    document.getElementById("i1").src= "assets/img/ranks/"+document.getElementById("s1").value+".jpg";
    var x = document.getElementById("s2").value;
    
zx=prise(x);
zy=prise(y);
i=zx-zy;
if(i<0){i=0};

document.getElementById("ppp").innerHTML= i+" рублей";
   }
   function prise(x) {
        var prise ={ "Silver_I":75,
                    "Silver_II":75,
                    "Silver_III":75,
                    "Silver_IV":75,
                    "Silver_Elite":75,
                    "Silver_Elite_Master":75,
                    "Gold_Nova_I":130,
                    "Gold_Nova_II":130,
                    "Gold_Nova_III":130,
                    "Gold_Nova_Master":130,
                    "Master_Guardian_I":130,
                    "Master_Guardian_II":130,
                    "Master_Guardian_Elite":300,
                    "Distinguished_Master_Guardian":300,
                    "Legendary_Eagle":300,
                    "Legendary_Eagle_Master":500,
                    "Supreme_Master_First_Class":600,
                    "Global_Elite":700};
                    
    var a = 0;
    cena = 0;    
    for(var key in prise){
                    if(key == x){cena += prise[key];a=a+1;return cena; break;}    
                 a=a+1;    
                    cena += prise[key];
                    
               }
    }

How to pass this value to php file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2015-10-11
@selmers

<button type="submit">means it is in some form. Once in the form, it is enough to let this form do what the forms are intended for - send data to the server. You can do this, for example, by adding it to the form <input type="hidden"/>(just don't forget to give it a name, nameless inputs don't go to the server).

I
Ivanq, 2015-10-11
@Ivanq

If you need to pass a js variable to php, you need to send an ajax request to the server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question