简体   繁体   中英

Got Error Message from Xampp “Notice : Undefined index” HTML

Notice: Undefined index: tombol in C:\xampp\htdocs\pw-liana\submitcomment.php on line 2
Notice: Undefined index: nama in C:\xampp\htdocs\pw-liana\submitcomment.php on line 25

And with the same notice for nama (line 25), email(line 25), website(line 25), komentar(line 25), art_id(line 26), art_url(line 26). But the last Notice is

Notice: Undefined variable: art_url in C:\xampp\htdocs\pw-liana\submitcomment.php on line 31

My php code

<?php
if ($_POST["tombol"]=="Kirim")
{
$nama=$_POST["nama"];
$email=$_POST["email"];
$website=$_POST["website"];
$komentar=$_POST["komentar"];
$art_id=$_POST["art_id"];
$art_url=$_POST["art_url"];
if(empty($nama))
$_POST["nama"]='anonymous';
if(empty($komentar)){
echo "<meta http-equiv='refresh' content='2; url=$art_url'>";
die("komentar harus diisi");}
}
//connect database
$con=mysql_connect("localhost", "root", "");
if(!$con)
die("Tidak dapat melakukan koneksi ke server MySQL");
//Menampilkan data
mysql_select_db("db_web", $con);
$sql="INSERT INTO comment (nama, email, website, komentar, art_id, art_url,
date)
VALUES
('$_POST[nama]','$_POST[email]','$_POST[website]', '$_POST[komentar]',
'$_POST[art_id]', '$_POST[art_url]', NOW())";
if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
echo "<meta http-equiv='refresh' content='0; url=$art_url'>";
//Memutuskan koneksi
mysql_close($con);
?>

And My html code

 <div id="publishcomment"> <?php include("publishcomment.php"); getcomment("1"); $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?></div> <div> <form name="submitcomment" method="post" action="submitcomment.php">Nama:<br> <input name="nama" type="text"><br> Email(optional):<br><input name="email" type="email"><br> Website(optional):<br><input name="website" type="text"><br> Komentar:<br><textarea name="komentar" rows="6" cols="50"></textarea><br> <input name="art_id" value="1" type="hidden"> <input name="art_url" value="" type="hidden"><br> <input name="tombol" value="Kirim" type="submit"> <input value="Reset" type="Reset"> </form></div> </article> </div> 

I got the error when i click the submit button

The problem is, that the "Global" array $_POST has no index of tombol. In the first step you should check if the index is set. Then check if the value is the value you have expected.

Also I would use mysqli instead of mysql. MySQL (without i) is deprecated.

I have updated your code

EDIT: Code is not tested!

Also I would try to get everything done in PHP without having a redirect all the time. If it is not possible use the PHP function header('Location: URL'); instead

EDIT2 : Here is an updated code. Try this one. Still untested, just Dreamweaver checked for syntax error.

<?php
if (isset($_POST["tombol"]))
{
    if($_POST["tombol"] == "Kirim")
    {

        $nama =     (isset($_POST["nama"]))     ? $_POST["nama"]        : 'anonymous';
        $email =    (isset($_POST["email"]))    ? $_POST["email"]       : '' ;
        $website =  (isset($_POST["website"]))  ? $_POST["website"]     : '' ;
        $komentar = (isset($_POST["komentar"])) ? $_POST["komentar"]    : '' ;
        $art_id =   (isset($_POST["art_id"]))   ? $_POST["art_id"]      : '' ;
        $art_url =  (isset($_POST["art_url"]))  ? $_POST["art_url"]     : '' ;

        if(empty($komentar))
        {
            echo '<div class="error">Comment is empty</div>';
        }
        else
        {
            //connect database
            $con = mysqli_connect("localhost", "root", "", "db_web");
            if(!$con)
            {
                echo '<div class="error">SQL connection Error</div>';   
            }
            else
            {
                //Menampilkan data
                $sql = 'INSERT INTO comment (nama, email, website, komentar, art_id, art_url, date)
                VALUES
                ("'.mysqli_real_escape_string($con,$nama).'","'.mysqli_real_escape_string($con,$email).'","'.mysqli_real_escape_string($con,$website).'", "'.mysqli_real_escape_string($con,$komentar).'", '.$art_id.', "'.mysqli_real_escape_string($con,$art_url).'", NOW())';
                if (!mysqli_query($con,$sql))
                {
                  echo '<div class="error">Error: ' . mysqli_error($con).'</div>';
                }

                //Memutuskan koneksi
                mysqli_close($con);
            }
        }
    }
    else
    {
        echo '<div class="error">The comment is not "Kirim"</div>'; 
    }
}
?>
<div id="publishcomment">
    <?php include("publishcomment.php"); getcomment("1");
    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    ?>
</div> 
<div>
    <form name="submitcomment" method="post" action="<?php echo $actual_link; ?>">Nama:<br>
        <input name="nama" type="text"><br>
        Email(optional):<br><input name="email" type="email"><br>
        Website(optional):<br><input name="website" type="text"><br>
        Komentar:<br><textarea name="komentar" rows="6" cols="50"></textarea><br>
        <input name="art_id" value="1" type="hidden">
        <input name="art_url" value="" type="hidden"><br>
        <input name="tombol" value="Kirim" type="submit">
        <input value="Reset" type="Reset">
    </form>
</div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM