我需要更改提交按钮类

时间:2015-07-06 13:43:44

标签: php html if-statement

我需要在此处按下按钮时从左到右布局和从右到左更改表格是我的代码:

<form method="post">
<ul>
 <li class="left">
    <input name="left" type="submit" class="left" value="left">
 </li>
 <li class="right">
  <input name="right" type="submit" class="right" value="right">
 </li>
 </ul>
</form>

现在我需要添加一个if语句,如:

if(isset($_POST['left'])) {
echo '    <ul>
 <li class="left">
    <input name="left" type="submit" class="left" value="left">
 </li>
 <li class="right">
  <input name="right" type="submit" class="right" value="right">
 </li>
 </ul>';
}
if(isset($_POST['right'])){
{
echo '    <ul>
 <li class="left">
    <input name="left" type="submit" class="right" value="left">
 </li>
 <li class="right">
  <input name="right" type="submit" class="left" value="right">
 </li>
 </ul>';
}

3 个答案:

答案 0 :(得分:1)

您需要一个if/elseif/else构造,您可以像这样实现它:

echo '<form method="post">';
if(isset($_POST['left'])) {
echo '    <ul>
 <li class="left">
    <input name="left" type="submit" class="left" value="left">
 </li>
 <li class="right">
  <input name="right" type="submit" class="right" value="right">
 </li>
 </ul>';
} elseif(isset($_POST['right'])){
echo '    <ul>
 <li class="left">
    <input name="left" type="submit" class="right" value="left">
 </li>
 <li class="right">
  <input name="right" type="submit" class="left" value="right">
 </li>
 </ul>';
} else {
 echo '
<ul>
 <li class="left">
    <input name="left" type="submit" class="left" value="left">
 </li>
 <li class="right">
  <input name="right" type="submit" class="right" value="right">
 </li>
 </ul>';
}

echo  '</form>';

答案 1 :(得分:1)

这是工作代码:

<form method="post" action="#">
<?php

if(isset($_POST['left'])) {
    echo '<ul><li class="left"><input name="left" type="submit" class="left" value="left"></li><li class="right"><input name="right" type="submit" class="right" value="right"></li></ul>';
}
if(isset($_POST['right'])){
    echo '<ul><li class="right"><input name="right" type="submit" class="left" value="right"></li><li class="left"><input name="left" type="submit" class="right" value="left"></li></ul>';
}

if(!isset($_POST['right']) && !isset($_POST['left'])){
    echo '<ul><li class="left"><input name="left" type="submit" class="right" value="left"></li><li class="right"><input name="right" type="submit" class="left" value="right"></li></ul>';
}
?>

</form>

答案 2 :(得分:0)

你不需要使用php就可以使用jQuery

JQuery有一个toogle类:

http://api.jquery.com/toggleclass/

使用此功能从右到左输入输入

示例:

HTML:

  <form method="post">
    <ul>
     <button class="changeside">Change side</button>
     <li class="left">
        <input name="left" type="submit" class="left" value="left">
     </li>
     </ul>
    </form>

的Javascript

 <script>
    $( ".changeside" ).click(function() {
      $( this ).toggleClass( "left" );
      $( this ).toggleClass( "right" );
    });
    </script>