Jump to content

forms submit and variables. Not sure what else to call this.


CB150Special

Recommended Posts

I was hoping to make a generalized footer for my pages. I though this was working but the SQL update is never run. Can it be made to run ? I probably have a concept of HTML wrong. !! Close but no cigar.

    <body>
        <?php 
        require ('sql_get_data.php');
        if (isset($_POST['submit'])){
            require ('sql_update_mem.php');
        }   
        ?>  
        <div style="text-align: center;">
            <form id="b_data" action="" method="post">
                 <input type='text' name='MemName' value='<?php echo $row_data['MemName'] ;?>' />
            </form>
            <form id="b_return" action="index.php" method="post">
            </form>   
        </div>
        <div id='footer'>
            <button id='button' form="b_return" type="submit">Return</button>     
            <button id='button' form="b_data" name='submit' type="submit">Update</button>
        </div>  
    </body>

Thanks.

 

Link to comment
Share on other sites

Hello CB150Special,

In those few lines of code there are many things that could be said. I will state only one of them. ID's in HTML should be unique , you have two buttons with id “button” . Fix this and if your problems stay we can see it further.

When we have a problem in a core understanding its easier to create a test project with only the parts needed. For example here you state that isset($_POST['submit']) is false, so make a simple test with only this (no db , nothing else included that we don't see) , and if this doesn't work post it here to let us figure out why. 

Link to comment
Share on other sites

you can make your development of this page simpler by placing all of your html in a once place in your script (near the end!) and then doing all the php work starting from the top.  Logic that is meant to build parts of the output page should generate the html code into one or more php vars that are then place in the block of html code.  Makes it easier to write the html, easier to read and much easier to maintain.

Link to comment
Share on other sites

CB150Special: You've been told at least three times by different people to escape variables. E – s – c – a – p – e. If you're unable to remember this information, then write it on a post-it note and stick that on your monitor. Making a mistake is fine. If it happens again, well, maybe you aren't the fastest learner. But when you make the exact same mistake over and over and over again without showing any sign of progress, there's something wrong.

 

As to the rest of the code: Don't try to come up with your own fancy structure. Constantly switching between HTML markup and PHP business logic is just bad, and putting individual queries into separate scripts makes even less sense. Use the standard structure described by ginerjm.

<?php

// the business logic (queries etc.) goes here
$name = 'test';

?>
<!-- the HTML markup goes here -->
<!DOCTYPE html>
<html>
    <head>
        <title>An HTML standard template</title>
        <meta charset="utf-8">
    </head>
    <body>
        <!-- an escaped(!) PHP variable -->
        <?= html_escape($name, 'UTF-8') ?>
    </body>
</html>
<?php

/**
 * HTML-escapes a string so that it can safely be included in an HTML document
 *
 * @param string $raw_input the string which should be escaped
 * @param string $encoding  the character encoding of the target document
 *
 * @return string the encoded string
 */
function html_escape($raw_input, $encoding)
{
    return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE, $encoding);
}

If you want a smarter approach with markup reuse etc., then use a template engine like Twig. This will also help you with the HTML-escaping.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.