Yevgeniy Bagackiy Yevgeniy Bagackiy - 1 month ago 6
HTML Question

How to check if droppable area is empty during submitting form

I have form with droppable area:

<form>
<span id="error">
<!----Initializing Session for errors--->
<?php
if (!empty($_SESSION['error1'])) {
echo $_SESSION['error1'];
unset($_SESSION['error1']);
}
?>
</span>
<div class="projLeader">
<ol>
<li class="placeholder" name="projLeader" <?php if (isset($projLeader)) echo 'value="'.$projLeader.'"' ?>><div class="adding">Drop here</div></li>
<input type="hidden" name="projLeader" class="hiddenListInput1" required/>
</ol>
<input type = "submit" name ="submit" class="button" value = "Create Report" onclick="userSubmitted = true;"/>
</div>
</form>


Where placeholder "Drop here" will be removed once item is dropped. And I am saving dropped data by this js function:

var LISTOBJ1 = {
saveList: function() {
var listCSV = "";
$( ".projLeader li" ).each(function() {
if (listCSV === "") {
listCSV = $(this).text();
} else {
listCSV += "," + $(this).text();
}
$(".hiddenListInput1").val(listCSV);
});
}
}


So my question is how i can make validation and not allow user to submit empty field, or field with "Drop here" placeholder, because if user adds nothing and just presses submit i got it placeholder to print.
I tried to check if it is empty by posting data and checking if it is empty:

$projLeader=$_POST['procLeader'];
if (empty($projLeader))
{
//setting error message
$_SESSION['error1'] = "Mandatory field(s) are missing, Please fill it again";
header("location: createProject2.php"); //redirecting to first page
}


So what is the way to make a validation? Thanks

Answer

When no item has dropped in droppable area then your hidden field will have "Drop here" value. So your condition should check for either hidden field have no value or hidden field have "Drop here" value and it should consider blank.

Note: Your spell was also wrong for getting value of hidden field. It must be projLeader not procLeader.

Please try with below modified PHP code.

$projLeader=trim($_POST['projLeader']);
if (empty($projLeader) || $projLeader == "Drop here")
{
    //setting error message
    $_SESSION['error1'] = "Mandatory field(s) are missing, Please fill it again";
    header("location: createProject2.php"); //redirecting to first page         
}

And must have to place sesstion_start(); function on both the pages to use $_SESSION.