473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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="testpage.php">
<INPUT type="submit" name="summary" value="Show Summary">
</FORM>

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

Jack
Jul 17 '05 #1
11 2282
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?
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?
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).

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

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

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

</FORM>

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


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 :
Jul 17 '05 #2
On 27 May 2004 10:29:58 -0700, ve****@twmi.rr.com (Jack) wrote:
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="testpage.php">
<INPUT type="submit" name="summary" value="Show Summary">
</FORM>

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

Jack


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.php'>
<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_pluggedout.com
http://www.pluggedout.com/blog
Jul 17 '05 #3
Pedro Graca <he****@hotpop.com> wrote in message news:<2h************@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?
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?


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).

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

$show_summary = false;
}

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

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

</FORM>

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


HTH


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
Jul 17 '05 #4
Jon Beckett <jonbeckett@nospam_yahoo.co.uk> wrote in message news:<r5********************************@4ax.com>. ..
On 27 May 2004 10:29:58 -0700, ve****@twmi.rr.com (Jack) wrote:
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="testpage.php">
<INPUT type="submit" name="summary" value="Show Summary">
</FORM>

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

Jack


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.


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?
If you do use a form,

<form method='POST' action='page.php'>
<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

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?

Regards

Jonathan Beckett
jonbeckett_at_pluggedout.com
http://www.pluggedout.com/blog

Jul 17 '05 #5
Jack wrote:
Pedro Graca <he****@hotpop.com> wrote in message news:<2h************@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 :-)
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 :
Jul 17 '05 #6
Pedro Graca <he****@hotpop.com> wrote in message news:<2h************@uni-berlin.de>...
Jack wrote:
Pedro Graca <he****@hotpop.com> wrote in message news:<2h************@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 :-)


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.
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
}
?>
========


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

Jack
Jul 17 '05 #7
Jack wrote:
Pedro Graca <he****@hotpop.com> wrote in message news:<2h************@uni-berlin.de>...
Jack wrote:
Pedro Graca <he****@hotpop.com> wrote in message news:<2h************@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 :-)

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.

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.getElementById('myTable').style.cssText =
'visibility: ' + visibility;
}
</script>

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

<input type="button" onclick="toggleTable()" value="table">
--
-Moxley
moxleystratton.com
Jul 17 '05 #8
Jack wrote:
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.


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($_SESSION['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 :
Jul 17 '05 #9
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.


Can't you use javascript?

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

<script language='javascript'>
function showtable() {
b = document.getElementById('thebutton');
t = document.getElementById('thetable');
if (b.value == 'show') {
b.value = 'hide';
t.style.display = '';
} else {
b.value = 'show';
t.style.display = 'none';
}
}
</script>
Jul 17 '05 #10
Moxley Stratton <mo****@REMOVEmoxleydata.com> wrote in message news:<W9********************@comcast.com>...
Jack wrote:
Pedro Graca <he****@hotpop.com> wrote in message news:<2h************@uni-berlin.de>...
Jack wrote:

Pedro Graca <he****@hotpop.com> wrote in message news:<2h************@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 :-)

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.

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.getElementById('myTable').style.cssText =
'visibility: ' + visibility;
}
</script>

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

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


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

Jack
Jul 17 '05 #11
<snip>
I want to display a table on a page based on whether a button is
pressed or not.


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="document.getElementById('dam').style.visi bility='visible'">Show
the dam table</a>

<button onClick="document.getElementById('dam').style.visi bility='hidden'">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.
Jul 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Anne M | last post by:
I have a report based on query..which is a team list with names and their role on team ie coach, assistant and player. Report is almost what I want and my knowledge is limited so I need some...
3
by: Lynn | last post by:
Hello, I have a user control that contains a table, and some text fields. I would like to show or hide a particular row of this table, based on a selection the user makes on my page. Here's...
2
by: Steve Bottoms | last post by:
Hi, all! Using VB as code-behind in asp.net page... I have a TABLE control which I'm building dynamically. After the table is built, I'm trying to retrieve the HEIGHT property of that table...
4
by: robmarleh | last post by:
Hi, I've been trying to set up a php system to calculate and order and recieve delivery address/s. It's going pretty well so far but I having trouble with one particular part. I've been trying...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
2
by: banderson | last post by:
Hello, I have a data entry form for a table with information about buildings and am having a problem making a combo box do what I want. I would like to make the combo box show a list of unique bldg...
5
by: Mike P | last post by:
How would I show or hide a div that is using client side Javascript based upon a server side variable? Here are my divs : <div id="idButton5" class="otherLeftBarLink" onmouseover="javascript:...
1
by: filch | last post by:
Hi, I am new to this forum ... so hello to all! I am trying to get a script working which will show or hide a table based on a user checking or unchecking a parent checkbox. This is the...
1
oranoos3000
by: oranoos3000 | last post by:
hi would you please help me i have a online shopping center that i show pictures of the my product in home page. in the InterExplorer pictures is shown correctly but in Firefox browser is shown...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.