0

I'm getting undefined index errors on the $_POST variables. I.E. $_POST['hostname']. I know kinda why I'm getting them, but is there a way to keep the php and form in the same file and not get these errors?

<html>
<head>
    <title>Install Forum</title>
</head>
<body>
<h1>Please enter your database information</h1>

<form action='install.php' method='post'>
Hostname: <input type='text' name='hostname'><br/>
MySQL User: <input type='text' name='dbuser'><br/>
MySQL Pass: <input type='password' name='dbpassword'><br/>
<input type='submit' name='submit' value='Submit'><br/>
</body>
</html>
<?php
include_once("config.php");
$date = date('Y-m-d');

//Database Variables
$dbhost = strip_tags($_POST['hostname']);
$dbuser = strip_tags($_POST['dbuser']);
$dbpass = strip_tags($_POST['dbpassword']);
$submit = isset($_POST['submit']);
1

1 Answer 1

4

Wrap your PHP code in an if with an isset condition. This way, the code will only be run if the form has been submitted:

if(isset($_POST['submit']))
{
    include_once("config.php");
    $date = date('Y-m-d');

    //Database Variables
    $dbhost = strip_tags($_POST['hostname']);
    $dbuser = strip_tags($_POST['dbuser']);
    $dbpass = strip_tags($_POST['dbpassword']);
    $submit = isset($_POST['submit']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

ahh I see, thanks. I was trying to put an isset on each individual variable. Like isset(strip_tags($_POST['hostname']));
Try $dbhost = isset($_POST['hostname']) ? strip_tags($_POST['hostname']) : FALSE; or if (isset($_POST['hostname'])) {$dbhost = strip_tags($_POST['hostname']); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.