tf.rz
tf.rz

Reputation: 1367

HTML form doesn't collect data if one or more fields are empty

I've created a form for the user to fill out and on submit it is processed and stuff is done with it. Usually just a database query.

With the database that I am working with, some fields can be NULL, so the user could leave something blank in some of the fields. However, when testing this, I tried having one or more fields empty, but the form wouldn't really submit anything at all. When I was debugging, almost all the values of the fields, even the ones with text in it, turn out NULL when I retrieve the values from the POST method.

The thing is, I need to be able to allow the user to submit a form with some fields blank, as they are not completely necessary. How would I go about this?

Do I have to check isset() for everything that could be blank and set it to NULL if it is?

Thanks in advance!

EDIT: Here is the form code.

    <form onsubmit="return confirm('Are you sure you want to save the edits?');" method="post" action="orgedit.php?id=<?php echo $organization_id?>" id="orgform" name="orgform" >
Parent Organization ID: <input type="text" name="parent_id" value="<?php echo $row['parent_id']; ?>"/> <br /><br />
Organization nm: <input type="text" name="org_nm" value="<?php echo $row['org_nm']; ?>"/> <br /><br />
TBR Organization Sysnm: <input type="text" name="tbr_sysnm" value="<?php echo $row['tbr_sysnm']; ?>"/> <br /><br />
Type: <input type="text" name="type" value="<?php echo $row['type']; ?>"/> <br /><br />
Contact nm: <input type="text" name="contact_nm" value="<?php echo $row['contact_nm']; ?>"/> <br /><br />
Financial Info: <input type="text" name="financial_info" value="<?php echo $row['financial_info']; ?>"/> <br /><br />
Other Info: <input type="text" name="other_info" value="<?php echo $row['other_info']; ?>"/> <br /><br />
Active: <input type="text" name="active" value="<?php echo $row['active']; ?>"/> <br /><br />
URL: <input type="text" name="url" value="<?php echo $row['url']; ?>"/> <br /><br />
<input type="submit" value="Save Entry" name="save" />
</form>

and here is the php processing code :)

if(isset($_GET['id']))
    {
    $organization_id = $_GET['id'];
    $parent_organization_id = $_POST['parent_organization_id'];
    $organization_nm = $_POST['organization_nm'];
    $tbr_organization_sysnm = $_POST['tbr_organization_sysnm'];
    $type = $_POST['type'];
    $contact_nm = $_POST['contact_nm'];
    $financial_info = $_POST['financial_info'];
    $other_info = $_POST['other_info'];
    $active = $_POST['active'];
    $url = $_POST['url'];

After I get the values I simply escape them and perform a query.

I have figured out the problem!

if(isset($_GET['id']))
    {
    $organization_id = $_GET['id'];
    $parent_organization_id = $_POST['parent_id'];
    $organization_nm = $_POST['org_nm'];
    $tbr_organization_sysnm = $_POST['tbr_sysnm'];
    $type = $_POST['type'];
    $contact_nm = $_POST['contact_nm'];
    $financial_info = $_POST['financial_info'];
    $other_info = $_POST['other_info'];
    $active = $_POST['active'];
    $url = $_POST['url'];

I was retrieving the wrong values from the form. This could've been fixed by either changing the php code or the html code.

The problem is, the one or more fields empty issue still persists. If one of the fields is blank, the entry is not saved. I've checked the logs and the message it outputted was:

PHP Notice: Undefined index: parent_id
PHP Notice: Undefined index: org_nm
PHP Notice: Undefined index: tbr_sysnm

This happens when I don't write anything for any of those fields and try to save the form.

EDIT 2: The problem was fixed once again. Did a var dump and found that the server wasn't giving out anything when the code was trying to retrieve the values. It was a spelling mistake in a few of the fields.

Upvotes: 2

Views: 1462

Answers (1)

Mihai Stancu
Mihai Stancu

Reputation: 16107

My first guess would be:

Probably your form inputs don't have a name attribute. Input data is only submitted if the input has a name attribute and the PHP $_POST key will be the name of the attribute.

Let's see OP's code to see if my guess is correct.

Upvotes: 2

Related Questions