Newbie
Newbie

Reputation: 57

Isset function for buttons in php code

How can I make the form not to run the php code directly when the database.php is executed. I can set an if clause that if isset then run.. but there is nothing in the form except the show button. Is there any way to test if the show button is set? what is the isset for buttons?

below is the code

database.php
<?php
        require 'core.inc.php';
        require 'conn.inc.php';


        if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id']))
        {
            $query= "SELECT *  FROM Properties";

            $query_run=mysql_query($query);

            if ($num=mysql_num_rows($query_run))
            {
                echo $num, " results found <br>";

                while ($query_row= mysql_fetch_assoc($query_run))
                {
                    echo "Found";
                }
            }
        }
    ?>      
    <html>
            <div >
                <form action='<?php echo $current_file;?>' method='POST'>
                <input type="submit" value="Show">
                </form>
            </div> 
    </html>

Upvotes: 0

Views: 3157

Answers (3)

Sarath Kumar
Sarath Kumar

Reputation: 2353

try this..

<?php
            require 'core.inc.php';
            require 'conn.inc.php';

       if (isset($_POST['submit'])) {
            if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id']))
            {
                $query= "SELECT *  FROM Properties";

                $query_run=mysql_query($query);

                if ($num=mysql_num_rows($query_run))
                {
                    echo $num, " results found <br>";
                    while ($query_row= mysql_fetch_assoc($query_run))
                      echo "Found";
                  }
               }
            }
        ?>      
        <html>
                <div>
                    <form action='<?=$_SERVER['PHP_SELF'] ?>' method='POST'>
                    <input type="submit" name="submit" value="Show">
                    </form>
                </div> 
        </html>

Upvotes: 3

Manmaru
Manmaru

Reputation: 578

Add a hidden input to your HTML inside the <form> like this:

 <input type="hidden" name="action" value="asdf" />

Then in your PHP you can check:

if (isset($_POST['action']) && $_POST['action'] === 'asdf') {
   // Something
} else {
   // Something else
}

Use this to branch your code logic, though if it gets really long I'd recommend using multiple files.

Upvotes: 0

Vipul Hadiya
Vipul Hadiya

Reputation: 882

Give a name to your submit button. e.g btnsubmit than check condition if(isset($_POST['btnsubmit'])). It must work, except this everything is pretty good with your code.

Upvotes: 0

Related Questions