Need Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rotavirus
Forum Newbie
Posts: 7
Joined: Thu May 28, 2009 5:40 pm

Need Help

Post by rotavirus »

Hello Everyone,

I m new to php and trying to develop a basic application using php.

What I am trying to do :
1. Create a Button
2. Whenever user click that button it should display different message.
For example, 1st click -- Hi
2nd click -- How are you?

Here is my code ....

Code: Select all

 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
         <form method="post" action="count.php">
           <input type="submit" value="Click me!" name = "action" />
       </form>
       
        
    </body>
</html>
 
count.php
 
<?php
FactCount(); 
function FactCount()
       {        
                
       if($_POST['action'] == 'Click me!')
                        echo "<h4>Hi </h4>";
 
             
Now my question is......

I want that "Click Me!" button after this output. In addition, if it clicked again then it should display other message (how are you).

Can any one please give their inputs....?

Thanks
Last edited by Benjamin on Fri May 29, 2009 10:25 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need Help

Post by requinix »

Look into "sessions".
rotavirus
Forum Newbie
Posts: 7
Joined: Thu May 28, 2009 5:40 pm

Re: Need Help

Post by rotavirus »

Thanks.....will look into session.....
rotavirus
Forum Newbie
Posts: 7
Joined: Thu May 28, 2009 5:40 pm

Re: Need Help

Post by rotavirus »

The session thing is working.....now the problem is....
when i click button then it shows...fact....and not button...
I want a button below the ouput and if the user click on that button than it should display other fact...


thanks
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Need Help

Post by mikemike »

Can we see what code you have now as there are any number of places you could be going wrong?
rotavirus
Forum Newbie
Posts: 7
Joined: Thu May 28, 2009 5:40 pm

Re: Need Help

Post by rotavirus »

this is index.php

Code: Select all

 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
         <form method="post" action="count.php">
           <input type="submit" value="Click me!" name = "action" />
       </form>
       
        
    </body>
</html>
 
count.php

Code: Select all

 
<?php
 session_start(); 
 FactCount();   
function FactCount()
       {        
            if(isset($_SESSION['count'])){
                $_SESSION['count']=$_SESSION['count']+1;
                $x = $_SESSION['count'];        
                switch ($x)
                {
                    case 2:
                        echo "<br>Fact 2";
                        break;
                    case 3:
                        echo "\nFact 3";
                        break;
                    case 4:
                        echo "\nFact 4";
                        break;
                    default:
                        echo "<br>Fact 5";
                        session_destroy();
                }
                    
            }
            else{
                $_SESSION['count']=1;
                echo 'Fun Fact 1';
            }
            echo "<br>Views=". $_SESSION['count'];
        
       }
   
?>
 
Thanks
Last edited by Benjamin on Fri Jun 05, 2009 4:48 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Need Help

Post by califdon »

It looks to me like what is happening is that, since you don't have a switch case for $x==1, every time you invoke the function, it's going to go to the default and destroy the session.
rotavirus
Forum Newbie
Posts: 7
Joined: Thu May 28, 2009 5:40 pm

Re: Need Help

Post by rotavirus »

When i click button first time then it will go in else loop....and echo fact1
later on when i click on button....then it goes into switch case....
so this logic is working properly.....

but once i click the button then I need to run index.php to get the button again....and i want button to present below the output.
For instance,

Fact 1
View =1
Button "Click Me"

Again if I click the button then it should look like this
Fact =2
View = 2
Button "Click Me"

I m not getting how to do this.....

Hope I am not confusing you guys...

Thanks
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Need Help

Post by McInfo »

rotavirus wrote:The session thing is working.....now the problem is....
when i click button then it shows...fact....and not button...
I want a button below the ouput and if the user click on that button than it should display other fact...
You are using two separate files. If you want the button to appear on every page view, either combine the code from index.php and count.php into a single file or use include.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 4:37 pm, edited 1 time in total.
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: Need Help

Post by akuji36 »

I think you need something like this NOT a session.

http://www.tizag.com/phpT/switch.php
rotavirus
Forum Newbie
Posts: 7
Joined: Thu May 28, 2009 5:40 pm

Re: Need Help

Post by rotavirus »

Thanks a ton guys....it was really helpful..... :D :D :D
Post Reply