How to show table based on variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack

    How to show table based on variable?

    I want to display a table on a page based on whether a button is
    pressed or not. I'm new at php so I'm sure I'm making a basic
    mistake. Here's what I am trying. My thought was that $show_summary
    would switch states with each click but it is coming up always true.
    So I'm guessing that is the default setting when the page loads. Can
    someone please point out where I am going wrong?

    if(isset($_POST['summary'])) {
    $show_summary = true;
    } else {
    $show_summary = false;
    }

    <FORM method="POST" action="testpag e.php">
    <INPUT type="submit" name="summary" value="Show Summary">
    </FORM>

    <?php if ($show_summary == true) { ?>
    <table border="1" cellpadding="3" >
    <tr>
    <td class="smallTex t" align="center" width="80%"><?p hp echo "URL";
    ?></td>
    </tr>
    </table>
    <?php } ?>

    Jack
  • Pedro Graca

    #2
    Re: How to show table based on variable?

    Jack wrote:[color=blue]
    > I want to display a table on a page based on whether a button is
    > pressed or not.[/color]

    If the button is not pressed, how does PHP receive the data?
    [color=blue]
    > I'm new at php so I'm sure I'm making a basic
    > mistake. Here's what I am trying. My thought was that $show_summary
    > would switch states with each click but it is coming up always true.
    > So I'm guessing that is the default setting when the page loads. Can
    > someone please point out where I am going wrong?[/color]

    You have to have a way to post the form /other/ than the submit button
    named "summary".

    You can do that with another submit button (or maybe with JavaScript).

    [color=blue]
    > if(isset($_POST['summary'])) {
    > $show_summary = true;
    > } else {
    > $show_summary = false;
    > }
    >
    > <FORM method="POST" action="testpag e.php">
    > <INPUT type="submit" name="summary" value="Show Summary">[/color]


    <INPUT type="submit" name="no_summar y" value="Don't Show Summary">

    [color=blue]
    > </FORM>
    >
    > <?php if ($show_summary == true) { ?>
    > <table border="1" cellpadding="3" >
    > <tr>
    > <td class="smallTex t" align="center" width="80%"><?p hp echo "URL";
    > ?></td>
    > </tr>
    > </table>
    > <?php } ?>[/color]

    HTH

    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Jon Beckett

      #3
      Re: How to show table based on variable?

      On 27 May 2004 10:29:58 -0700, vendor@twmi.rr. com (Jack) wrote:
      [color=blue]
      >I want to display a table on a page based on whether a button is
      >pressed or not. I'm new at php so I'm sure I'm making a basic
      >mistake. Here's what I am trying. My thought was that $show_summary
      >would switch states with each click but it is coming up always true.
      >So I'm guessing that is the default setting when the page loads. Can
      >someone please point out where I am going wrong?
      >
      >if(isset($_POS T['summary'])) {
      > $show_summary = true;
      >} else {
      > $show_summary = false;
      >}
      >
      ><FORM method="POST" action="testpag e.php">
      ><INPUT type="submit" name="summary" value="Show Summary">
      ></FORM>
      >
      ><?php if ($show_summary == true) { ?>
      > <table border="1" cellpadding="3" >
      > <tr>
      > <td class="smallTex t" align="center" width="80%"><?p hp echo "URL";
      >?></td>
      > </tr>
      > </table>
      ><?php } ?>
      >
      >Jack[/color]

      Keep things more simple...

      You don't need the FORM.

      Just put an anchor in with a value in the GET data - e.g.
      <a href='page.php? field=x'>button </a>

      Then check $_GET["field"] to see if it's got anything in it.

      If you do use a form,

      <form method='POST' action='page.ph p'>
      <input type='hidden' name='field' value='x'>
      <input type='submit'>
      </form>

      then in the page, access
      $_POST["field"] to see if theres anything in it


      Regards

      Jonathan Beckett
      jonbeckett_at_p luggedout.com

      Comment

      • Jack

        #4
        Re: How to show table based on variable?

        Pedro Graca <hexkid@hotpop. com> wrote in message news:<2hmoa7Fei earU1@uni-berlin.de>...[color=blue]
        > Jack wrote:[color=green]
        > > I want to display a table on a page based on whether a button is
        > > pressed or not.[/color]
        >
        > If the button is not pressed, how does PHP receive the data?
        >[color=green]
        > > I'm new at php so I'm sure I'm making a basic
        > > mistake. Here's what I am trying. My thought was that $show_summary
        > > would switch states with each click but it is coming up always true.
        > > So I'm guessing that is the default setting when the page loads. Can
        > > someone please point out where I am going wrong?[/color]
        >
        > You have to have a way to post the form /other/ than the submit button
        > named "summary".
        >
        > You can do that with another submit button (or maybe with JavaScript).
        >
        >[color=green]
        > > if(isset($_POST['summary'])) {
        > > $show_summary = true;
        > > } else {[/color]
        > $show_summary = false;[color=green]
        > > }
        > >
        > > <FORM method="POST" action="testpag e.php">
        > > <INPUT type="submit" name="summary" value="Show Summary">[/color]
        >
        >
        > <INPUT type="submit" name="no_summar y" value="Don't Show Summary">
        >
        >[color=green]
        > > </FORM>
        > >
        > > <?php if ($show_summary == true) { ?>
        > > <table border="1" cellpadding="3" >
        > > <tr>
        > > <td class="smallTex t" align="center" width="80%"><?p hp echo "URL";
        > > ?></td>
        > > </tr>
        > > </table>
        > > <?php } ?>[/color]
        >
        > HTH[/color]

        Thanks for the help. This does work. I'll look into your suggestion
        about javascript too since I would really rather have just the one
        button.

        Jack

        Comment

        • Jack

          #5
          Re: How to show table based on variable?

          Jon Beckett <jonbeckett@nos pam_yahoo.co.uk > wrote in message news:<r5dcb0lki oftrc0vd5oibjcg nj6as3d5gf@4ax. com>...[color=blue]
          > On 27 May 2004 10:29:58 -0700, vendor@twmi.rr. com (Jack) wrote:
          >[color=green]
          > >I want to display a table on a page based on whether a button is
          > >pressed or not. I'm new at php so I'm sure I'm making a basic
          > >mistake. Here's what I am trying. My thought was that $show_summary
          > >would switch states with each click but it is coming up always true.
          > >So I'm guessing that is the default setting when the page loads. Can
          > >someone please point out where I am going wrong?
          > >
          > >if(isset($_POS T['summary'])) {
          > > $show_summary = true;
          > >} else {[/color]
          > $show_summary = false;[color=green]
          > >}
          > >
          > ><FORM method="POST" action="testpag e.php">
          > ><INPUT type="submit" name="summary" value="Show Summary">
          > ></FORM>
          > >
          > ><?php if ($show_summary == true) { ?>
          > > <table border="1" cellpadding="3" >
          > > <tr>
          > > <td class="smallTex t" align="center" width="80%"><?p hp echo "URL";
          > >?></td>
          > > </tr>
          > > </table>
          > ><?php } ?>
          > >
          > >Jack[/color]
          >
          > Keep things more simple...
          >
          > You don't need the FORM.
          >
          > Just put an anchor in with a value in the GET data - e.g.
          > <a href='page.php? field=x'>button </a>
          >
          > Then check $_GET["field"] to see if it's got anything in it.[/color]

          I tried this but, probably due to my inexperience, I don't see the
          difference. If I click on the link and $_GET returns an x, then that
          could be the "on" setting. Then, when I click on it again, I need it
          to be turned off. But $_GET will still return an x won't it? Am I
          totally missing the point?
          [color=blue]
          > If you do use a form,
          >
          > <form method='POST' action='page.ph p'>
          > <input type='hidden' name='field' value='x'>
          > <input type='submit'>
          > </form>
          >
          > then in the page, access
          > $_POST["field"] to see if theres anything in it
          >[/color]

          As I mentioned, I'm new at this so I am probably missing the obvious,
          but can you explain to me how $_POST["field"] checks for two different
          values since the form is only submitting one?
          [color=blue]
          >
          > Regards
          >
          > Jonathan Beckett
          > jonbeckett_at_p luggedout.com
          > http://www.pluggedout.com/blog[/color]

          Comment

          • Pedro Graca

            #6
            Re: How to show table based on variable?

            Jack wrote:[color=blue]
            > Pedro Graca <hexkid@hotpop. com> wrote in message news:<2hmoa7Fei earU1@uni-berlin.de>...[color=green]
            >> Jack wrote:[color=darkred]
            >> > I want to display a table on a page based on whether a button is
            >> > pressed or not.[/color]
            >>
            >> If the button is not pressed, how does PHP receive the data?[/color][/color]
            [color=blue]
            > Thanks for the help. This does work. I'll look into your suggestion
            > about javascript too since I would really rather have just the one
            > button.[/color]

            I still don't understand how you're going to submit a form without
            pressing a button :-)


            Try this:

            ========
            <?php
            $hidden = 1;
            $show_form = true;
            $show_summary = false;
            $show_thanks = false;
            if (isset($_POST['hidden'])) {
            if ($_POST['hidden'] == '1') { $hidden = 2; $show_summary = true; }
            if ($_POST['hidden'] == '2') { $hidden = 3; $show_form = false; $show_thanks = true; }
            }

            if ($show_form) {
            ?>
            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <input type="hidden" name="hidden" value="<?php echo $hidden; ?>"/>
            text: <input type="text" name="text" value="text"/>
            <input type="submit"/>
            </form>
            <?php
            }

            if ($show_summary) {
            ?>
            <br/><br/>Summary:<br/>
            The text you entered is: [<strong><?php echo $_POST['text']; ?></strong>]<br/>
            <?php
            }

            if ($show_thanks) {
            ?>
            <p>Thank you!</p>
            <p>Turn your computer off, and go read a book.</p>
            <?php
            }
            ?>
            ========

            --
            USENET would be a better place if everybody read: : mail address :
            http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
            http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
            http://www.expita.com/nomime.html : to 10K bytes :

            Comment

            • Jack

              #7
              Re: How to show table based on variable?

              Pedro Graca <hexkid@hotpop. com> wrote in message news:<2hnaacFf4 gc6U1@uni-berlin.de>...[color=blue]
              > Jack wrote:[color=green]
              > > Pedro Graca <hexkid@hotpop. com> wrote in message news:<2hmoa7Fei earU1@uni-berlin.de>...[color=darkred]
              > >> Jack wrote:
              > >> > I want to display a table on a page based on whether a button is
              > >> > pressed or not.
              > >>
              > >> If the button is not pressed, how does PHP receive the data?[/color][/color]
              >[color=green]
              > > Thanks for the help. This does work. I'll look into your suggestion
              > > about javascript too since I would really rather have just the one
              > > button.[/color]
              >
              > I still don't understand how you're going to submit a form without
              > pressing a button :-)[/color]

              I'm not really wanting to submit a form. That was the only way I
              could think of to try to do what I wanted. All I want is a button
              that acts as an on/off switch. Press it once and a table is
              displayed. Press it again and the table is hidden.
              [color=blue]
              > Try this:
              >
              > ========
              > <?php
              > $hidden = 1;
              > $show_form = true;
              > $show_summary = false;
              > $show_thanks = false;
              > if (isset($_POST['hidden'])) {
              > if ($_POST['hidden'] == '1') { $hidden = 2; $show_summary = true; }
              > if ($_POST['hidden'] == '2') { $hidden = 3; $show_form = false; $show_thanks = true; }
              > }
              >
              > if ($show_form) {
              > ?>
              > <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
              > <input type="hidden" name="hidden" value="<?php echo $hidden; ?>"/>
              > text: <input type="text" name="text" value="text"/>
              > <input type="submit"/>
              > </form>
              > <?php
              > }
              >
              > if ($show_summary) {
              > ?>
              > <br/><br/>Summary:<br/>
              > The text you entered is: [<strong><?php echo $_POST['text']; ?></strong>]<br/>
              > <?php
              > }
              >
              > if ($show_thanks) {
              > ?>
              > <p>Thank you!</p>
              > <p>Turn your computer off, and go read a book.</p>
              > <?php
              > }
              > ?>
              > ========[/color]

              I do appreciate you taking the time to enter this code but it is
              getting farther away from what I need.

              Jack

              Comment

              • Moxley Stratton

                #8
                Re: How to show table based on variable?

                Jack wrote:[color=blue]
                > Pedro Graca <hexkid@hotpop. com> wrote in message news:<2hnaacFf4 gc6U1@uni-berlin.de>...
                >[color=green]
                >>Jack wrote:
                >>[color=darkred]
                >>>Pedro Graca <hexkid@hotpop. com> wrote in message news:<2hmoa7Fei earU1@uni-berlin.de>...
                >>>
                >>>>Jack wrote:
                >>>>
                >>>>>I want to display a table on a page based on whether a button is
                >>>>>pressed or not.
                >>>>
                >>>>If the button is not pressed, how does PHP receive the data?[/color]
                >>
                >>
                >>[color=darkred]
                >>>Thanks for the help. This does work. I'll look into your suggestion
                >>>about javascript too since I would really rather have just the one
                >>>button.[/color]
                >>
                >>I still don't understand how you're going to submit a form without
                >>pressing a button :-)[/color]
                >
                >
                > I'm not really wanting to submit a form. That was the only way I
                > could think of to try to do what I wanted. All I want is a button
                > that acts as an on/off switch. Press it once and a table is
                > displayed. Press it again and the table is hidden.[/color]


                If you don't mind using javascript and css:

                <script type="text/javascript">
                var visibility = 'hidden';
                function toggleTable()
                {
                if( visibility == 'visible' ) visibility = 'hidden';
                else visibility = 'visible';

                document.getEle mentById('myTab le').style.cssT ext =
                'visibility: ' + visibility;
                }
                </script>

                <table id="myTable" style="visibili ty: hidden">
                <tr><th>My Table</th></tr>
                </table>

                <input type="button" onclick="toggle Table()" value="table">


                --
                -Moxley
                moxleystratton. com

                Comment

                • Pedro Graca

                  #9
                  Re: How to show table based on variable?

                  Jack wrote:[color=blue]
                  > I'm not really wanting to submit a form. That was the only way I
                  > could think of to try to do what I wanted. All I want is a button
                  > that acts as an on/off switch. Press it once and a table is
                  > displayed. Press it again and the table is hidden.[/color]

                  As Moxley said you can do it with JavaScript.
                  If you want to do it with PHP, I think your best bet is to use a session
                  variable and toggle it with each submit. Then check that toggle and send
                  (or do not send) the table to the browser; something like:


                  <?php
                  session_start() ;
                  if (!isset($_SESSI ON['table_toggle'])) $_SESSION['table_toggle'] = true;

                  if (isset($_POST['submit_button'])) {
                  // reverse table_toggle
                  $_SESSION['table_toggle'] = !$_SESSION['table_toggle'];
                  }

                  if ($_SESSION['table_toggle']) {
                  // send table to browser
                  }
                  ?>

                  --
                  USENET would be a better place if everybody read: : mail address :
                  http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
                  http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
                  http://www.expita.com/nomime.html : to 10K bytes :

                  Comment

                  • Kevin Thorpe

                    #10
                    Re: How to show table based on variable?

                    [color=blue]
                    > I'm not really wanting to submit a form. That was the only way I
                    > could think of to try to do what I wanted. All I want is a button
                    > that acts as an on/off switch. Press it once and a table is
                    > displayed. Press it again and the table is hidden.[/color]

                    Can't you use javascript?

                    <input id='thebutton' type='submit' value='show' onClick='return
                    showtable()'>
                    <table id='thetable' style='display: none'>
                    ...
                    ...
                    </table>

                    <script language='javas cript'>
                    function showtable() {
                    b = document.getEle mentById('thebu tton');
                    t = document.getEle mentById('theta ble');
                    if (b.value == 'show') {
                    b.value = 'hide';
                    t.style.display = '';
                    } else {
                    b.value = 'show';
                    t.style.display = 'none';
                    }
                    }
                    </script>

                    Comment

                    • Jack

                      #11
                      Re: How to show table based on variable?

                      Moxley Stratton <moxley@REMOVEm oxleydata.com> wrote in message news:<W92dnT_oG rnbeSvdRVn-hw@comcast.com> ...[color=blue]
                      > Jack wrote:[color=green]
                      > > Pedro Graca <hexkid@hotpop. com> wrote in message news:<2hnaacFf4 gc6U1@uni-berlin.de>...
                      > >[color=darkred]
                      > >>Jack wrote:
                      > >>
                      > >>>Pedro Graca <hexkid@hotpop. com> wrote in message news:<2hmoa7Fei earU1@uni-berlin.de>...
                      > >>>
                      > >>>>Jack wrote:
                      > >>>>
                      > >>>>>I want to display a table on a page based on whether a button is
                      > >>>>>pressed or not.
                      > >>>>
                      > >>>>If the button is not pressed, how does PHP receive the data?
                      > >>
                      > >>
                      > >>>Thanks for the help. This does work. I'll look into your suggestion
                      > >>>about javascript too since I would really rather have just the one
                      > >>>button.
                      > >>
                      > >>I still don't understand how you're going to submit a form without
                      > >>pressing a button :-)[/color]
                      > >
                      > >
                      > > I'm not really wanting to submit a form. That was the only way I
                      > > could think of to try to do what I wanted. All I want is a button
                      > > that acts as an on/off switch. Press it once and a table is
                      > > displayed. Press it again and the table is hidden.[/color]
                      >
                      >
                      > If you don't mind using javascript and css:
                      >
                      > <script type="text/javascript">
                      > var visibility = 'hidden';
                      > function toggleTable()
                      > {
                      > if( visibility == 'visible' ) visibility = 'hidden';
                      > else visibility = 'visible';
                      >
                      > document.getEle mentById('myTab le').style.cssT ext =
                      > 'visibility: ' + visibility;
                      > }
                      > </script>
                      >
                      > <table id="myTable" style="visibili ty: hidden">
                      > <tr><th>My Table</th></tr>
                      > </table>
                      >
                      > <input type="button" onclick="toggle Table()" value="table">[/color]

                      Thanks for the code - this works great. My thanks to all those that
                      helped. This was quite a learning experience.

                      Jack

                      Comment

                      • Nairb

                        #12
                        Re: How to show table based on variable?

                        <snip>
                        [color=blue][color=green][color=darkred]
                        > > >I want to display a table on a page based on whether a button is
                        > > >pressed or not.[/color][/color][/color]

                        You don't mention what the data of the table you are referring to, And
                        this is probably off track Or at least the subject of another news
                        group, BUT, perhaps you could do it w/o hitting the server again:


                        <table id="dam" border="1">
                        <tr><td> The dam Table.</tr></td>
                        </table>

                        <a href="#" onClick="docume nt.getElementBy Id('dam').style .visibility='vi sible'">Show
                        the dam table</a>

                        <button onClick="docume nt.getElementBy Id('dam').style .visibility='hi dden'">Hide
                        the dam table</button>

                        Of course I'm sure it brings up a whole new slew of compatibility
                        issues you would have to consider.

                        Comment

                        Working...