admin管理员组

文章数量:1356957

I've got a simple form that I would like to be able to add a hidden value that is only used in the event the user doesn't enter a value. The values are for simple text input fields and can use HTML, PHP, JavaScript, JQuery, or any bination thereof. Let me explain further that the value attributes are already set to contain the values in the DB for that form currently. However, in the event there is no current value in the DB for that specific field and the user does not enter a value themselves, I would like a default value to be passed in the POST.

I've done some research and found examples of custom attributes, etc... but these will not work for me. As I'm using an array builder that passes all of the fields in the form as a single array to a new function. Any help would be greatly appreciated!

The code shown below is the creation of the form... As you can see two separate fields are created redundantly for however many rounds exist within the tournament. I've also provided output HTML code of a two round form. In the event the DB has NULL values for the field in question, and if the user doesn't enter a value of his/her own, I would like to pass the value of "false".

                $rchkpos = array();
                $rchkscore = array();

                for ($i=0; $i<=$tournament['numRounds']; $i++)
                {
                    $rchkthis = array("$i" => "round".$i."pos");
                    $rchkthis2 = array("$i" => "round".$i."score");

                    $rchkpos = array_merge($rchkpos, $rchkthis);
                    $rchkscore = array_merge($rchkscore, $rchkthis2);

                    if ($i > 0)
                    {
                        $roundpos = $rchkpos[$i];
                        $roundscore = $rchkscore[$i];

                        if (!is_null($stats2[$roundpos])){
                            $roundposvar = "$stats2[$roundpos]";
                        }else{ 
                            $roundposvar = false;
                        }

                        if (!is_null($stats2[$roundscore])){ 
                            $roundscorevar = "$stats2[$roundscore]";
                        }else{ 
                            $roundscorevar = false;
                        }

                        $team_stats.="
                        <tr valign='top'>
                            <td align='center'>" . LANG_MAN_ROUND . " $i " . LANG_MAN_POSITION . "</td>     
                            <td class='alt1' align='center'>            
                                <input type='text' name='round[".$rchkpos[$i]."]' value='$stats2[$roundpos]' size='40' maxlength='5' />
                            </td>
                        </tr>

                        <tr valign='top'>
                            <td align='center'>" . LANG_MAN_ROUND . " $i " . LANG_MAN_SCORE . "</td>
                            <td class='alt1' align='center'>
                                <input type='text' name='round[".$rchkscore[$i]."]' value='$stats2[$roundscore]' size='40' maxlength='5' />
                            </td>
                        </tr>";
                    }
                }

Output HTML Example:

                 <tbody><tr valign="top">
                            <td align="center">Round 1 Position</td>        
                            <td align="center" class="alt1">            
                                <input type="text" maxlength="5" size="40" value="2" name="round[round1pos]">
                            </td>
                        </tr>

                        <tr valign="top">
                            <td align="center">Round 1 Score</td>
                            <td align="center" class="alt1">
                                <input type="text" maxlength="5" size="40" value="false" name="round[round1score]">
                            </td>
                        </tr>
                        <tr valign="top">
                            <td align="center">Round 2 Position</td>        
                            <td align="center" class="alt1">            
                                <input type="text" maxlength="5" size="40" value="false" name="round[round2pos]">
                            </td>
                        </tr>

                        <tr valign="top">
                            <td align="center">Round 2 Score</td>
                            <td align="center" class="alt1">
                                <input type="text" maxlength="5" size="40" value="false" name="round[round2score]">
                            </td>
                        </tr>
                </tbody>

I've got a simple form that I would like to be able to add a hidden value that is only used in the event the user doesn't enter a value. The values are for simple text input fields and can use HTML, PHP, JavaScript, JQuery, or any bination thereof. Let me explain further that the value attributes are already set to contain the values in the DB for that form currently. However, in the event there is no current value in the DB for that specific field and the user does not enter a value themselves, I would like a default value to be passed in the POST.

I've done some research and found examples of custom attributes, etc... but these will not work for me. As I'm using an array builder that passes all of the fields in the form as a single array to a new function. Any help would be greatly appreciated!

The code shown below is the creation of the form... As you can see two separate fields are created redundantly for however many rounds exist within the tournament. I've also provided output HTML code of a two round form. In the event the DB has NULL values for the field in question, and if the user doesn't enter a value of his/her own, I would like to pass the value of "false".

                $rchkpos = array();
                $rchkscore = array();

                for ($i=0; $i<=$tournament['numRounds']; $i++)
                {
                    $rchkthis = array("$i" => "round".$i."pos");
                    $rchkthis2 = array("$i" => "round".$i."score");

                    $rchkpos = array_merge($rchkpos, $rchkthis);
                    $rchkscore = array_merge($rchkscore, $rchkthis2);

                    if ($i > 0)
                    {
                        $roundpos = $rchkpos[$i];
                        $roundscore = $rchkscore[$i];

                        if (!is_null($stats2[$roundpos])){
                            $roundposvar = "$stats2[$roundpos]";
                        }else{ 
                            $roundposvar = false;
                        }

                        if (!is_null($stats2[$roundscore])){ 
                            $roundscorevar = "$stats2[$roundscore]";
                        }else{ 
                            $roundscorevar = false;
                        }

                        $team_stats.="
                        <tr valign='top'>
                            <td align='center'>" . LANG_MAN_ROUND . " $i " . LANG_MAN_POSITION . "</td>     
                            <td class='alt1' align='center'>            
                                <input type='text' name='round[".$rchkpos[$i]."]' value='$stats2[$roundpos]' size='40' maxlength='5' />
                            </td>
                        </tr>

                        <tr valign='top'>
                            <td align='center'>" . LANG_MAN_ROUND . " $i " . LANG_MAN_SCORE . "</td>
                            <td class='alt1' align='center'>
                                <input type='text' name='round[".$rchkscore[$i]."]' value='$stats2[$roundscore]' size='40' maxlength='5' />
                            </td>
                        </tr>";
                    }
                }

Output HTML Example:

                 <tbody><tr valign="top">
                            <td align="center">Round 1 Position</td>        
                            <td align="center" class="alt1">            
                                <input type="text" maxlength="5" size="40" value="2" name="round[round1pos]">
                            </td>
                        </tr>

                        <tr valign="top">
                            <td align="center">Round 1 Score</td>
                            <td align="center" class="alt1">
                                <input type="text" maxlength="5" size="40" value="false" name="round[round1score]">
                            </td>
                        </tr>
                        <tr valign="top">
                            <td align="center">Round 2 Position</td>        
                            <td align="center" class="alt1">            
                                <input type="text" maxlength="5" size="40" value="false" name="round[round2pos]">
                            </td>
                        </tr>

                        <tr valign="top">
                            <td align="center">Round 2 Score</td>
                            <td align="center" class="alt1">
                                <input type="text" maxlength="5" size="40" value="false" name="round[round2score]">
                            </td>
                        </tr>
                </tbody>
Share Improve this question edited Jul 15, 2013 at 19:46 ThatTechGuy asked Jul 15, 2013 at 19:32 ThatTechGuyThatTechGuy 8891 gold badge11 silver badges29 bronze badges 2
  • 3 Can you post any code that shows what you have tried? – colestrode Commented Jul 15, 2013 at 19:34
  • @cfs I've added the code, though I'm not sure it will help. – ThatTechGuy Commented Jul 15, 2013 at 19:48
Add a ment  | 

2 Answers 2

Reset to default 5

You can possibly do it like this:

HTML

<input type="text" name="name" />
<input type="hidden" name="name_default" value="name_default" />

PHP (after form submit)

if(!isset($_POST['name'])) {

    $_POST['name'] = $_POST['name_default'];

}

Or make an aray like this:

$array_check = array('name', 'telephone', 'gender');

foreach($array_check AS $key => $value) {

    if(!isset($_POST[$value])) {

        $_POST[$value] = $_POST[$value."_default"];

    }    

}

Keep in mind that users can edit the values of the hidden inputs (with developer tools like Firebug), always check the input!

Other possible approach is to set the default values in a dedicated property in each html input and then replace the value of the field before the post using jQuery:

// before post
$('input').each(function () {
if ($(this).val() == '')
    $(this).val($(this).attr('defaultvalue'));
});

本文标签: phpHidden Default Form ValuesStack Overflow