Display Dynamic form on a click event of a Button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    Display Dynamic form on a click event of a Button

    I need to display a form dynamically.
    First i load the page and at that time Login button will display.
    Nothing else.
    If some clicks the Login button, then only I want to display the Form.
    So what will be the best solution for this.

    [PHP]
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitl ed Document</title>
    </head>

    <body><input name="login" type="button" id="Login" value="Login" />
    <form id="form1" name="form1" method="post" action="login.p hp">
    <p>
    <input type="text" name="textfield " />
    <input type="text" name="textfield 2" />
    <input type="submit" name="Submit" value="Submit" />
    </p>
    </form>
    </body>
    </html>
    [/PHP]
  • xwero
    New Member
    • Feb 2007
    • 99

    #2
    Originally posted by ajaxrand
    I need to display a form dynamically.
    First i load the page and at that time Login button will display.
    Nothing else.
    If some clicks the Login button, then only I want to display the Form.
    So what will be the best solution for this.

    [PHP]
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitl ed Document</title>
    </head>

    <body><input name="login" type="button" id="Login" value="Login" />
    <form id="form1" name="form1" method="post" action="login.p hp">
    <p>
    <input type="text" name="textfield " />
    <input type="text" name="textfield 2" />
    <input type="submit" name="Submit" value="Submit" />
    </p>
    </form>
    </body>
    </html>
    [/PHP]
    the most easy way in php is to use an if statement


    [PHP]
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitl ed Document</title>
    </head>

    <body>
    <form id="form1" name="form1" method="post" action="login.p hp">
    <?php if($_POST['login']){ ?>
    <p>
    <input type="text" name="textfield " />
    <input type="text" name="textfield 2" />
    <input type="submit" name="Submit" value="Submit" />
    </p>
    <?php }else{ ?>
    <input name="login" type="submit" id="Login" value="Login" />
    <?php } ?>
    </form>
    </body>
    </html>
    [/PHP]

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Its sorry to say that this is not working as i need, please read the original post carefully, i have mentioned my requirement there.

      When i first load the page login button will display.(Not the form)
      Then when i press the Login button form area will display, then i can submit it for processing.

      Anyway i made it. But few more questions.Here in my coding when i press the Login button Form is getting. But since i am using PHP self execute, the entire page area will reload..

      Is there any way to avoid this. I mean with out reloading the entire page cant i load Just only the form to the same page.

      [PHP]
      <?
      if ($_POST['login'])
      {
      $form = '<form id="form1" name="form1" method="post" action="login.p hp">
      <input type="text" name="textfield " />
      <input type="text" name="textfield 2" />
      <input type="submit" name="Submit" value="Submit" />
      </form>';
      }
      ?>
      <html>

      <head>

      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

      <title>Untitl ed Document</title>

      </head>



      <body>

      <form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
      <input name="login" type="submit" id="Login" value="Login" />
      </form>
      <?php echo $form; ?>
      </body>

      </html>
      [/PHP]

      Comment

      • xwero
        New Member
        • Feb 2007
        • 99

        #4
        Your code is the same as mine only you let the login button on the page, you replaced the login form to the code and you changed the action of the form tag .

        Why would you let the login stay there without use? Or are you thinking about toggleing the form by that button.

        Putting the html code in php code to display it is considered bad practice in these times of templated website design.

        To answer your question: it's not possible to do this without a page reload because php is a server language. It always needs a roundtrip to the server to make things work. that's why javascript and the xmlhttp object get so much attention these days.

        code with toggleing
        [PHP]
        <html>

        <head>

        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

        <title>Untitl ed Document</title>

        </head>



        <body>

        <form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF; ?>">

        <?php
        // to be sure you could replace the if statment with
        // isset($_POST['login']) && $_POST['login'] == 'Login'
        if($_POST['login']){ ?>
        <p><input name="hide" type="submit" id="Login" value="Hide Login" /> </p>
        <p>

        <input type="text" name="textfield " />

        <input type="text" name="textfield 2" />

        <input type="submit" name="Submit" value="Submit" />

        </p>

        <?php }else{ ?>

        <p><input name="login" type="submit" id="Login" value="Login" /> </p>

        <?php } ?>

        </form>

        </body>

        </html>
        [/PHP]

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          OK thanks.Looks like i have to go for a Ajax Solution for this.
          Thank you very much again.

          Comment

          Working...