帮助AJAX,使用PHP和隐藏元素

时间:2010-12-28 07:48:15

标签: php javascript html

这是我第一次使用AJAX,所以我有点困惑,需要你的帮助。我有四个div id,并希望根据数据库的结果切换它们之间的隐藏/显示。 听起来很简单,呃!但是对我来说很难实现。 HELP!

这是我的代码 -

<div id="1">HEya</div>
<div id="2">What's up?</div>
<input type="submit" id='approve' name="action" value="Approve" onclick="a()" class="approve" />
<input type="submit" id='reject' value="Reject" name="action" onclick="r()" class="reject"/>

<script language="javascript" type="text/javascript">
//if cookie exists, at the beginning the form should be hidden


if (<?php $responseanswer['response']=='approve'; ?> ){

document.getElementById('1').style.display = 'none';
document.getElementById('2').style.display = 'inline';

}

//if user clicks reject, hide one element dislay another

else if (<?php $responseanswer['response']=='reject'; ?>){
//if cookie exists
document.getElementById('2').style.display = 'none';
document.getElementById('1').style.display = 'block';

}

else
{


function a()
{
 var a = document.getElementById('2');

document.getElementById('1').style.display= 'block';


}

//on reject creating a new cookie

function r()
{
 var a = document.getElementById('reject');
 document.getElementById('1').style.display = 'none';
document.getElementById('2').style.display= 'block';


}

}

</script>

Eveything很好,但div并没有隐藏。

1 个答案:

答案 0 :(得分:0)

    if (<?php echo ($responseanswer['response']=='approve') ? true:false; ?> == 1 ){

document.getElementById('hide').style.display = 'none';
document.getElementById('onapprove').style.display = 'inline';

}

//if user clicks reject, hide one element dislay another

else if (<?php echo ($responseanswer['response']=='reject')? true:false; ?> == 1 ){
//if cookie exists
document.getElementById('hide').style.display = 'none';
document.getElementById('onreject').style.display = 'block';

}

编辑:

这一行<?php $responseanswer['response']=='approve'; ?>,对浏览器没有任何回应,所以你的js脚本会是这样的 if () { some code } else if () { some code } 但是这一行<?php echo ($responseanswer['response']=='reject')? true:false; ?>根据条件($responseanswer['response']=='reject')回显0或1(假或真),所以你的js代码看起来像if(1 == 1) { some code } else if (0 == 1) { some code },现在你有一些条件要测试你不是吗?

第二次编辑:使用此代码并测试它(将数组值更改为aprove或reject并查看差异

    <?php $responseanswer = array('response' => 'aprove'); ?>
<div id="1">HEya</div>
<div id="2">What's up?</div>
<br />
<input type="submit" id='approve' name="action" value="Approve" onclick="a();" class="approve" />
<input type="submit" id='reject' value="Reject" name="action" onclick="r();" class="reject"/>

<script language="javascript" type="text/javascript">
//if cookie exists, at the beginning the form should be hidden


if (<?php echo ($responseanswer['response']=='aprove')? 1:0; ?> == 1)
{
    document.getElementById('1').style.display = 'none';
    document.getElementById('2').style.display = 'inline';

} else if (<?php echo ($responseanswer['response']=='reject')? 1:0; ?> == 1){

    document.getElementById('2').style.display = 'none';
    document.getElementById('1').style.display = 'block';

} else {
    function a()
    {
        var a = document.getElementById('2');
        document.getElementById('1').style.display= 'block';
        document.getElementById('2').style.display = 'none';
    }
    function r()
    {
        var a = document.getElementById('reject');
        document.getElementById('1').style.display = 'none';
        document.getElementById('2').style.display= 'block';
    }
}

</script>