0

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>
1
  • I don't entirely understand what you want to happen when, but if you give your button a name, name="showbutton", then you should be able to check isset($_POST['showbutton']) to see if it was clicked, i.e. the form was submitted Commented Dec 1, 2014 at 9:23

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

3 Comments

Why ===? Shouldn't == be enough? (Don't get me wrong, I'm just curious)
Why should i have two separate $_POSTs?
== would be enough in this case, but it is good practice to use === when comparing strings in PHP that you are certain will not contain numbers. Newbie I don't understand your question, there is only one $_POST, you are just including an extra <input> tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.