SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generate & Process Form on same page with HTML table

    Hi
    I am just starting out with PHP, and have set myself the task of recreating a simple version of this page http://www.runningforfitness.org/calc/convert.php

    I have managed to write the code that takes the user input and displays the processed results on a different page but I really want the form generation and processing to occur on the same page.

    I believe I need to have the form action pointing to the page in which it appears and then when the submit button is pressed for some php code to step in and generate the results table.

    I want my results to be displayed within a HTML table so how do I write the code within a php block?
    If I simply write the HTML code inside the PHP nothing happens.

    If anyone does something similar to this I would be grateful if you could post the code for me to try and unscramble.

    The trouble when you are just learning is that its very difficult to know what help you need and how to ask for it - so sorry if this is not clear.

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,dont know exactly if this is what you look for, but the principle works:

    <?php

    if($_POST['submit']){ // if submit is pressed (button named 'submit')

    print '<table>ztr><td>some data...';

    }

    ?>

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That condition is insufficient and ambiguous. It relies on the value of the submit button evaluating to true, throws notices if not (accessing a key that may not be in the array), and the submit button is not sent by all browsers, including IE when you press enter instead of clicking the button to submit.

    Test for a non-empty value in one of the required fields, or create a hidden field indicating the form was submitted (guaranteed to have a value), or test if the entire $_POST array is empty.

  4. #4
    Passionate Web Developer Egyptechno's Avatar
    Join Date
    Jan 2004
    Location
    Dubai
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    if($_POST['logme']){
    if(!empty(
    $_POST['username']) AND !empty($_POST['pass'])){
    echo 
    "Your Are Logged $_POST[username] ";
    }else{
    echo 
    "You have to fill out the two fields !";
    }
    }else{
    ?>
    <form method="post">
    Username : <input type="text" name="username" />
    Password: <input type="password" name="pass" />
    <br/>
    <input type="submit" name="logme" value="Login" />
    </form>
    <?php
    }
    hope that helps !

  5. #5
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the responses.

    I have now managed to get php to generate a html form - thanks danman.
    I am now having problems actually utilising one of the variables within the table. In the following code I got an parse error, unexpected T_string on the line starting <td>echo $_GET...... from the colour coding applied by dreamweaver it appears that the "echo $GET" bit is not recognised as code at all.

    <?php
    if($_GET['speedsubmit']){ // if submit is pressed)
    print '<table width="50%" border="0">
    <tr>
    <td><p>put php var here</p></td>
    <td>echo $_GET['min']</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>';
    }
    else {
    echo "do nothing yet";
    }
    ?>

    Dan Grossman - I get your point about the use of a hidden field, I will definately move onto that when I get my form issues sorted.

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman
    That condition is insufficient and ambiguous. It relies on the value of the submit button evaluating to true, throws notices if not (accessing a key that may not be in the array), and the submit button is not sent by all browsers, including IE when you press enter instead of clicking the button to submit.
    Pressing enter only submits the form if it there is one field in it?
    Well maybe it is not sent if you tab to the submit button and press enter either,hmm guess I should change the way I do that.

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by warriorwomen
    Thanks for all the responses.

    I have now managed to get php to generate a html form - thanks danman.
    I am now having problems actually utilising one of the variables within the table. In the following code I got an parse error, unexpected T_string on the line starting <td>echo $_GET...... from the colour coding applied by dreamweaver it appears that the "echo $GET" bit is not recognised as code at all.
    Because the code is not correct. You're still inside a print statement there, it would print the word "echo" if the single quote after that didn't terminate the string being "print"ed and then end the statement without a semicolon.

    PHP Code:
    <?php if (isset($_GET['speedsubmit'])) { ?>
          <table width="50%"  border="0">
            <tr>
              <td><p>put php var here</p></td>
              <td><?php echo $_GET['min']; ?></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </table>
    <?php 
    } else {
      echo 
    "Do nothing yet.";
    }
    ?>

  8. #8
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha!
    Thank you very much for helping out there.
    I will learn to write my own code eventually!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •