1

I have a php page that I'm trying to simplify and am running into some issues that I can't get through alone. My form takes user data, posts to itself, validates that fields have been filled in, and then displays form contents/posts to a mysql database.

The issue I am having is that instead of having 20 if()/elseif statements I wanted to load the variable names into an array, loop through that array, and if a variable wasn't populated in the form have it produce an error message. Unfortunately my code will display the error message regardless of whether the field has a value in it or not.

As an additional note, I can add the $ShippingCo to my form and echo it but the notice that it isn't completed still shows up.

Also, if the script enters the if statement I'd like for it to stop executing the remainder of the page after the closing I've tried exit; without success.

Here is what I have:

<?php

$ShippingCo = $_POST['ShippingCo'];
$ShipAcct = $_POST['ShipAcct'];
$ShipService = $_POST['ShipService'];
$FOB = $_POST['FOB'];
$Terms = $_POST['Terms'];

$ENote[] = '$Terms';
$ENote[] = '$FOB';
$ENote[] = '$ShippingCo';
$ENote[] = '$ShipAcct';
$ENote[] = '$ShipService';

$Emessg[] = 'Shipping Terms';
$Emessg[] = 'FOB Method';
$Emessg[] = 'Shipping Company';
$Emessg[] = 'Shipping Account';
$Emessg[] = 'Shipping Service Type';


foreach ($ENote as $a => $b) {

if(!$$ENote[$a]){       //I intentionally put the '$$' in this line otherwise none of the messages show. . . with data in the variables or not.

$error = "Error!  Please Add the $Emessg[$a]!";
?>
<table width="800" align="center">
<tr>
<td align="center">

<h2>Sales Order Entry Form</h2>
</td>
</tr>
<tr>
<td align="center">
<h3>
<font color="red">
<?php
echo "$error";
?>
</font>
</h3>
</td>
</tr>
<tr>
<td align="center">
Please press back to properly complete the form</td>
</tr>
</table>

<?php
}
}
?>

Thank you in advance.

1
  • Asker knows that, and is in fact leveraging it. Commented Jul 13, 2012 at 23:37

3 Answers 3

1

I suspect that the syntax $$ENote[$a] is probably being interpreted as ($$ENote)[$a] instead of $($ENote[$a]) (parenthesis are not legal syntax, just for demo).

So I suggest an intermediate variable, or else complex syntax:

foreach ($ENote as $a => $b) {
  $varname = $ENote[$a];
  if(!$$varname){

or (not tested, just a hunch that it might work):

foreach ($ENote as $a => $b) {
  if(!${$ENote[$a]}){
Sign up to request clarification or add additional context in comments.

Comments

0

There are several issues at play here.

First, each $ENote entry must omit the leading $. Variable variables must not include the $ part.

$ENote[] = 'Terms';
$ENote[] = 'FOB';
...

Second, as ctrahey said, you need to store the variable name into a variable in order to expand it with another $.

Here are some code cleanup tips:

  • Combine the two arrays into one (a map).
  • Assign to the named variables from $_POST within the loop.
  • Use break to exit the loop.

This will remove much of the redundancy.

$ENote = array(
    'Terms' => 'Shipping Terms',
    'FOB' => 'FOB Method',
    ...
);

foreach ($ENote as $field => $title) {
    $value = $$field = $_POST[$field];
    if (!$value) {
        ... display error using $title ...
        break;  // exit loop
    }
}

Comments

0

Why don't you just create an array or all the fields and pass that array onto a function, and in that function, loop through each item and if empty throw out an error?

Something like this:

function checkEmpty($some_array) {
    foreach($some_array as $key=>$value) {
      if($value=="") {
         or if(empty($key)) {   ///throw error }
         //put error message and exit

       }

    }
  }

Comments

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.