'HTML and PHP in one file

I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.

If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.

My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?



Solution 1:[1]

OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION


script1.php
<form action="script2.php" method="post" name="myform">
    ...
    <input type="submit" name='submit_button' value="Submit" />
<input
</form>

---------------
script2.php

include 'script1.php';

if(isset($_POST['submit_button']
{.......}

Solution 2:[2]

NO you dont put your php scripts on the same page as your html file/s

Try this link for your reference =)

Solution 3:[3]

Yeah You can put html and php in single document. With the help of action.But it not the proper way. In action you should mention this for writing html and php in same page.

<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>

Solution 4:[4]

You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.

If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.

e.g. Your file: script.php

<?php
if(isset($_POST['btnsubmit'])) {
    // Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
    ...
    <input type="submit" name="btnsubmit" value="Submit" />
<input
</form>

Solution 5:[5]

What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly

 header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...

Or another URI

header( 'Location: http://some.url/' );

Solution 6:[6]

If you really want to do this, here is a way:

<?php
        if(isset($_POST)){

        //do your php work here

       }
?>


<html>
<form method='POST'>
   //form elements here
   <input type='submit'>
</form>
<!-- other html code -->
</html>

Solution 7:[7]

It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdullah Aden
Solution 2 Kim Oliveros
Solution 3 user3296575
Solution 4 Ashwin Parmar
Solution 5 WASasquatch
Solution 6 Dan Jay
Solution 7