PHP POST in function for sticky form
I made this function to display a form, the form had to be sticky so I wanted to add开发者_运维技巧 an if POST is set that it echoes that value. This makes sure that when a user enters bad input, all the right input will still be in filled out. However I used to use
<input type="text" name="Avoornaam" value = "<?php if(isset($_POST['Avoornaam'])){echo htmlentities($_POST['Avoornaam']);} ?>"/>
But now I want to migrate that to a function :
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> ';
}
The problem is that I get a syntax error on my if where I concatinate the function with the HTML.
Anyone a nice solution ?
Instead of
"'.if(isset($_POST['Avoornaam'])){echo htmlentities($_POST['Avoornaam']);}.'"
You could use something like
"'.((isset($_POST['Avoornaam']))?htmlentities($_POST['Avoornaam']):'').'"
But this is a lot worse to read than the other solution posted by Mike.
You will need to do
if (isset($_POST['blah'])) $return .= $_POST['blah'];
for all the times you want to do that.
Edit: I didn't see the part where you try to echo it since I didn't scroll to the right. This is what you want:
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;
}
精彩评论