Submit form with link inside php

1385 views javascript
7

I am trying to submit the form as a link instead of a button inside php and it doesn't seem to be working. Any idea what I might be doing wrong? It works outside the php code but not inside.

<nav class="nav-right">
      <ul>
        <?php
        if(isset($_SESSION['userId'])) {
          echo
          '<form id="logoutfrm" action="includes/logout.inc.php" method="post">
          <li><a href="#">Profile</a></li>
          <li><a href="#" onclick="document.forms["logoutfrm"].submit();">Logout</a></li>
          <li><a href="#">Help</a></li>
          </form>';
        } else {
          echo
          '<li><a href="login.php">Login</a></li>
          <li><a href="signup.php">Sign-Up</a></li>
          <li><a href="#">Help</a></li>';
        }
        ?>
      </ul>
</nav>

answered question

This is not a PHP question. Include the relevant HTML, not server-side code.

And you're going to have problems with your quotes inside the attribute. You should be using proper event binding instead of onclick attributes.

This is where the problem is I don't understand how I asked the question wrong? I don't want to post pages of HTML.

You don't need to post pages of HTML. Just the stuff that's relevant. For example, the HTML generated by the PHP code you currently have in your question should be 9 lines long.

Maybe I am confused miken but it is just simply not running the php file. Thanks for your replies. I just wanted to know if it was possible to run a script of a onclick since I am using a link...

1 Answer

6

<nav class="nav-right">
  <ul>
    <?php
    if(isset($_SESSION['userId'])) {
      echo
      '<form id="logoutfrm" action="includes/logout.inc.php" method="post">
      <li><a href="#">Profile</a></li>
      <li><a href="#" onclick="document.getElementById('logoutfrm').submit();">Logout</a></li>
      <li><a href="#">Help</a></li>
      </form>';
    } else {
      echo
      '<li><a href="login.php">Login</a></li>
      <li><a href="signup.php">Sign-Up</a></li>
      <li><a href="#">Help</a></li>';
    }
    ?>
  </ul>

test : https://jsfiddle.net/0whfsc3a/

posted this

Have an answer?

JD

Please login first before posting an answer.