1

When users submit a form leaving some of it's fields empty, to populate the associated empty fields of database with predefined data the following codes first check if the users are leaving the form's fields empty and then insert the predefined data in to the respective fields issuing_date reference_details and name of database through hidden form inputs.

if ((isset($_POST["submit_form"])) && ($_POST["submit_form] == "Submit")) { 

$issue_date = $_POST['DEFAULT CURRENT_TIMESTAMP'];

$reference = "Not Available";
if(isset($_POST['reference_details']) && !empty($_POST['reference_details'])){  
$reference = $_POST['reference_details'];
}

$drawer_name = "Not Available";
if(isset($_POST['name']) && !empty($_POST['name'])){  
$drawer_name = $_POST['name'];
}

$insertSQL = sprintf("INSERT INTO table (issuing_date, reference_details, name) VALUES (%s, %s, %s)",

GetSQLValueString(trim($issue_date), "date"),
GetSQLValueString(trim($reference), "text"),
GetSQLValueString(trim($drawer_name), "text"));

and do more.......

<input type="submit" name="submit_form" id="submit" value="Submit" />
<input type="hidden" name="issuing_date" value="<?php echo "$issue_date"; ?>" />---->Line110
<input type="hidden" name="reference_details" value="<?php echo "$reference"; ?>" />---->Line111
<input type="hidden" name="name" value="<?php echo "$drawer_name"; ?>" />---->Line112

When error_log is active, the form page produces the following example of notice in the error_log whenever it is launched in the web browser.

PHP Notice:  Undefined variable: issuing_date in /home/user/public_html/dir/subdir/test.php on line 110
PHP Notice:  Undefined variable: reference_details in /home/user/public_html/dir/subdir/test.php on line 111
PHP Notice:  Undefined variable: name in /home/user/public_html/dir/subdir/test.php on line 112

What's going wrong here? Are the hidden inputs of the form defined incorrectly?

Any idea?

13
  • Which lines are 110-112? EDIT: My mistake I see it now. Commented Apr 30, 2014 at 13:56
  • Is $issue_date actually being set before trying to use it in the hidden inputs? Commented Apr 30, 2014 at 13:57
  • @PrimitiveType I think I screwed up, OP mentioned errors in their form so it's probably the hidden fields... Commented Apr 30, 2014 at 13:59
  • @SilentPond You should probably provide more code because these snippets only cause speculation as to where your are defining and using these variables... Commented Apr 30, 2014 at 14:01
  • 1
    so the error is on the form page with out a "submit"? This means that none of the code within the first code block above is executed based on the "if" statement ---> Add '$issue_date =""; $reference_details=""; $name=""; ' at the top of your test.php file and see if the problem goes away. Commented Apr 30, 2014 at 16:06

2 Answers 2

2

Upon initial loading of your form you have not posted any data to it so it never enters this code block:

1st load - visiting the page

if ((isset($_POST["submit_form"])) && ($_POST["submit_form"] == "Submit")) { // Data received from your submit button is not available because the form was not submitted

    // We never make it here so $issue_date is not available when you need it

    $issue_date = $_POST['DEFAULT CURRENT_TIMESTAMP'];

    // everything else

}

2nd load - submitting the form to itself

if ((isset($_POST["submit_form"])) && ($_POST["submit_form"] == "Submit")) { // Data received from your submit button is available so we enter this block of code

    // We made it here so $issue_date is available later on

    $issue_date = $_POST['DEFAULT CURRENT_TIMESTAMP'];

    // everything else

}

You have three choices:

1 - turn off error reporting

^ This is the easiest solution and should always be done in a Production environment or a public-facing website

At the very beginning of your file do this:

error_reporting(0);

2 - use isset() to figure out if the variable has been declared

<input type="submit" name="submit_form" id="submit" value="Submit" />
<input type="hidden" name="issuing_date" value="<?php echo (isset($issue_date) ? $issue_date : ''); ?>" />
<input type="hidden" name="reference_details" value="<?php echo (isset($reference) ? $reference: ''); ?>" />
<input type="hidden" name="name" value="<?php echo (isset($drawer_name) ? $drawer_name: ''); ?>" />

3 - declare your variables before the if(){} block

$issue_date = NULL;
$reference = NULL;
$drawer_name = NULL;

if ((isset($_POST["submit_form"])) && ($_POST["submit_form"] == "Submit")) {

    $issue_date = $_POST['DEFAULT CURRENT_TIMESTAMP'];

    // everything else
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. The form works well when error reporting is off but I was trying to figure out the root of a problem that prevents the cleanliness of the code or keeps the code away from being clean. I actually need the issue date on 2nd load. However, your answer is something I expected.
your 2nd solution is something I expected.
0

did you set the variable before?

$variable = $_POST['NAME VALUE'];

for example if the column in the DB is test

you have to set

$variablename = $_POST['test'];

of course you even have to run the query

1 Comment

if you read my question carefully, you may realize that, defining a post variable isn't related to my problem. I receive this notice whenever I launch the page in the browser before any form action. It's really very strange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.