Hello,

As in my other post, I have a php page in the form of

<?php
...
?>

<html>
...
</html>

Somewhere in the HTML part there is a form with a button:

<form method="POST">
<input type=SUBMIT action="<?php
			a set of php commands ?>" 
value="My Button">
</form>

As you may be able to tell I am trying to execute a set of PHP commands when My Button is clicked. However once the page is opened, the PHP commands are also executed. How can I implement this so that the PHP stuff is executed only when the button is clicked?

Thanks in advance.

    <form method="POST" action=''>
    <input type="submit" name="button1"  value="My Button">
    </form> 

    Notice name="button1" This is nessacary for what you want to do.

    This will execute the statement(s) when "My Button" is pressed

    if (isset($_POST['button1']))
    {
       echo "button 1 has been pressed";
    }

      Thanks for your help. Here's almost exactly what I have in the HTML part:

      <form method="POST" action=''>
                  <input type="SUBMIT" class="style19" name="use_button" value="something">
      </form>

      followed immediately by:

      <?php
      	if (isset($_POST['use_button'])){
      	$add_log = "INSERT INTO ****
      		VALUES ****";
      	$session_result = pg_query($add_log);}?>

      which is not working. Of course it doesn't function the same way it used to, but does not execute the php statements neither. Your help is appreciated.

      P.S> I've tried tuning on/off register_globals but no difference...

        I'm testing something so hold on please.

        Edit:
        It works for me http://www.sherrykern.com/comments/button.php

        <?php
        
        echo
        "<form action='' method='post'>
        <input type='submit' name='use_button' value='something' />
        </form>";
        
        if(isset($_POST['use_button']))
        {
        	echo "hey";
        }
        
        ?>

          you might also want to take note that in order for this to work a new request needs to be made to the server. php runs on the server, and there is no way to have it run on the client. you could try it the way you are going (and it should work), but its just the same as having your button point to a new script.

          one thing you might want to look at is ajax. it will hide the request to the server.

            thanks harmor and thorpe for the help. the piece of code is working when implemented outside my messy PHP-HTML code, so i'm going to just play around with the rest of the code to see what is causing the problem. thanks again!

              Please see the code below:

              <html>
              <style type="text/css">
              ... some code ...
              </style>
              </head>
              
              <?php
              $db = pg_connect("****") or die("Unable to connect to the database.");
              $sql = "SELECT * FROM reg_users where username like '$username' AND passwd like '$passwd'";
              $result = pg_query($sql);
              if (pg_num_rows($result) == 0){
                ?>
                <H2 class="style6">Access denied</H2>
                <span class="style6"><BR>
                Authorization failed.
                </span>
                <?php
                exit;}
              else{
              $currentdate = getdate(time());
              $time = $currentdate["hours"].":".$currentdate["minutes"];
              $date = $currentdate["mday"]." ".$currentdate["month"]." ".$currentdate["year"];
              $firstname = pg_fetch_result($result,firstname);
              ?>
              ... some tables ...
              
              W</font></span></font>elcome <?php echo $firstname?>!<br>
              
              ... some other stuff ...
              
                    <td width="108" height="52" align="left" valign="bottom"><form method="POST" action=''>
                      <input type="SUBMIT" class="style19" name="use_button" value="Use Rheometer"></form>
              		<?php
              		if (isset($_POST['use_button'])){
              		$add_session = "INSERT INTO sessions (username, date, time)
                			VALUES ('".$username."', '".$date."', '".$time."')";
              		$session_result = pg_query($add_session);
              		$add_log2 = "INSERT INTO web_log (username, action, date, time)
              		  VALUES ('".$username."', '".RH_TST."', '".$date."', '".$time."')";
              		$session_result2 = pg_query($add_log2);}?>
                    </td>
              
              ... some more stuff ...
              
              <p>&nbsp;</p>
              <?php
              }
              ?>
              </html>

              That is the actual code that I am working on. I have tried to diagnose it and concluded that this piece of code causes the problem:

              if (pg_num_rows($result) == 0){
                ?>
                <H2 class="style6">Access denied</H2>
                <span class="style6"><BR>
                Authorization failed.
                </span>
                <?php
                exit;}

              To remind you, the problem is that when the button "Use Rheometer" is pressed, the PHP command following it doesn't execute. Also there's login page which, if username/pass is correct, redirects the user to the above page. $username and $passwd are defined as global sessions in the logn page.

              Any help on how to make this work is greatly appreciated.

              P.S> I included every single PHP part of the entire code, just in case. But took out the HTML's and JS and CSS etc.

                As thorpe noted, keep in mind that PHP runs on the server, so it runs that button action code when it's putting the next page together - after repeating all that user access control stuff and HTML that precedes it.

                  thanks weedpack for the reply. I'm now getting confused... as I mentioned in my previous post the button thing works when the if-else piece of code (the statement that gives Access Denied when user/pass is incorrect) is not included in the page. so I don't know, maybe the if-statement needs revision (although i don't see why), etc. I'm totally lost.

                  P.S> and btw, i know that php runs on server and i'm not trying to run any php on the client side (at least as far as i can see).

                    Write a Reply...