MAS1
MAS1

Reputation: 1711

how to pass php variable value to action attribute of html form

I want to pass php variable value as a action to html form. I am trying as follows, but it is not working.

<?php
    $url='test.php';
?>

<html>
    <body>
        <form name="upload" action="<?=$url?>" method="post" >
            <input type="submit" value="submit">
        </form>
    </body>
</html>

All this code are in one php file.

Upvotes: 5

Views: 35010

Answers (4)

rmarscher
rmarscher

Reputation: 5642

Sounds like you need to enable short_open_tag if your example doesn't work.

<?php
    ini_set('short_open_tag', 'on');
    $url='test.php';
?>

<html>
    <body>
        <form name="upload" action="<?=$url?>" method="post" >
            <input type="submit" value="submit">
        </form>
    </body>
</html>

Alternately, write it like this:

<?php
    $url='test.php';
?>

<html>
    <body>
        <form name="upload" action="<?php echo $url ?>" method="post" >
            <input type="submit" value="submit">
        </form>
    </body>
</html>

Upvotes: 4

Jakob Kruse
Jakob Kruse

Reputation: 2494

Remove your single quotes:

<form name="upload" action="<?=$url?>" method="post">

Upvotes: 0

webbiedave
webbiedave

Reputation: 48887

Have you tried <?php echo $url ?> If it works, then short_open_tag in the php.ini is turned off. That means you will need to either turn it on or use the long open tag <?php throughout your code.

Upvotes: 8

nc3b
nc3b

Reputation: 16270

Try this

<form name="upload" action="<? echo $url ?>" method="post" >

Upvotes: 2

Related Questions