-2

Need to get selected radio button value stored in a variable and I am not sure how to go about it

<input type="radio" name="spilldeck" class="happy" id="happy" value="0" checked />
<input type="radio" name="spilldeck" class="neutral" id="neutral" value="1" />
<input type="radio" name="spilldeck" class="sad" id="sad" value="2" />

I think I can listen to which radio button has been clicked by adding onchange event

<input type="radio" name="spilldeck" class="happy" id="happy" value="0" onChange="getValue(this)" checked />
<input type="radio" name="spilldeck" class="neutral" id="neutral" value="1" onChange="getValue(this)" />
<input type="radio" name="spilldeck" class="sad" id="sad" value="2" onChange="getValue(this)" />

Now I need to store selected value in $spilldeck = ...............

user2686655
  • 71
  • 1
  • 1
  • 10
  • 1
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – B001ᛦ Jun 08 '21 at 20:43

2 Answers2

2

You want to perform a calculation based on the value of the radiobuttons.

You prefer to use php for it, so the data has to be send to the server, otherwise you can't assign the data to a variable, well you can but not a php variable. Then you should use javascript, because php is serverside.

<form action="YOURPAGE.php" method="POST">
  <input type="radio" name="spilldeck" class="happy" value="0"/>
  <input type="radio" name="spilldeck" class="neutral" value="1"/>
  <input type="radio" name="spilldeck" class="sad" value="2"/>
 <button type="submit">submit</button>
</form>

This will be the form that you can use. Now you want the data saved to a variable so u can perform a calculation right?

if($_SERVER['REQUEST_METHOD'] == "POST"){
    $spilldeck = $_POST['spilldeck']
}

This way your value of the selected radiobutton is stored. Now you want to perform the calculation, because the spilldeck variable is created.

so write:

if(isset($spilldeck)){
    your function, calculation whatever
}

Afther that you can redirect to an other page or whatever.

Does this solve your question?

Jazzly
  • 66
  • 5
-1

Example:

<form action="YOUR_ACTION" method="post">
    <input type="radio" name="spilldeck1" class="happy" id="happy" value="0" onChange="getValue(this)" checked />
    <input type="radio" name="spilldeck2" class="neutral" id="neutral" value="1" onChange="getValue(this)" />
    <input type="radio" name="spilldeck3" class="sad" id="sad" value="2" onChange="getValue(this)" />
    <button name="submit" type="submit">Save</button>
</form>

<?php

if ($_POST['submit']) {
    $spilldeck1 = $_POST['spilldeck1'];
    $spilldeck2 = $_POST['spilldeck2'];
    $spilldeck3 = $_POST['spilldeck3'];
}




B001ᛦ
  • 2,036
  • 6
  • 23
  • 31