PHP POST函数用于粘性表单

时间:2011-07-08 21:31:17

标签: php html post

我使这个函数显示一个表单,表单必须是粘性的,所以我想添加一个if POST设置它回显该值。这可以确保当用户输入错误输入时,所有正确的输入仍将被填写。但是我以前用过

<input type="text" name="Avoornaam"  value = "<?php if(isset($_POST['Avoornaam'])){echo htmlentities($_POST['Avoornaam']);} ?>"/>

但现在我想将其迁移到一个函数:

function BabysitterForm(){
        return '        <form id="form_206335" class="appnitro"  method="post" action="self">
                        <div class="form_description">
                <h2>Babysitter</h2>
                <p>Gelieve je hier in te schrijven als babysitter</p>
            </div>                      
                <ul >

                        <li id="li_1" >
            <label class="description" for="element_1">Naam </label>
            <div>
                <input id="element_1" name="element_1" class="element text medium" type="text" maxlength="255" value="'.if(isset($_POST['Avoornaam'])){echo htmlentities($_POST['Avoornaam']);}.'"/> 
            </div><p class="guidelines" id="guide_1"><small>Voer uw naam in.</small></p> 
            </li>       <li id="li_2" >
            <label class="description" for="element_2">Voornaam </label>
            <div>
                <input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value=""/> 
            </div><p class="guidelines" id="guide_2"><small>Vul uw voornaam in.</small></p> 
            </li>       <li id="li_3" >
            <label class="description" for="element_3">Adres </label>
            <div>
                <input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/> 
            </div><p class="guidelines" id="guide_3"><small>vul uw straat en huisnummer in.</small></p> 
            </li>       <li id="li_4" >
            <label class="description" for="element_4">Woonplaats </label>
            <div>
                <input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value=""/> 
            </div><p class="guidelines" id="guide_4"><small>vul uw woonplaats in.</small></p> 
            </li>       <li id="li_5" >
            <label class="description" for="element_5">Postcode </label>
            <div>
                <input id="element_5" name="element_5" class="element text small" type="text" maxlength="255" value=""/> 
            </div><p class="guidelines" id="guide_5"><small>Vul uw Postcode hier in</small></p> 
            </li>       <li id="li_6" >
            <label class="description" for="element_6">telefoonnummer </label>
            <div>
                <input id="element_6" name="element_6" class="element text medium" type="text" maxlength="255" value=""/> 
            </div><p class="guidelines" id="guide_6"><small>Vul uw telefoonnummer in.</small></p> 
            </li>       <li id="li_7" >
            <label class="description" for="element_7">email </label>
            <div>
                <input id="element_7" name="element_7" class="element text medium" type="text" maxlength="255" value=""/> 
            </div><p class="guidelines" id="guide_7"><small>vul uw email in.</small></p> 
            </li>       <li id="li_8" >
            <label class="description" >Geboortedatum </label>
            <span>
                <input id="element_8_1" name="element_8_1" class="element text" size="2" maxlength="2" value="" type="text"> /
                <label for="element_8_1">MM</label>
            </span>
            <span>
                <input id="element_8_2" name="element_8_2" class="element text" size="2" maxlength="2" value="" type="text"> /
                <label for="element_8_2">DD</label>
            </span>
            <span>
                <input id="element_8_3" name="element_8_3" class="element text" size="4" maxlength="4" value="" type="text">
                <label for="element_8_3">YYYY</label>
            </span>

            <li id="li_10" >
            <label class="description" for="element_10">Wachtwoord </label>
            <div>
                <input id="element_10" name="element_10" class="element text medium" type="text" maxlength="255" value=""/> 
                <input id="element_10_1" name="element_10_1" class="element text medium" type="text" maxlength="255" value=""/> 
            </div><p class="guidelines" id="guide_10"><small>Vul uw wachtwoord twee maal in. Hierdoor voorkomt men typfouten door validatie.</small></p> 
            </li>       



                <li id="li_9" >
            <label class="description" for="element_9">Opmerkingen </label>
            <div>
                <textarea id="element_9" name="element_9" class="element textarea medium"></textarea> 
            </div> 
            </li>

                        <li class="buttons">
                    <input type="hidden" name="form_id" value="206335" />

                    <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
            </li>
                </ul>
            </form> ';
    }

问题是,如果我将该函数与HTML结合,我的语法错误就会出现。

任何人都有一个好的解决方案

2 个答案:

答案 0 :(得分:1)

而不是

"'.if(isset($_POST['Avoornaam'])){echo htmlentities($_POST['Avoornaam']);}.'"

您可以使用类似

的内容
"'.((isset($_POST['Avoornaam']))?htmlentities($_POST['Avoornaam']):'').'"

但这比Mike发布的其他解决方案要糟糕得多。

答案 1 :(得分:0)

你需要做

if (isset($_POST['blah'])) $return .= $_POST['blah'];

你想要做的所有时间。

编辑:我没有看到你试图回应它的部分,因为我没有滚动到右边。这就是你想要的:

function BabysitterForm(){
    $return = '        <form id="form_206335" class="appnitro"  method="post" action="self">
                    <div class="form_description">
            <h2>Babysitter</h2>
            <p>Gelieve je hier in te schrijven als babysitter</p>
        </div>
            <ul >

                    <li id="li_1" >
        <label class="description" for="element_1">Naam </label>
        <div>
            <input id="element_1" name="element_1" class="element text medium" type="text" maxlength="255" value="'
    if(isset($_POST['Avoornaam'])){$return .= htmlentities($_POST['Avoornaam']);}
    $return .= '"/>
        </div><p class="guidelines" id="guide_1"><small>Voer uw naam in.</small></p>
        </li>       <li id="li_2" >
        <label class="description" for="element_2">Voornaam </label>
        <div>
            <input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value=""/>
        </div><p class="guidelines" id="guide_2"><small>Vul uw voornaam in.</small></p>
        </li>       <li id="li_3" >
        <label class="description" for="element_3">Adres </label>
        <div>
            <input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/>
        </div><p class="guidelines" id="guide_3"><small>vul uw straat en huisnummer in.</small></p>
        </li>       <li id="li_4" >
        <label class="description" for="element_4">Woonplaats </label>
        <div>
            <input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value=""/>
        </div><p class="guidelines" id="guide_4"><small>vul uw woonplaats in.</small></p>
        </li>       <li id="li_5" >
        <label class="description" for="element_5">Postcode </label>
        <div>
            <input id="element_5" name="element_5" class="element text small" type="text" maxlength="255" value=""/>
        </div><p class="guidelines" id="guide_5"><small>Vul uw Postcode hier in</small></p>
        </li>       <li id="li_6" >
        <label class="description" for="element_6">telefoonnummer </label>
        <div>
            <input id="element_6" name="element_6" class="element text medium" type="text" maxlength="255" value=""/>
        </div><p class="guidelines" id="guide_6"><small>Vul uw telefoonnummer in.</small></p>
        </li>       <li id="li_7" >
        <label class="description" for="element_7">email </label>
        <div>
            <input id="element_7" name="element_7" class="element text medium" type="text" maxlength="255" value=""/>
        </div><p class="guidelines" id="guide_7"><small>vul uw email in.</small></p>
        </li>       <li id="li_8" >
        <label class="description" >Geboortedatum </label>
        <span>
            <input id="element_8_1" name="element_8_1" class="element text" size="2" maxlength="2" value="" type="text"> /
            <label for="element_8_1">MM</label>
        </span>
        <span>
            <input id="element_8_2" name="element_8_2" class="element text" size="2" maxlength="2" value="" type="text"> /
            <label for="element_8_2">DD</label>
        </span>
        <span>
            <input id="element_8_3" name="element_8_3" class="element text" size="4" maxlength="4" value="" type="text">
            <label for="element_8_3">YYYY</label>
        </span>

        <li id="li_10" >
        <label class="description" for="element_10">Wachtwoord </label>
        <div>
            <input id="element_10" name="element_10" class="element text medium" type="text" maxlength="255" value=""/>
            <input id="element_10_1" name="element_10_1" class="element text medium" type="text" maxlength="255" value=""/>
        </div><p class="guidelines" id="guide_10"><small>Vul uw wachtwoord twee maal in. Hierdoor voorkomt men typfouten door validatie.</small></p>
        </li>



            <li id="li_9" >
        <label class="description" for="element_9">Opmerkingen </label>
        <div>
            <textarea id="element_9" name="element_9" class="element textarea medium"></textarea>
        </div>
        </li>

                    <li class="buttons">
                <input type="hidden" name="form_id" value="206335" />

                <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
        </li>
            </ul>
        </form> ';
    return $return;
}