Jump to content

[SOLVED] Check for require fields, any suggestion to my coding?


ubuntu

Recommended Posts

Dear eveyone,

 

I'm developing an online reservation system. The code below (dummy code) is to check for required fields (every fields are mandatory enter by client).

 

if (!$_POST['fullname'] || !$_POST['ic_no'] || !$_POST['gender'] || !$_POST['address'] || !$_POST['city'] || !$_POST['state'] || !$_POST['contact_no'] || !$_POST['hp_no'] || !$_POST['email'] || !$_POST['username'] || !$_POST['password'] || !$_POST['confirmpassword']) {
     die("You didn't enter mandatory field. Click <a href=\"./register.php\">Here</a> to reenter your information.");
}

 

I'm a beginner in PHP and not good in programming, hope you can help me to produce a better coding. Any advice or suggestion are most welcome. Thank you for your kind attention. Regards.

Link to comment
Share on other sites

I would suggest seperating them out. Something like this:

 

<?php
$error = NULL;

if (!isset($_POST['fullname'])) {
     $error .= "Full name is required.<br />";
}
if (!isset($_POST['ic_no'])) {
     $error .= "IC_NO is required.<br />";
}
if (!isset($_POST['gender'])) {
     $error .= "Gender is required.<br />";
}

if (!is_null($error)) {
    die($error);
}

// process here
?>

 

This way you can tell your user exactly what is wrong. There is probably a better way to set it up than what I have, but yea it works.

Link to comment
Share on other sites

What are you wanting to do differently?

 

For one, I would suggest not using "die" and instead building a proper error page. In fact, I would repopulate the form with any values the user did enter.

 

Hre is an example - not a full script:

 

<?php




if (isset($_POST[])) { //User posted the page

 $error = false;

 //Check the req. fields
 if (!$_POST['fullname'] || !$_POST['ic_no'] || !$_POST['gender']
      || !$_POST['address'] || !$_POST['city'] || !$_POST['state']
      || !$_POST['contact_no'] || !$_POST['hp_no'] || !$_POST['email']
      || !$_POST['username'] || !$_POST['password'] || !$_POST['confirmpassword']) {
   $error = "You didn't enter all mandatory fields.";
 }

 if (!$error) {

   //Process the form data

 }

}

?>

<html>
<head></head>
<bodY>
FORM TITLE
<br><br>
<?php if ($error) { echo $error; } ?>
<br><br>
Full Name: <input type="text" name="fullname" value="<?=$_POST['fullname']>?"><br>
IC Number: <input type="text" name="ic_no" value="<?=$_POST['ic_no']>?"><br>
Gender: <input type="radio" name="gender" value="male" <?=(($_POST['gender']=='male')?'checked':'')> Male
       <input type="radio" name="gender" value="female" <?=(($_POST['gender']=='female')?'checked':'')> Female<br>
Address: <input type="text" name="address" value="<?=$_POST['address']>?"><br>

</body>
</html>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.