使用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

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;

Your query syntax were wrong.

Try this,

<?php
    $result1 = mysql_query("SELECT * FROM `favourite` 
                            WHERE `sendfrom`='".$_SESSION["id"]."' AND 
                            sendto=$current_user"); 
    $num_rows1 = mysql_num_rows($result1);

    if($num_rows1 == 1) {
?>
      <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 {
?>
      <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
    }
?>

I think using a form in this case makes no sense.

Try this one instead.

<form method="post" action="addfav.php" id="addfav">
    <td width="10%"><img src="images/watchlist.png"  width="24" /></td>
    <td align="left"><a href="addfav.php?id=whatever">Add to Favourite</a></td>
</form>

Same for remove. If you want to get rid of the ?id=whatever in your URL you can simply use a header redirect. And if you want a great user experience you could even use an AJAX call to to the update in the background.