1

I've got a problem with simple PHP form:

<form class="form-horizontal" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" align="center">
    <?php 
        session_start ();
        if (isset($_SESSION['login'])) {
            echo'<div class="row">';
            echo '<textarea style="resize: none; width: 45%; height: 150px;" name="comments" id="comments" placeholder="Leave a comment here..."></textarea>';
            echo '</div>';
            echo '<div class="row" style="margin-top: 10px;">';
            echo '<button type="button" class="btn btn-success" name="complete">Submit</button>';
            echo '</div>';
        } else {
            echo '<h4>You must be logged in to add comments.</h4>';
        }
    ?>
</form>

And PHP request:

<?php
    if (isset($_POST['complete'])) {
        $text_field = $_POST['comments'];
        $result = mysqli_query($link, "INSERT INTO `comments` (`text`) VALUES ('$text_field')");
    } 
?>

When I click on button nothing happens.

halfer
  • 19,824
  • 17
  • 99
  • 186
I. Gursky
  • 31
  • 1
  • 3
  • 2
    because your button has type `button` nothing happens, either remove the attribute or change it to `submit` – Dale Jun 15 '16 at 13:46
  • If you want to use `type="button"` you'll have to JS to submit the form. Otherwise change it per @Alok's answer. – Greg McMullen Jun 15 '16 at 13:50

1 Answers1

3

Change your button type to submit instead of button. Like this:

<button type="submit" class="btn btn-success" name="complete">Submit</button>

button type doesn't enforce form to be submitted. If you're required to use button type you can handle the form submission on JS by handling onclick event of a button.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47