paulyay
paulyay

Reputation: 67

Form not posting to database - PHP

I'm fairly new to PHP and designing a small blog. I'm trying to submit a form to create a new post but it isn't working and I can't figure out why. I've compared it to my working code for the register form and it seems the same. I'm not getting any errors, it's just reloading the page and not posting to the database.

View - v_newpost.php

    <article>
    <?php
    if (!isset ($_SESSION['username']))
    {
    ?>
    <span class="alert">Please login to create a post.</span>
    <?php
    }

    else 
    {
    ?>
    <form class="newpost" action="" method="post">
    <fieldset>
    <legend>Submit a new post</legend>
    <?php if ($error['alert'] != '') { echo "<span class='alert'>".$error['alert']."</span>";} ?>
    <ul>
    <li>
    <label for="title">Title:</label>
    <input type="text" name="title" value="<?php echo $input['title']; ?>" required autofocus>
    </li>
    <li>
    <label for="content">Content:</label>
    <textarea id="content" name="content" rows=6 value="<?php echo $input['content']; ?>"></textarea>
    </li>
    </ul>
    </fieldset>
    <fieldset>
    <button type="submit" class=postbutton>Publish</button>
    </fieldset>
    </form>
    </div>
    <?php
    }
    ?>
</article>

newpost.php

    <?php
    require_once 'includes/connection.php';

$error['alert'] = '';
$input['title'] = '';
$input['content'] = '';

if (isset($_POST['submit']))
{
    if ($_POST['title'] == '' || $_POST['content'] == '')
    {
        $error['alert'] = 'Please give your post a title and content.';

        $input['title'] = $_POST['title'];
        $input['content'] = $_POST['content'];

        include_once('v_newpost.php');
    }
    else
    {
        $input['title'] = htmlentities($_POST['title'], ENT_QUOTES);
        $input['content'] = htmlentities($_POST['content'], ENT_QUOTES);

        if ($stmt = $mysqli->prepare("INSERT INTO posts (title, content) VALUES (?,?)"))
        {
            $stmt->bind_param("ss", $input['title'], $input['content']);
            $stmt->execute();
            $stmt->close();

            $error['alert'] = '';
            $input['title'] = '';
            $input['content'] = '';

            header('Location: index.php');
        }
        else
        {
            $error['alert'] = 'Failed to create post';
        }
    }

}
else
{
    include_once('v_newpost.php');
}


?>

I'm sure it's probably something stupid, but I've looked over it so many times and can't understand why it isn't working...

Upvotes: 0

Views: 596

Answers (4)

giker
giker

Reputation: 4245

In newpost.php you have if (isset($_POST['submit'])) It'll not work because you do not send anything with name submit.

Upvotes: 0

greut
greut

Reputation: 4363

The following will never be true since you have no form element with name="submit".

if (isset($_POST['submit']))

Upvotes: 0

Jeremy Harris
Jeremy Harris

Reputation: 24549

You need an action in your form. Try:

<form class="newpost" action="newpost.php" method="post">

Upvotes: 0

Martin Dandanell
Martin Dandanell

Reputation: 851

You'll have to set the action of the form to point to newpost.php

Upvotes: 1

Related Questions