Vranvs
Vranvs

Reputation: 1521

if isset($_POST issue

I have one instance of where a user on my PHP site can login, and I'm trying to make a second where administrators can post announcements.

My login works beautifully, but for some reason I can't get my announcement submission to work. Despite my input fields being named appropriately, the field is being seen as "not set" when I type in a title and announcement on my form. Here is my code:

FORM:

        <form action="announce.php">
    <input  type="text" name="title" />
    <textarea  name="announce" cols="20" rows="2" ></textarea>
    <input type="submit" name="submit" value="Announce" />
    </form>

Here is my PHP:

include_once 'creds.php';
$con=mysqli_connect("$db_hostname","$db_username","$db_password","$db_database");

if (isset($_POST['title']))
{
    $title = $_POST['title'];
    $announce = $_POST['announce'];

$query = "INSERT INTO announcements (labname, name, author, announce)
        VALUES ($lab, $title, $username, $announce)";

$insert = mysqli_query($con, $query);

mysqli_close($con);

echo "added successfully";}
else
{
echo "something went wrong";
}

My other form seems to work but this one doesn't... is my syntax wrong somewhere?

Upvotes: 0

Views: 355

Answers (2)

timclutton
timclutton

Reputation: 13004

Your form is submitting as a GET request, not POST.

Change this line:

<form action="announce.php">

to this:

<form action="announce.php" method="post">

Edit: I should really refresh the page before posting. All credit to noobie-php for getting this right in their comment 30 mins before I posted this!

Upvotes: 1

J Prakash
J Prakash

Reputation: 1333

Php :

include_once 'creds.php';
$con=mysqli_connect("$db_hostname","$db_username","$db_password","$db_database");

if (isset($_POST['submit']))
{
    $title = $_POST['title'];
    $announce = $_POST['announce'];

$query = "INSERT INTO announcements (labname, name, author, announce)
        VALUES ($lab, $title, $username, $announce)";

$insert = mysqli_query($con, $query);

mysqli_close($con);

echo "added successfully";}
else
{
echo "something went wrong";
}

i think if (isset($_POST['submit'])) this is the problem.

Upvotes: 0

Related Questions