Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to add PHP code with submit buttons into HTML?

    So I have this perfectly running copied code below:

    Basically it creates a box with a submit option in it, that stores info to a database. But I don't want the box and submit button in php. I want it in html.

    So the <form...> data is HTML


    PHP Code:
    <?php
    /*
    First, the server checks if the form has been submitted.
    When the page is first loaded this will return false and the form is displayed for them to fill out

    When the user clicks the submit button the form reloads the page and runs the PHP script again. This time the if statement returns true as the form HAS been submitted so it executes the part of the script that inserts the data in MSSQL and displays the successful message.
    */

    if (isset($_POST['submit'])){
        
        
    //connect to the database 
        
    $link mssql_connect('db','user','pass');    
        
    //display error if database cannot be accessed 
        
    if (!$link || !mssql_select_db('php'$link)) 
        {
            die(
    'Unable to connect or select database!');
        }
        
        
    //assign form input to variables
        
    $txtName $_POST['txtName'];
        
    $txtPass $_POST['txtPass'];
        
        
    //SQL query to insert variables above into table
        
    $sql " INSERT INTO customer ([Name],[Pass])VALUES('$txtName','$txtPass')";
        
    $result mssql_query($sql$link);
        
        
    //if the query cant be executed
        
    if(!$result)
        {
            echo 
    mssql_error();
            exit;
        }
        
        
    // close the connection
        
    mssql_free_result($result); 
        
    mssql_close();
        echo 
    "Data successfully inserted";
    }
    else { 
    ?>
        <form name="input" action="$_SERVER['PHP_SELF']" method="POST">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">                
                <tr>
                    <td colspan="3" height="10" valign="top" width="98%"></td>
                </tr> 
                <tr>
                    <td width="22%">Name:</td>
                    <td width="60%"><INPUT type="text" name="txtName" id="txtName" Width="200px" MaxLength="30" /> </td>
               </tr>
               <tr>
                    <td>Password:</td>
                    <td><INPUT type="text" name="txtPass" id="txtPass" width="200px" MaxLength="30" /></td>                 
               </tr>   
               <tr>
                    <td><input type="button" name="submit" value="submit" /></td>
               </tr>
            </table>
        </form>
    <?php ?>

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    PHP can produce HTML, if you want the files to be separate then you need the HTML form to point to the PHP file, or have the PHP file include an HTML file if you only care about keeping it that way on the server side

  • #3
    New to the CF scene
    Join Date
    May 2014
    Location
    Derby
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure why you'd want to searate them. The work much better in the php file. You are clear that the php only executes server side and is not seen in a browser. Only the genertated HTML appears here

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Allow me to re-explain what I meant.

    If you want users to go to HTML pages, but still allow for PHP when submitting data, you can point the form action to the PHP page, or use jQuery/ajax to submit and reveal the results. Either way, you'll most likely need PHP on your pages unless you're going to submit calls on page loads to get the data, which wouldn't be very efficient unless there's a specific reason why.

    If you want to have strictly HTML and PHP files separate on the server for designing purposes, but have them collected when needed for output, then that's your call, the user won't know how you set this up.
    For instance, I can create index.php, then echo file_get_contents('home.html'); or readfile('index.html'); to make it easier to modify files or make a template system, that's possible, but like I said, the user can't tell the difference. This is just all on the developers preference and file layout design.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •