Jump to content

How can I test $_POST array fields?


amelio

Recommended Posts

Hi,

 

There is probably a very simple answer to this but I'm darned if I know it.

 

If I have a form field coming in to the script as an array...

 

for ($a=1;$a<5;$a++) {
<input type="text" name="<?php echo "var_name[$a]"; ?>" />
}

 

I check the input with...

 

if (isset($_POST['var_name'])) {
      $var_name = $_POST['var_name'];
      function_call ($var_name);
}

 

So I am passing the incoming array to the variable name and then using that variable as a parameter in a function call that only occurs if the form input is set. The trouble is, because text fields come in as empty strings the input will always test positive for being set if someone submits empty form fields. So then I tested it to see if it was empty. Each element of the array is positive for being empty but the array itself is not empty because it is populated with empty strings. So I am in fact using an uninitialized variable in the argument and that is causing an error.

 

I've searched for an answer to this but to no avail. It must be such a common thing to have arrays coming in like this so I would be grateful if someone could cast some light on it for me so that if all of the elements of the array are empty then it fails the test.

 

Much appreciated.

 

Thank You

Link to comment
Share on other sites

$var_name= empty($_POST['var_name']) ? array() : $_POST['var_name'];

 

if its empty it will result in empty array, or you could do this:

 

$somethinghere= "string"; // will output whatevers here if empty
$var_name= empty($_POST['var_name']) ? array($somethinghere) : $_POST['var_name'];

Link to comment
Share on other sites

Thanks Vista86 I appreciate your help but that hasn't solved my problem.

 

I am creating a simple script for myself so that I can put in hourly rates. When I first load the page, no problem, the fields aren't set so the script doesn't try to access the array. The problem occurs when the form is submitted with no fields populated. Shouldn't the incoming array test false for isset?, it doesn't. I understand that form input of type="text" always comes in as a string. So even when the fields are empty my tests show that the array is not empty even though each element of that array is empty.

 

I have stripped the script to the bones to show the problem area. In the real script, if the incoming array is set then I call a function which uses the array. I also have a value attribute on the input element to redisplay the values.

 

 


<?php
if (isset($_POST['hourly_rate'])) {
$hourly_rate = $_POST['hourly_rate'];
echo "\$hourly_rate is set";
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Hourly Rates</p>
<?php 
for ($a=1;$a<=5;$a++) {
	?>
	<input type="text" name="<?php echo "hourly_rate[]"; ?>" /> 
	<?php 
}
?>
<br/><br/>
<input type="submit" value="Send" />
</form>

[code=php:0]

[/code]

 

When I submit the form with empty fields and pass the array to a testing function I get the following output.

 

print_r for Array

Array

(

    [0] =>

    [1] =>

    [2] =>

    [3] =>

    [4] =>

)

 

var_dump for Array

array(5) {

  [0]=>

  string(0) ""

  [1]=>

  string(0) ""

  [2]=>

  string(0) ""

  [3]=>

  string(0) ""

  [4]=>

  string(0) ""

}

Array is not empty

Array is set

 

index [0] is empty

index [0] is set

index [1] is empty

index [1] is set

index [2] is empty

index [2] is set

index [3] is empty

index [3] is set

index [4] is empty

index [4] is set

 

I know that it is correct that each element of the incoming array would test true for empty, as an empty string is empty, and true for isset because a variable with an empty string is still considered to be set. What I can't understand is that it shows the array as a whole is not empty and isset. So neither test will prevent my script from trying to access the array and when it does I get errors.

 

This must be such a common thing, am I missing something really obvious here? I've searched this site and others and cannot find any answers. There is much information on single variables coming in but not arrays so it seems.

 

Very grateful for any assistance.

 

Thank You

 

p.s Just noticed on the preview that it doesn't display the results for index[0] properly but it is the same as the other indexes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.