0

Well you see, I'm not really sure how but I'm guessing I can cut the code below by about 70% of its length if I can turn the "mandatory fields" into an array. Anyone care to help? Thank you so much in advance!

<?php session_start();
if (isset($_POST['submit'])) 
{
if (empty($_POST['cliname'])){
$_SESSION['err'] = "All fields are mandatory!";
}else if (empty($_POST['cliadd'])){
$_SESSION['err'] = "All fields are mandatory!";
}else if (empty($_POST['clino'])){
$_SESSION['err'] = "All fields are mandatory!";
}else if (empty($_POST['cliprod'])){
$_SESSION['err'] = "All fields are mandatory!";
}else if (empty($_POST['clitest'])){
$_SESSION['err'] = "All fields are mandatory!";
}else if (empty($_POST['clidr'])){
$_SESSION['err'] = "All fields are mandatory!";
}else {click_submit();}
}
$err = $_SESSION['err'];
?>
2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by AdventDeo
0
<?php 
session_start();
if (isset($_POST['submit'])) 
{
  $fields = array ('cliname', 'cliadd', 'clino', 'cliprod', 'clitest', 'clidr');
  $empty_field = false;
  foreach ($fields as $field)
    if (empty($_POST[$field]))
      $empty_field = true;

  if ($empty_field)
    $_SESSION['err'] = 'All fields are mandatory!';
  else
    click_submit();
}
$err = $_SESSION['err'];
?>

//OR

<?php 
session_start();
if (isset($_POST['submit'])) 
{
  if (empty($_POST['cliname']) or empty($_POST['cliadd']) or empty($_POST['clino']) or
      empty($_POST['cliprod']) or empty($_POST['clitest']) or empty($_POST['clidr'])) 
    $_SESSION['err'] = "All fields are mandatory!";
  else 
    click_submit();
}
$err = $_SESSION['err'];
?>
0

Woot! you're awesome, thank you!
I just realized that putting a space there is counted as an input... where can I use the striplashes thingy?

0
<?php 
session_start();
if (isset($_POST['submit'])) 
{
  $fields = array ('cliname', 'cliadd', 'clino', 'cliprod', 'clitest', 'clidr');
  $empty_field = false;
  foreach ($fields as $field)
    if (empty(trim($_POST[$field]))) // notice the trim
      $empty_field = true;
 
  if ($empty_field)
    $_SESSION['err'] = 'All fields are mandatory!';
  else
    click_submit();
}
$err = $_SESSION['err'];
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.