简体   繁体   中英

Pass value of html to PHP variable

I'm having some trouble passing the value of some HTML elements to PHP variable. I have a PHP web app where I give the users some drop down option. What I want to do is after the user has selected the option I give than to click on a button and make some calculations based on his/her selection.

I have tried using the POST and GET method but because I'm new to php I probably did it wrong. I also try with ids but no results.

Here is the HTML code The 1st dropdown menu :

<div>
   <select id="kleidwma" onchange="kleidwmata_change(this.value);kwdikos();ypsos();" 
       class="form-control selectpicker kleidwmata" data-size="10"  data- 
       style="btn-white" name="guard_kleidwmata" required  >

       <option value="" >&nbsp;</option>
       <option value="12">12 MS</option>
       <option value="14">14 KETE</option>
       <option value="15">15 ARRIKTON</option>
       <option value="16">16 KETE</option>
       <option value="22">22 KETE</option>
   </select>
</div>

The 2nd dropdown menu :

<div>
    <select id="sigkratisi" onchange="kwdikos()" name="sigkratisi" 
        class="form-control sigkratisi" required>

        <option value="" ></option>
    <option value="metalliki" >Μεταλλική</option>
    <option value="xilogonia" >Ξυλογωνία</option>
     </select>
</div>

Here is the button to be clicked after the selection has been made :

<div class="row" style="margin-top:20px; margin-left: 235px;">
    <div class="col-md-7">
    <input type="hidden" name="tmp_calculate" value="pending" />
    <button onclick="calculate()" type="button" class="btn btn-success" > 
    <?php echo "Υπολογισμός";?></button>
    </div> 
</div>

And finally here is the function that is called :

function calculate()
{
    $var1 =   sigkratisi;
    $var2 =   kleidwma;
        "code to be executed base on the var1 and var2"
}

I hope it's clear enough. Thanks in advance for your time

you can use Forms

<form name="f" action="" method="POST">
<div>
<select id="kleidwma" name="variable" 
   class="form-control selectpicker kleidwmata" data-size="10"  data- 
   style="btn-white" name="guard_kleidwmata" required  >
   <option value="" >&nbsp;</option>
   <option value="12">12 MS</option>
   <option value="14">14 KETE</option>
   <option value="15">15 ARRIKTON</option>
   <option value="16">16 KETE</option>
   <option value="22">22 KETE</option>
</select>
<input type="submit" value="submit" />
</div>
</form>

in PHP

<?php
if(isset($_POST['variable']))
{
echo $_POST['variable'];
}
?>

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