Arwa Moath
Arwa Moath

Reputation: 59

Can I use a form tag inside PHP code

I m trying to put a submit button inside a php code using form tag. however, I can't click the button while running the code

the code will be like that:

<?php
    print("<form action='page.php' method='post'>
        <input type='hidden' name='CourseID' value='".$Array[0]."'> 
        // $Array[0] comes from a Query in php                                            
        <input type='submit' value='Edit' name='Edit' >
    </form>");
?>

Upvotes: 0

Views: 13459

Answers (2)

spinozarabel
spinozarabel

Reputation: 41

It didn't work for me either! The form would display but selection would not work for a dropdown although the submit button worked. I was writing a form in PHP for a short code for wordpress plugin. I had to write the code without echo, using closing and opening of PHP tags and mixing HTML and PHP that way. For example:

echo '<form action="page.php" method="post">

had to be written as:

?> <form action="page.php" method=post""><?php

It is tricky to write code this way but with perseverance and method to keep code readable it works. Below is another example of inserting a HTML line of a form in PHP that shows how to use PHP variables inside ($term_name in our example below)

?>
        <option value="<?php echo htmlspecialchars($term_name); ?>"><?php echo htmlspecialchars($term_name); ?></option>
<?php

Upvotes: 1

Dhara Parmar
Dhara Parmar

Reputation: 8101

You can write any html using php as below:

 <?php
        echo '<form action="page.php" method="post">
            <input type="hidden" name="CourseID" value="'.$Array[0].'"> 
            <input type="submit" value="Edit" name="Edit">
        </form>';
    ?>

Upvotes: 2

Related Questions