Jump to content

stopping empty inputs being parsed to php array


tomd79

Recommended Posts

Hi all

 

I'm guessing this is another real simple question!!

 

I have a HTML form with a number of check boxes that creates an array for my php. The form has one text field for a user to add a unique entry not available from the existing check boxes (see html code)

 

<input type="checkbox" name="players[]" value="somevalue"  /> some value<br />
<input type="checkbox" name="players[]" value="somevalue1"  /> some value 1<br />
<input type="text" name="players[]"  /> Please input players name<br />

 

The problem I'm having is the text input is being parsed in the array even if its empty (i.e User doen't want to add another unique value)...

 

What I want to happen is the text input is parsed if a value is entered but is simply ignored if the text input is empty. I do NOT want it to report an error is the text input is empty.....

 

I've been browsing the web and can see loads of documention on form validation, but everything seems to be about validating inputs or throwing errors. I can't find anything on simply ignoring empty fields and making sure they don't get submitted...

 

My guess is that the $post has to send the empty text input, so the PHP will have to have a check to look for empty inputs and disregard them?

Link to comment
Share on other sites

Any input that is in a form will be submitted with the form regardless of it's content. This doesn't matter though because you can choose what to do with it when you process it. For example, if you don't want an error to be displayed then simply don't throw an error because it's empty! This isn't an automatic thing, you must actually do it yourself so just don't.

Link to comment
Share on other sites

would anyone help with this as I'm pulling my hair out and getting nowhere!!

 

This is a 'simple' team picker for our works 5 aside.. its working, but I have tried to add a field to the form where a guest players name can be inserted.. The problem is if no guest is playing the empty field is still sent, and counted in the PHP..

 

The html form:

<form action="sort.php" method="POST">
<input type="checkbox" name="players[]" value="name"  /> name <br />
<input type="checkbox" name="players[]" value="name1"  /> name 1<br />
<input type="checkbox" name="players[]" value="name2"  /> name 2<br />
<input type="checkbox" name="players[]" value="name3"  /> name 3<br />
<input type="text" name="players[]"  /> Enter guests name<br />
<br />
<input type="submit" value="Pick the teams!">
</form><br />

 

The php:

<?php
shuffle( $_POST['players'] );
$player_count = count( $_POST['players'] );
$team_size = ceil( $player_count / 2 );
list( $team1, $team2 ) = array_chunk( $_POST['players'], $team_size );
echo "<h2>Team 1</h2>";
echo "<b>Player count:</b> " . count( $team1 ) . "<br />";
echo "<b>Player list:</b>";
echo "<ul>";

foreach ( $team1 as $player ) {
echo "<li>$player</li>";
}
echo "</ul>";

echo "<h2>Team 2</h2>";
echo "<b>Player count:</b> " . count( $team2 ) . "<br />";
echo "<b>Player list:</b>";
echo "<ul>";
foreach ( $team2 as $player ) {
echo "<li>$player</li>";
}
echo "</ul>";

?>

 

I have tried alsorts but cant get the empty elements removed before the shuffle.. help would be greatly appreciated.

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.