I have a web page that contains a few forms. Form1 lets the user enter text into a few input boxes. Once the submit button is clicked, the info is displayed in a textarea in form2. The info is displayed in the following format:

Inputbox1|| Inputbox2|| Inputbox3|| Inputbox4|| Inputbox5

The user can then manually add or edit the info (must be in the format above) and click on the submit button in form2 which will save the info to a flat file. Each time the user accesses this page, the info will be displayed in form2.

The problem I am running into is a Notice: Undefined variable: myAray and Undefined array: myAray error. This happens if a user skips form1 or deletes all of the data in the textarea in form2 and then clicks the submit button in form2 to save/create the empty flat file. It is because the array and/or variable is empty. Other than that, the page works fine with no errors.

Here is the code that causes the errors:

foreach ($myAray as $ary) { 
    $crSrg .= "array(\"" . $ary[0] . "\", \"" . $ary[1] . "\", \"" . $ary[2] . "\", \"" . $ary[3] . "\", \"" . $ary[4] . "\", \"" . $ary[5] . "\"), ";
}

If I change it to the following, the Undefined array: myAray error goes away, but the Undefined variable: myAray error remains:

if(is_array( $myAray)){
foreach ($myAray as $ary) { 
    $crSrg .= "array(\"" . $ary[0] . "\", \"" . $ary[1] . "\", \"" . $ary[2] . "\", \"" . $ary[3] . "\", \"" . $ary[4] . "\", \"" . $ary[5] . "\"), ";
}
}

If I change it to the following, I get no errors, but the text that a user adds to the textarea in form2 is not saved when the submit button is clicked. The text area box just goes blank after the submit button is clicked:

if( isset( $_POST['myAray'] ))
$cnAry = $_POST['myAray'];
else
$myAray = '';
if(is_array( $myAray)){
foreach ($myAray as $ary) { 
    $crSrg .= "array(\"" . $ary[0] . "\", \"" . $ary[1] . "\", \"" . $ary[2] . "\", \"" . $ary[3] . "\", \"" . $ary[4] . "\", \"" . $ary[5] . "\"), ";
}
}

I guess what I need to do, is to print or echo an error message, instead of having the blank textarea being saved, when a user tries to submit form2 with a blank textarea.

I'm sure it's a simple fix, but I can't figure it out.

Any ideas?

    It sounds like what you want to do would be better done with Javascript or by splitting your forms into two different pages. Javascript would be better if you just want to populate Form2 based on input from Form1. A second page would be better if you need to use the input from Form1 to retrieve data from your flat file to populate Form2.

      ixalmida;10892493 wrote:

      It sounds like what you want to do would be better done with Javascript or by splitting your forms into two different pages. Javascript would be better if you just want to populate Form2 based on input from Form1. A second page would be better if you need to use the input from Form1 to retrieve data from your flat file to populate Form2.

      Thanks for the input🙂

      I do have a layer of JavaScript which fixes the problem, but I want to make sure no PHP errors are thrown if a user has JavaScript disabled.

        Using PHP to generate Javascript shouldn't throw any PHP errors. Using Javascript to generate PHP could cause you problems though. Imagine a user having Javascript disabled so PHP can't assign a variable, versus Javascript throwing an error if it doesn't assign a variable. Since Javascript errors only put up an alert in a browser status bar (unless the user has debugging turned on), it isn't nearly as disruptive.

          ixalmida;10892538 wrote:

          Using PHP to generate Javascript shouldn't throw any PHP errors.

          I guess my reply was not very clear. When testing the page with a JavaScript enabled browser, everything is fine because of the JavaScript I added to the page. The PHP errors happen when the page is accessed with a browser that does not support JavaScript.

          The JavaScript throws a client side warning if the textarea box is empty when submitting. I want to at least get rid of the PHP notices for those with no JavaScript support. Preferably add a PHP sever side warning about the box being empty. Basically do the same thing that the JavaScript does but I want it to be sever side. Hope that clearer🙂

            Write a Reply...