Is there a better way to handle form tab order in PHP than this?
I have a page with multiple repeated forms. Due to the layout I want the tab order to be non-sequential, so I have to specify it in the html (so that I can specify it out of order). My forms are constructed using PHP's block quote mechanism, which as far as I can make out doesn't allow arithmetic on PHP variables inside the block
I've implemented it, but my implementation seems awkward. I can't help thinking there's a more elegant way to do this.
First I initialize some php variables at the top of the page.
$tab1=1;
$tab2=2;
$tab3=3;
$tab4=5;
$tab5=4;
Then later on, inside a php loop I do this...
$tab1+=5;
$tab2+=5;
$tab3+=5;
$tab4+=5;
$tab5+=5;
Then add the variables to the block quote...
$picshtml .= <<<开发者_如何学运维BLOCK
<div class="addcomment">
<a href="">Add Comment</a><br /><br />
<div id="commentform$row[photoid]" class="commentform">
<div id="submitanim$row[photoid]" style="display:none">
<img src="loadingAnimation.gif" alt="Processing..." />
</div>
<form action="">
<div style="float:left;clear:left;text-align:right">
Comment<br />Name <input type="text" id="name$row[photoid]" tabindex="$tab1" /> <br />
Email <input type="text" id="email$row[photoid]" tabindex="$tab2" /><br />
Website <input type="text" id="website$row[photoid]" tabindex="$tab3" /><br />
<input type="button" value="Submit Comment" id="submit$row[photoid]" onclick="javascript:ajax($row[photoid])" tabindex="$tab4" />
</div>
<textarea rows="5" cols="50" id="comment$row[photoid]" tabindex="$tab5" ></textarea>
</form>
</div>
</div>
BLOCK;
How about sprintf
?
$html = <<<BLOCK
...
<input type="text" tabindex="%d" />
...
BLOCK;
$order = 0;
$html = sprintf($html, $order, $order + 1, $order + 2, $order + 3, $order + 4, $order + 5, ...);
$order += 6;
// ...
Syntax reference here:
http://php.net/sprintf
精彩评论