I am using bootstrap and I want to create a contact Form. I have create a File with the name testForm.php and there I have written my php and my html code(should I make a different files? one for php and one for html?). My file starts with the php code to and after the html code. As soon as I put an <?php echo....
in the html area, everywhere, appears all the time in the site Undefined index: .for example
Now I m trying only with one parameter :thema to see if it works and how it works and if I put something to value comes the result like the picture above. My php code:
<?php
if (isset($_POST["submit"])) {
$thema = $_POST['thema'];
// Check if thema has been entered
if (!$_POST['thema']) {
$errThema = 'Please enter your thema';
}
}
?>
and my html code:
<!-- MAIN SEITE -->
<h4><p class="text-primary"><u>Neuanforderungsformular</u></p></h4>
<!-- START FORM -->
<form method="post" role="form" action="testForm.php">
<!-- Thema Feld -->
<div class="form-group">
<label for="usr">Thema / Title:</label>
<input type="text" id="thema" name="thema" class="form-control input-lg" value="<?php echo htmlspecialchars($_POST['thema']); ?>">
</div>
<!-- Email Adresse Feld-->
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<div class="input-group col-sm-5">
<input type="text" class="form-control input-lg" placeholder="Ihre Email Addresse" aria-describedby="basic-addon2">
<span class="input-group-addon" id="basic-addon2">@teswt.de</span>
</div>
how can I fix the provbem so my contact form will works?
Try this:
<?php
if (isset($_POST["submit"])) {
if (isset($_POST['thema'])){
$thema = $_POST['thema'];
} else {
$thema = 'Please enter your thema';
}
}
?>
You should check first if $thema
has been set. What you did is you are using $_POST['thema']
without checking, thus the error appearing in the text field
You are accessing to $_POST['thema']) also when an entry for thema is not present in $_POST
<div class="form-group">
<label for="usr">Thema / Title:</label>
<input type="text" id="thema" name="thema" class="form-control input-lg"
value="<?php echo htmlspecialchars($_POST['thema']); ?>">
</div>
then you should use a proper setting for a var eg $myThema
if (isset($_POST["submit"])) {
if (isset($_POST['thema'])){
$myThema = $_POST['thema'];
} else {
$errThema = 'Please enter your thema';
$myThema = '';
}
}
.
<div class="form-group">
<label for="usr">Thema / Title:</label>
<input type="text" id="thema" name="thema" class="form-control input-lg"
value="<?php echo htmlspecialchars($myThema); ?>">
</div>
I would use something like that:
function GetPostOrDefault($key, $default = '')
{
if (array_key_exists($key, $_POST))
{
return $_POST[$key];
}
return $default;
}
And then
<input value="<?= GetPostOrDefault('thema') ?>">
or
<input value="<?= GetPostOrDefault('thema', 'Neues Thema') ?>">
You get this error because the index is not defined, if you have made no post.
That means $_POST['thema']
is only available if you have submitted a form that contains a field with the name thema
. On your initial page load, you do a GET request. The form is not submitted.
use like this
$thema = isset($_POST['thema']) ? $_POST['thema'] : '';
instead of
$thema = $_POST['thema'];
update.1
try it
<?php $thema = !empty($thema) ? $thema : ''; ?>
<input type="text" id="thema" name="thema" class="form-control input-lg" value="<?php echo htmlspecialchars($thema); ?>">
instead of
<input type="text" id="thema" name="thema" class="form-control input-lg" value="<?php echo htmlspecialchars($_POST['thema']); ?>">
Simply move your default $thema value outside of the check for $_POST['submit']. Your current code only sets it when the form has been submitted.
$thema = '';
$errThema = '';
if (isset($_POST["submit"])) {
// Check if thema has been entered
if (!$_POST['thema']) {
$errThema = 'Please enter your thema';
} else {
$thema = $_POST['thema'];
}
}
Of course, you should then display $thema
instead of $_POST['thema']
in your form.