Page 1 of 1

Need Help

Posted: Thu May 28, 2009 5:54 pm
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

Re: Need Help

Posted: Thu May 28, 2009 7:31 pm
by requinix
Look into "sessions".

Re: Need Help

Posted: Sat May 30, 2009 3:22 pm
by rotavirus
Thanks.....will look into session.....

Re: Need Help

Posted: Wed Jun 03, 2009 6:31 pm
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

Re: Need Help

Posted: Wed Jun 03, 2009 6:40 pm
by mikemike
Can we see what code you have now as there are any number of places you could be going wrong?

Re: Need Help

Posted: Thu Jun 04, 2009 12:38 pm
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

Re: Need Help

Posted: Thu Jun 04, 2009 12:44 pm
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.

Re: Need Help

Posted: Thu Jun 04, 2009 1:01 pm
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

Re: Need Help

Posted: Thu Jun 04, 2009 2:16 pm
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.

Re: Need Help

Posted: Fri Jun 05, 2009 3:40 pm
by akuji36
I think you need something like this NOT a session.

http://www.tizag.com/phpT/switch.php

Re: Need Help

Posted: Thu Jun 11, 2009 4:53 pm
by rotavirus
Thanks a ton guys....it was really helpful..... :D :D :D