John Davidson

html - php condition : first test of my inputs array ignored

0 comments
Message:


I want to check the inputs of my inputs with conditions (if, else..) nothing very complicated so far. But the problem is that the first condition is never taken into account.


Here is the code of my form:


 <form method="POST" action="">
<table>
<caption>author :</caption>
<?php
for($i=1; $i <= $author; $i++){?>
<tr>
<td align="right">
<label for="<?= 'author'.$i ?>"> <?='author '.$i ?> :</label>
</td>
<td>
<input type="text" placeholder=" nom du author" id=" <?= 'author'.$i ?>" name="authors[]">
</td>
</tr>
<?php
}?>
</table>
<table>
<caption>title :</caption>
<?php
for($i=1; $i <= $title; $i++){?>
<tr>
<td align="right">
<label for="<?= 'title'.$i ?>"> <?= 'title '.$i ?> :</label>
</td>
<td>
<input type="text" placeholder=" title" id="<?= 'title'.$i ?>" name="titles[]">
</td>
</tr>
<?php
}?>
</table>
<table>
<caption>article :</caption>
<?php
for($i=1; $i <= $article; $i++){?>
<tr>
<td align="right">
<label for="<?= 'article'.$i ?>"> <?= 'article '.$i ?> :</label>
</td>
<td>
<input type="text" placeholder="ex: score possible(min:0)" id="<?= 'article'.$i ?>" name="articles[]">
</td>
</tr>
<tr>
<td>
<input type="hidden" name="article" value="0">
</td>
<td align="right">
<br />
<input type="submit" name="btnSusbmit" class="modal-btn modal-eventTrigger modal-reportEventTrigger" value="Next">
</td>
</tr>
</table>
</form>

and here is my php code :


if(isset($_POST['btnSusbmit']))
{
if(!empty($_POST['authors']))
{
$_SESSION['authors'] = array_unique($_POST['authors']);
if(!empty($_POST['titles']))
{
$_SESSION['titles'] = array_unique($_POST['titles']);
if(!empty($_POST['articles']) and !empty($articles) and $articles != 0)
{
$_SESSION['articles'] = array_unique($_POST['articles']);
}
elseif(!empty($articles) and $articles != 0 and empty($_POST['articles']))
{
$error = 'all fields "articles" must be filled in.';
}
}
else
{
$error = 'all fields "titles" must be filled in.';
}
}
else
{
$error = 'all fields "authors" must be filled in';
}
}

I transmuted the conditions to test and no matter the condition it is always the same problem.


Edit :


var_dump($POST] = array(5) { ["authors"]=> array(1) { [0]=> string(0) "" } ["titles"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["articles"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["article"]=> string(1) "0" ["btnSubmit"]=> string(7) "Next" }


My Answer:
html









php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$age = $_POST["age"];

if ($name == "") {
echo "Name is required";
} elseif ($age == "") {
echo "Age is required";
} else {
echo "Form submitted successfully";
}
}
?>


In the above code, the first condition checks if the name input is empty. If it is empty, it should display "Name is required". If the name input is not empty, it should move on to the next condition to check if the age input is empty. If the age input is empty, it should display "Age is required". If both inputs are filled, it should display "Form submitted successfully".

If the first condition is not being taken into account, it could be due to the way the form is being submitted or how the inputs are being processed in the PHP code. Make sure that the form method is set to "post" and that the PHP code is correctly accessing the input values from the $_POST array.

Rate this post

3 of 5 based on 3945 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us