在返回if / else语句时隐藏DIV

时间:2014-03-19 21:43:59

标签: javascript php jquery html css

我一直试图在此代码中隐藏此div ID #signup我正在努力工作并且似乎无法使用我正在做的工作。有什么好方法可以隐藏这个div ID,淡出并反过来淡入新div #test?这就是我得到的!

HTML(索引)

     <form id="signup" action="<?=$_SERVER['PHP_SELF']; ?>" method="get">
      <fieldset>

          <label for="email" id="address-label">Email Address</label>
          <input type="text" name="email" id="email" />
          <input type="image" src="i/join.jpg" name="submit" value="Join" class="btn" alt="Join" />

      </fieldset>
    </form>

    <div id="response">
        <? require_once('inc/store-address.php'); if($_GET['submit']){ echo storeAddress(); } ?>
        <div id="test">test</div>
    </div>

CSS

#test {display: none;}

JS / PHP(inc / store-address.php)

<?php
function storeAddress(){

    if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
        // It worked!
    ( "#signup" ).hide( "slow" );   
    ( "#test" ).show( "slow" );
    }

    else {
        // An error ocurred, return error message   
        return 'Error: ' . $api->errorMessage;
    }

}

// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>

2 个答案:

答案 0 :(得分:0)

从我可以看到你试图在一个函数中混合php和Javascript,但这不起作用。 Php在将网页发送到客户端之前运行服务器端,并且Javascript仅在客户端上运行,并且仅在页面发送后才运行。

你最需要在JavaScript中执行此操作,Jquery是一个轻量级库,对于像这样的任务非常推荐。

here是从下面获取代码的链接。

$("#hide").click(function(){
  $("p").hide();
});

$("#show").click(function(){
  $("p").show();
});

答案 1 :(得分:0)

你可以使用php有条件地将js输出到浏览器 - 不是最优雅但它会起作用;关于服务器/客户端的评论很重要,你必须非常注意单/双引号。

<?php
function storeAddress(){

    if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
        // It worked!

     echo '<script>';
     echo '$( "#signup" ).hide( "slow" );';   
     echo '$( "#test" ).show( "slow" );';   
     echo '</script>';

    }

    else {
        // An error ocurred, return error message   
        return 'Error: ' . $api->errorMessage;
    }

}

// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>
相关问题