user134930
user134930

Reputation: 3

Assign a HTML Button with a value to PHP

Hi do anyone know how to Assign a value to a "Submit" type button on HTML to the PHP process? Currently the user needs to click a button which would then assign a global variable a value. etc. button 1 = 10, button 2 = 30... Attached are the codes that I have typed in so far. Please help! Thanks!

<html>
<head>
    <title>Test </title>
</head>
<body>
    <h2>
        Test Generator</h2>
    <p>
        <br />
        <input type="submit" name="action" value="2 Weeks"/>
    <p>
        <input type="submit" name="action" value="1 Month"/></p>
    <p>
        <input type="submit" name="action" value="3 Months" /></p>
    <p>
        <input type="submit" name="action" value="6 Months" /></p>
    <p>
        <input type="submit" name="action" value="1 Year" /></p>

<?php

    $dateSelection;

    switch ($_POST['action']) 
    {
        case '2 Weeks':
            $dateSelection==1;
        break;
        case '1 Month':
            $dateSelection==2;
        break;
        case '3 Months':
            $dateSelection==3;
        break;
        case '6 Months':                    
            $dateSelection==4;
        break;
        case '1 Year':
            $dateSelection==5;
        break;
        default:
            echo "Something is wrong...";
        break;
    }
?>

</body>

Thanks!

Upvotes: 0

Views: 6682

Answers (2)

D&#233;j&#224; vu
D&#233;j&#224; vu

Reputation: 783

First of all make a form.

<form id="someform" method="post" action="<?php echo $_SERVER['REQUEST_URI'] ; ?>">
    <input type="submit" name="action" value="1 year">
    <input type="submit" name="action" value="2 year">
</form>

Then add php code on the same page at the bottom:

<?php
    echo $_POST['action'];
?>

Now if you load the page (it will show an error for now that's it not assigned) now click one of your buttons. It should show 1 or 2 years depending on what button you clicked.

If this is what you mean with your question i will help you further and remove errors and help you send it to another page if you want.

Upvotes: 1

SEoF
SEoF

Reputation: 1114

You could try putting the submit buttons inside a label for a radio button...

<?php
if(!empty($_POST['action']) {
    echo $_POST['action'],' weeks selected';
}
?>

<style>
.hidden-radio{display:none;}
</style>

<form action="" method="post">
    <input type="radio" name="action" value="2" id="2_weeks" class="hidden-radio" />
    <label for="2_weeks"><input type="submit" value="2 Weeks"/></label>

    <input type="radio" name="action" value="4" id="4_weeks" class="hidden-radio" />
    <label for="4_weeks"><input type="submit" value="1 Month"/></label>

    <input type="radio" name="action" value="13" id="13_weeks" class="hidden-radio" />
    <label for="13_weeks"><input type="submit" value="3 Months"/></label>

    <input type="radio" name="action" value="26" id="26_weeks" class="hidden-radio" />
    <label for="26_weeks"><input type="submit" value="6 Months"/></label>

    <input type="radio" name="action" value="52" id="52_weeks" class="hidden-radio" />
    <label for="52_weeks"><input type="submit" value="1 Year"/></label>
</form>

Upvotes: 0

Related Questions