douxiuar885064 2014-06-23 06:25
浏览 23
已采纳

使用IF条件在PHP内部回显Javascript

Here's the situation. I am a beginner

I have a table called favorite which stores Id's of the sender and receiver (To favorite list a user). I have done the php coding to add and remove from favorites

This is the form for submitting the form to addfav.php

<form method="post" action="addfav.php" id="addfav">
     <td width="10%"><img src="images/watchlist.png"  width="24" /></td>
     <td align="left"><a href="javascript:{}" onclick="document.getElementById('addfav').submit(); return false;">Add to Favourite</a></td>
     <input type="hidden" value=" <?php echo $temp; ?>" name="toid" />
</form>

Similarly i have another form for removing from favorite

<form method="post" action="remfav.php" id="remfav">
    <td width="10%"><img src="images/watchlist.png"  width="24" /></td>
    <td align="left"><a href="javascript:{}" onclick="document.getElementById("remfav").submit(); return false;">Remove</a></td>
    <input type="hidden" value=" <?php echo $temp; ?>" name="toid" />
</form>

Both the PHP's are workking... What i want is to use an if condition to check whether the user is already added and display the add/remove form accordingly

I Have the sql query with me

$result1 = mysql_query("SELECT * FROM `favourite` WHERE `sendfrom`='".$_SESSION["id"]."' AND sendto="ID of current user"); 
$num_rows1 = mysql_num_rows($result1);

So if $num_rows1 returns 1 means there is value in the table then REMOVE must appear else ADD.

I Tried to encapsulate both the form inside a php tag with an If conditon.. but the java script doesn't work..

PLs Help

  • 写回答

3条回答 默认 最新

  • dongpai6552 2014-06-23 06:32
    关注

    It's very straightforward:

    <?php
    $result1 = mysql_query("SELECT * FROM `favourite` WHERE `sendfrom`='".$_SESSION["id"]."' AND sendto='ID of current user'"); 
    $num_rows1 = mysql_num_rows($result1);
    
    if ($num_rows1 > 0): /* Open condition. Display remove block */
    ?>
    <form method="post" action="remfav.php" id="remfav">
        <td width="10%"><img src="images/watchlist.png"  width="24" /></td>
        <td align="left"><a href="javascript:{}" onclick="document.getElementById('remfav').submit(); return false;">Remove</a></td>
        <input type="hidden" value=" <?php echo $temp; ?>" name="toid" />
    </form>
    <?php else: /* Close the if block. Display add block if count <= 0 */ ?>
    <form method="post" action="addfav.php" id="addfav">
         <td width="10%"><img src="images/watchlist.png"  width="24" /></td>
         <td align="left"><a href="javascript:{}" onclick="document.getElementById('addfav').submit(); return false;">Add to Favourite</a></td>
         <input type="hidden" value=" <?php echo $temp; ?>" name="toid" />
    </form>
    <?php endif; /* End conditional block */ ?>
    

    Edit: Note that the two following constructions are perfectly equivalent in PHP

    if ($condition1) {                  |    if ($condition1):
        /* Do something */              |        /* Do something */
    } elseif ($condition2) {            |    elseif ($condition2):
        /* Do something else */         |        /* Do something else */
    } else {                            |    else:
        /* Well, you know the drill */  |        /* Well, you know the drill */
    }                                   |    endif;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败