Php Post Action

I want to post my action according to select entries. However, my code does not work. How can I do? Thank you.

 <form action=<?php echo $filename; ?> method="post">
<br/>
    <input type="submit" name="select" value="A" onclick="selecta()" />
    <br/>
    <input type="submit" name="select" value="B" onclick="selectb()" />
    <br/>
    <input type="submit" name="select" value="C" onclick="selectc()" />
    </form>
    <?php
        function selecta(){
            $filename = "a.php";
        }
        function selectb(){
            $filename = "b.php";
        }
        function selectc(){
            $filename = "c.php";
        }
        ?>

You cannot use PHP to dynamically change HTML. Use JavaScript:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<form action="#" method="post" id="myform">
<br/><input type="submit" name="select" value="A" onclick="selecta()"/>
<br/><input type="submit" name="select" value="B" onclick="selectb()"/>
<br/><input type="submit" name="select" value="C" onclick="selectc()"/>
</form>
<script>
var action = 'a.php';
$(function(){
    $('#myform').submit(function(){
        $(this).attr('action', action);
    });
});
function selecta() {
    action = 'a.php';
}
function selectb() {
    action = 'b.php';
}
function selectc() {
    action = 'c.php';
}
</script>

I assume you want this code on server side, since you are using PHP

  <?php 

    $filename = '';


    $files =
    [
        'A' => 'a',
        'B' => 'b',
        'C' => 'c',
    ]

    if( !empty( $_POST['select'] ) )
    {
        if( isset( $files[ $_POST['select'] ] ) )
        {
          $filename = $files[ $_POST['select'] ].'.php';
        }
    }

    ?>

    <form action= "<?php echo $filename; ?>" method="post"> <br/>

        <input type="submit" name="select" value="A"/> <br/>
        <input type="submit" name="select" value="B"/> <br/>
        <input type="submit" name="select" value="C" />

    </form>

Route::get('anyString','UserController@profile');

  • When you type anyString on url then it call UserController Profile method.