Member Avatar for LastMitch

Hi,

It's really late now,

I am trying to figure out how to echo a message when I submit a form.

Here is the code:

<?php 
$msg = "";
if ($_POST['title'] != ""){
    $title = $_POST['title'];
    $author = $_POST['author']; 
    $author = stripslashes($author);

    if (!$title){
        $msg = "Please Add Title";
    } else if (!$author){
        $msg = "Please Add Author";
    }
    $query = mysql_query("INSERT INTO time (title, contents) VALUES ('$title', '$author')") or die (mysql_error());
    $msg = "Submitted";
}
?> 

Here is my Form:

<form action="addinfo.php" method="post">
Title:<br />
<input type="text" name"title" /><br /><br />
Author:<br />
<textarea name="author"></textarea>
<br /><br />
<input type="submit" name="submit" value="Submit" /><?php echo $msg; ?>

</form>

The issue is that the when I click Submit it should echo "Submitted" but it doesn't any suggestions?

I really appreciate if someone explain to me how fixed this minor issue. Thanks!

Can you confirm whether the PHP code appears immediately before the HTML code in the same file, addinfo.php?

Member Avatar for LastMitch

@blocblue

Thanks for the reply!

The code that I wrote above is the addinfo.php.

The file is addinfo.php

   <?php
    $msg = "";
    if ($_POST['title'] != ""){
    $title = $_POST['title'];
    $author = $_POST['author'];
    $author = stripslashes($author);
    if (!$title){
    $msg = "Please Add Title";
    } else if (!$author){
    $msg = "Please Add Author";
    }
    $query = mysql_query("INSERT INTO time (title, contents) VALUES ('$title', '$author')") or die (mysql_error());
    $msg = "Submitted";
    }
    ?> 

-

    <form action="addinfo.php" method="post">
    Title:<br />
    <input type="text" name"title" /><br /><br />
    Author:<br />
    <textarea name="author"></textarea>
    <br /><br />
    <input type="submit" name="submit" value="Submit" /><?php echo $msg; ?>
    </form>

Sorry for the confusing. I put it altogether into 1 file to see how it works. I create a table on the server and I want to submit it to the database. So that's why I want it echo $msg to let me know it's Submitted

Okay, looks like you need a few tweaks.

  1. Line #2 - don't test for the presence of $_POST['title'] as this will generate a notice because the index will not exist when the form hasn't been submitted.

  2. Lines #7 - #13 - Your query and "submitted" message need to be in an else block, otherwise any message set due to the title or author fields being empty will always be overwritten come line #13.

  3. Line #3 (form) - you're missing an equals sign - name"title". This is causing the value of the name attribute to not be recognised.

Please see revised code below:

$msg = "";

if ($_POST){
    $title = $_POST['title'];
    $author = $_POST['author'];
    $author = stripslashes($author);

    if (!$title){
        $msg = "Please Add Title";
    } else if (!$author){
        $msg = "Please Add Author";
    } else {
        $query = mysql_query("INSERT INTO time (title, contents) VALUES ('$title', '$author')") or die (mysql_error());
        $msg = "Submitted";
    }
}
?> 
<form action="addinfo.php" method="post">
Title:<br />
<input type="text" name="title" /><br /><br />
Author:<br />
<textarea name="author"></textarea>
<br /><br />
<input type="submit" name="submit" value="Submit" /><?php echo $msg; ?>
</form>
commented: Thanks for the explanation & correction! +2
Member Avatar for LastMitch

@blocblue

Thanks for the explanation. I will test it now

Member Avatar for LastMitch

@blocblue

It work! Thanks for the explanation!

So you took this out:

['title'] != ""

and just $_POST by itself

 if ($_POST){

it makes sense now.

I didn't see any error on Dreamweaver on the form. It's usually turn red if there's a error, so in the form it was all yellow. I assumed it must be in php script (there was no red marks either). I can't believe I miss the equal sign!

It worked and the $msg appeared once I click submited and the data appeared in the database. I appreciate again for taking your time to explain and correct my errors in the code and answer my question. Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.