9

Creating Files from Radio Button Tags

 2 years ago
source link: https://www.codesd.com/item/creating-files-from-radio-button-tags.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creating Files from Radio Button Tags

advertisements

I want to create a text file (or at least to echo) with some values taken from a radio button list.

Looking for info, I have been able to build the radio forms like this:

<!DOCTYPE html>
<html>
<head>
   <title>Config</title> <!-- Include CSS File Here-->
   <link href="css/style.css" rel="stylesheet">
</head>
<body>
   <div class="container">
      <div class="main">
         <!---- Radio Button Starts Here ----->
         <form>
            <label class="heading">First value </label><br>
            <input name="v1" type="radio" value="v1text1">Value 1 - Option 1<br>
            <input name="v1" type="radio" value="v1text2">Value 2 - Option 1<br>
         </form>
         <br>
         <form>
            <label class="heading">Second value </label><br>
            <input name="v2" type="radio" value="v2text1">Value 2 - Option 1<br>
            <input name="v2" type="radio" value="v2text2">Value 2 - Option 2<br>
            <input name="v2" type="radio" value="v2text3">Value 2 - Option 3
         </form>
         <input name="submit" type="submit" value="Submit">
      </div>
   </div>
</body>
</html>

And now I would like to take the values v1 and v2, so I found the following php code (my idea would be to do this with each one of the values):

<?php
   if (isset($_POST['v1']))
     echo $_POST['v1'];
   else
?>

So I added it after the submit button code, however after selecting values and clicking on Submit button nothing happens.

I know nothing about php, the code I have written has been taken from google findings.


First of all create a single form, & place the <input name="submit" type="submit" value="Submit"> inside the <form> tag.

We need certain parameters for submitting the form, just like method & action parameter. Always use 'post' method for better security.

Now, modify the <form> tag with this code:

<form method="post" name="form1" action="">

And in 'action' parameter , write the path of the page where you want to submit the form & want to display the values of radio buttons. Just like this

<form method="post" name="form1" action="results.php">

otherwise leave the action parameter 'Blank' just like the first <form> tag, if you want to submit & get the radio button values on the same page.

Now, the code for retrieving the values of radio buttons:

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

Place it just above the html tag.

Now, here is your full updated code for proper understanding, just copy & replace with your code.

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

<!DOCTYPE html>
<html>
<head>
   <title>Config</title> <!-- Include CSS File Here-->
   <link href="css/style.css" rel="stylesheet">
</head>
<body>
   <div class="container">
      <div class="main">
         <!---- Radio Button Starts Here ----->
         <form method="post" name="form1" action="">
            <label class="heading">First value </label><br>
            <input name="v1" type="radio" value="v1text1">Value 1 - Option 1<br>
            <input name="v1" type="radio" value="v1text2">Value 2 - Option 1<br>
            <br/>
            <label class="heading">Second value </label><br>
            <input name="v2" type="radio" value="v2text1">Value 2 - Option 1<br>
            <input name="v2" type="radio" value="v2text2">Value 2 - Option 2<br>
            <input name="v2" type="radio" value="v2text3">Value 2 - Option 3

            <input name="submit" type="submit" value="Submit">
         </form>

      </div>
   </div>
</body>
</html>

Hope, this may be useful to you.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK