Search code examples
javascriptphpinputradio-button

How do I get value from input radio in a href?


I'm creating a card with values, example:

Subscribers:
Value: 5,50$
Value: 9,50$
Value: 15,50$
--------

Each radio input has a value and a link as a variable... so I'm passing a variable with the link to your checkout in the value... but I want that when I select a value like that, it passes the correct variable to the submit .

<?php $url = 'wwww.exemplo.com/checkout1'?>
<?php $url2 = 'wwww.exemplo.com/checkout2'?>
<?php $url3 = 'wwww.exemplo.com/checkout3'?>
<?php $url4 = 'wwww.exemplo.com/checkout4'?>
<form>
<input type="radio" name="opcao" id="1" value="<?php echo $url;?>">
<input type="radio" name="opcao" id="2" value="<?php echo $url2;?>">
<input type="radio" name="opcao" id="3" value="<?php echo $url3;?>">
<input type="radio" name="opcao" id="4" value="<?php echo $url4;?>">    
<a href=""><button id="submit">Submit</button>
</form>

How do I get the input value in my href


Solution

  • You dont actually need to use an anchor tag, you can achieve a querystring and filling the $_GET array using a <form method="get" ....>. If that is your aim?

    
    <?php
    // start by using an array for your values, its more useful and extendable
    $urls = ['wwww.exemplo.com/checkout1',
            'wwww.exemplo.com/checkout2',
            'wwww.exemplo.com/checkout3',
            'wwww.exemplo.com/checkout4'
            ];
    # Now use a form with a GET method, 
    # which will set the querystring and fill the $_GET array in the `action` form
    ?>
    <form action="action_page.php" method="get">
    
    <?php
    foreach ( $urls as $id => $url){
        echo "<input type='radio' name='opcao' id='$id' value='$url'>";
    }
    ?>
        <button type="submit" id="submit">Submit</button>
    </form>
    

    -- UPDATE

    I dont think you actually want a radio button set for this, all you need is a set of anchor tags like this

    <?php
    // start by using an array for your values, its more useful and extendable
    $urls = ['wwww.exemplo.com/checkout1',
            'wwww.exemplo.com/checkout2',
            'wwww.exemplo.com/checkout3',
            'wwww.exemplo.com/checkout4'
            ];
    <?php
    foreach ( $urls as $id => $url){
        echo "<a href='http://$url'> SITE $id</a>";
    }
    ?>
    

    ---UPDATE 2

    $urls = ['https://wwww.exemplo.com/checkout1',
            'https://wwww.exemplo.com/checkout2',
            'https://wwww.exemplo.com/checkout3',
            'https://wwww.exemplo.com/checkout4'
            ];
    
    foreach ( $urls as $id => $url){
        echo "<a href='$url'> SITE $id</a>";
    
    }
    

    Should get you

    <a href='https://wwww.exemplo.com/checkout1'> SITE 0</a>
    <a href='https://wwww.exemplo.com/checkout2'> SITE 1</a>
    <a href='https://wwww.exemplo.com/checkout3'> SITE 2</a>
    <a href='https://wwww.exemplo.com/checkout4'> SITE 3</a>
    

    Also, dont be afraid of 'https%3A%2F%2Fwwww.exemplo.com%2Fcheckout1' as that is just 'https://Fwwww.exemplo.com?checkout1' that has been URL Encoded.