html - i've a problem with my form that i tried validating it with php then it should send the data to the firebase
Get the solution ↓↓↓it always gives me the first condition which gives me "Name Cannot be empty." and it dosen't send the data...
i tried changing the $_POST['inputs'] with variables but everytime it gives me undefined index
inside the script there's the code that sends the data to firebase
what seems to be the problem here
<form class="pb-5 ml-5" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<input type="text" class="form-control text-right" id="inputName" name="inputName">
<label for="staticEmail" class="col-sm-3 col-form-label text-left">Ш§Щ„ШҐШіЩ…</label>
<input type="email" class="form-control text-right" id="inputEmail" name="inputEmail">
<label for="staticEmail" class="col-sm-3 col-form-label text-left" >Ш§Щ„ШҐЩЉЩ…ЩЉЩ„</label>
<input type="password" class="form-control text-right" id="inputPassword" name="inputPassword">
<label for="staticPassword" class="col-sm-3 col-form-label text-left">ЩѓЩ„Щ…Ш© Ш§Щ„Щ…Ш±Щ€Ш±</label>
<input type="tel" id="inputNum" class="form-control text-right" name="inputNum">
<label for="staticNum" class="col-sm-3 col-form-label text-left" >Ш±Щ‚Щ… Ш§Щ„Щ…Щ€ШЁШ§ЩЉЩ„</label>
<input type="hidden" name="form_submitted" value="1" />
<input type="button" value="Ш§ШґШЄШ±Щѓ" class="btn btn-danger w-50 mr-5" id="create-newuser-button" name="createUser">
</form>
</section>
</div>
<?php
$nameEmptyErr = $emailEmptyErr = $mobNumEmptyErr = $passwordEmptyErr = "";
$nameErr = $emailErr = $mobNumErr = $passwordErr = "";
$validation = true;
//Name Validation
if (empty($_POST['inputName'])) {
$nameEmptyErr = '<div class="error">
Name cannot be empty.
</div>';
echo $nameEmptyErr;
} else {
$name = test_input($_POST['inputName']);
//Email Validation
if (empty($_POST['inputEmail'])) {
$emailEmptyErr = '<div class="error">
Email cannot be empty.
</div>';
echo $emailEmptyErr;
} else {
$email = test_input($_POST['inputEmail']);
//Password Validation
if (empty($_POST['inputPassword'])) {
$passwordEmptyErr = '<div class="error">
Password cannot be empty.
</div>';
echo $passwordEmptyErr;
} else {
$password = test_input($_POST['inputPassword']);
//Mobile Number Validation
if (empty($_POST['inputNum'])) {
$mobNumEmptyErr = '<div class="error">
Mobile Number cannot be empty.
</div>';
echo $mobNumEmptyErr;
} else {
$mobNum = test_input($_POST['inputNum']);
$validation = true;
}
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['createUser']) && $validation = true) :
?>
<script type='text/javascript'></script>
<?php
endif;
?>
Answer
Solution:
Your page isn't testing to see if it has been submitted. As a result it displays your form and immediately starts validating the parameters - which aren't yet there.
You also don't seem to have asubmit
button, so there's no way to submit the form. If you have such a button you can test for it and do the validation if you find it.
Rework your page like this:
// Add a test for the presence of the submit button.
//If not found, display a form, otherwise do the validation
if (!isset($_POST['createUser'])) {
?>
<div>
<section>
<!-- No need to specify an action if the form is submitting to the same page. -->
<form class="pb-5 ml-5" method="POST">
<input type="text" class="form-control text-right" id="inputName" name="inputName">
<label for="staticEmail" class="col-sm-3 col-form-label text-left">Ш§Щ„ШҐШіЩ…</label>
<input type="email" class="form-control text-right" id="inputEmail" name="inputEmail">
<label for="staticEmail" class="col-sm-3 col-form-label text-left" >Ш§Щ„ШҐЩЉЩ…ЩЉЩ„</label>
<input type="password" class="form-control text-right" id="inputPassword" name="inputPassword">
<label for="staticPassword" class="col-sm-3 col-form-label text-left">ЩѓЩ„Щ…Ш© Ш§Щ„Щ…Ш±Щ€Ш±</label>
<input type="tel" id="inputNum" class="form-control text-right" name="inputNum">
<label for="staticNum" class="col-sm-3 col-form-label text-left" >Ш±Щ‚Щ… Ш§Щ„Щ…Щ€ШЁШ§ЩЉЩ„</label>
<input type="hidden" name="form_submitted" value="1" />
<!-- Change this element to type="submit" -->
<input type="submit" value="Ш§ШґШЄШ±Щѓ" class="btn btn-danger w-50 mr-5" id="create-newuser-button" name="createUser">
</form>
</section>
</div>
<?php
} else {
// Do your validation here
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: invalid argument supplied for foreach() laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.