Question

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?

Was it helpful?

Solution

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
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top