Jump to content

Strider64

Members
  • Posts

    470
  • Joined

  • Last visited

  • Days Won

    12

Posts posted by Strider64

  1. A side note, most people get annoyed entering (or choosing) more than 8-10 fields on one page, so if the form is huge with a lot of input fields it is best to split the form and have it on multiple web pages. So that might give you an idea if you should separate the code in a separate file? 

  2. You really should have error reporting turned on and an IDE that will flag syntax errors (it'll make life much easier). I would also separate the query from the prepare.

     

    Here's an example of mine ->

    $query = 'INSERT INTO trivia_questions (question, answer1, answer2, answer3, answer4, correct, category, play_date) VALUES (:question, :answer1, :answer2, :answer3, :answer4, :correct, :category, NOW())';
    
    $stmt = $pdo->prepare($query);
    

    That way you can easily debug the script. Everyone who writes code gets syntax errors which are easily fixable and the less time you spend on them the more you can concentrate on the baddies (logic errors).  :  :happy-04:

  3. Trying to be nice as possible and not trying to sound like a poster on a different forum, but your HTML itself needs a lot of working itself. I believe having a firm grasp on HTML, CSS and JavaScript (Basic pure javascript) will make the learning curve easier. I agree with ginerjm to do the email form in php first then it will be simple to add JQuery or Javascript to the form.

     

    Here's a contact form that I did for a Github Repository: https://github.com/Strider64/html-contact-form

            <form id="contact" action="index.php" method="post"  autocomplete="on">
    
                <fieldset>
    
                    <legend><?php echo (isset($message)) ? $message : 'Contact Details'; ?></legend>
    
                    <label for="name" accesskey="U">Your Name</label>
                    <input name="name" type="text" id="name" placeholder="Enter your name" required="required" />
    
                    <label for="email" accesskey="E">Email</label>
                    <input name="email" type="email" id="email" placeholder="Enter your Email Address"  required="required" />
    
                    <label for="phone" accesskey="P">Phone <small>(optional)</small></label>
                    <input name="phone" type="tel" id="phone" size="30" placeholder="Enter your phone number" />
    
                    <label for="website" accesskey="W">Website <small>(optional)</small></label>
                    <input name="website" type="text" id="website" placeholder="Enter your website address" />
    
                </fieldset>
    
                <fieldset>
    
                    <legend>Your Comments</legend>
    
                    <div class="radioBlock">
                        <input type="radio" id="radio1" name="reason" value="support" checked>
                        <label class="radioStyle" for="radio1">support</label>
                        <input type="radio" id="radio2" name="reason" value="advertise">
                        <label class="radioStyle" for="radio2">advertise</label>  
                        <input type="radio" id="radio3" name="reason" value="error">
                        <label class="radioStyle" for="radio3">Report a Bug</label>    
                    </div>
    
                    <label class="textBox" for="comments">Comments</label>
                    <textarea name="comments" id="comments" placeholder="Enter your comments" spellcheck="true" required="required"></textarea>             
    
                </fieldset>
    
                <input type="submit" name="submit" value="submit">
    
            </form>
    

    and here's the css to the form:

    form#contact {
        display: block;
        width: 100%;
        max-width: 800px;
        height: auto;
        background-color: #ebdb8d;
        padding: 20px;
        margin: 0 auto 20px;
    }
    
    form#contact fieldset {
        border: 2px solid #2e2e2e;
        padding: 30px;
        margin-bottom: 20px;
    }
    
    form#contact fieldset legend {
        font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
        font-size: 1.4rem;
        color: #2e2e2e;
        padding: 0 5px;
    }
    
    form#contact fieldset label {
        float: left;
        display: block;
        width: 100%;
        max-width: 220px;
        height: 30px;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 1.2rem;
        line-height: 30px;
        color: #2e2e2e;
        text-align: right;
        padding: 0 10px;
    }
    
    form#contact fieldset input {
        clear: right;
        display: block;
        width: 100%;
        max-width: 250px;
        height: 30px;
        border: none;
        outline: none;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 1.2rem;
        color: #2e2e2e;
        padding: 0 5px;
        margin-bottom: 10px;
    }
    
    form#contact fieldset .radioBlock {
        display: block;
        width: 100%;
        max-width: 100%;
        height: auto;
        margin: 0 auto;
    }
    
    form#contact fieldset input[type=radio] {
        display: none;
        margin: 10px;
    }
    
    form#contact fieldset input[type=radio] + label.radioStyle {
        display: inline-block;
        width: 33.33333333333%;
        height: 45px;
        margin: -2px;
        text-align: center;
        text-transform: capitalize;
        cursor: pointer;
        color: #fff;
        background-color: #000;
        border: 2px solid #ffa;
        line-height: 45px;
        margin: 10px auto 0 auto;
    }
    
    form#contact fieldset input[type=radio] + label.radioStyle:hover {
        background-color: #aaa;
        color: #ffa;
    }
    
    form#contact fieldset input[type=radio]:checked + label.radioStyle {
        background-image: none;
        color: #ffa;
        background-color: #aaa;
    }
    
    form#contact fieldset label.textBox {
        clear: both;
        text-align: left;
        padding: 0;
        margin-top: 20px;
    }
    
    form#contact fieldset textArea#comments {
        resize: none;
        border: none;
        outline: none;
        clear: both;
        display: block;
        width: 100%;
        max-width: 660px;
        height: 400px;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 1.2rem;
        padding: 10px;
    }
    
    form#contact input[type=submit] {
        outline: none;
        border: none;
        display: block;
        width: 100%;
        max-width: 120px;
        height: 40px;
        cursor: pointer;
        border: 3px solid #ffa;
        background-color: #000;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 1.2rem;
        color: #fff;
        text-transform: capitalize;
        margin-left: 640px;
    }
    
    form#contact input[type=submit]:hover {
        color: #ffa;
        background-color: #aaa;
    }
    

    Granted it's not a fancy styling with CSS, but it could be easily modified to be so.  A great website site to practice CSS is : http://www.csszengarden.com/

     

    HTH JOHN 

     

    P.S. There are a lot of great websites and books on HTML/CSS, plus some of them are even free. 

  4. You would be better to go to php.net (The direct source for php into) and check over the functions. People even give examples on them. 

     

    They (php.net) even provide you where you can download a free database to check MySQL functions (use PDO or mysqli) and it called world.sql (I believe).

     

    What I do is have a folder (I call it phpSandbox) where I put all my learning and testing scripts in.

     

    Here's just one of many scripts I have written over the years.

    <?php
    include 'lib/includes/connect/connect.php';
    
    $db_options = [
        /* important! use actual prepared statements (default: emulate prepared statements) */
        PDO::ATTR_EMULATE_PREPARES => false
        /* throw exceptions on errors (default: stay silent) */
        , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        /* fetch associative arrays (default: mixed arrays)    */
        , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
    ];
    if (filter_input(INPUT_POST, submit)) {
    
        $pdo = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=world;charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
    
        $query = 'SELECT Name, CountryCode, District, Population FROM city WHERE CountryCode=:CountryCode ORDER BY District'; // Set the Search Query:
    
        $stmt = $pdo->prepare($query); // Prepare the query:
    
        $result = $stmt->execute([':CountryCode' => filter_input(INPUT_POST, countryCode)]); // Execute the query with the supplied user's parameter(s):
    }
    ?>
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Database Search</title>
            <link rel="stylesheet" href="lib/css/reset.css">
            <link rel="stylesheet" href="lib/css/grids.css">
            <link rel="stylesheet" href="lib/css/searchstyle.css">
        </head>
        <body>
            <div class="container seachBackground">
                <form id="searchForm" action="search.php" method="post">
                    <label for="searchStyle">search</label>
                    <input id="searchStyle" type="text" name="countryCode" value="" placeholder="Enter Country Code (For Exampe : USA)..." tabindex="1" autofocus>
                    <input type="submit" name="submit" value="submit" tabindex="2">
                </form>
            </div>
            <div <?= $result ? 'class="turnOn" ' : 'class="turnOff" '; ?>>
                <table id="search" summary="Cities Around the World!">
                    <thead>
                        <tr>
                            <th scope="col">City</th>
                            <th scope="col">Country Code</th>
                            <th scope="col">District / State</th>
                            <th scope="col">Population</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if ($result) {
                            while ($record = $stmt->fetch()) {
                                echo "<tr>";
                                echo '<td>' . $record->Name . "</td>";
                                echo '<td>' . $record->CountryCode . "</td>";
                                echo '<td>' . $record->District . "</td>";
                                echo '<td>' . $record->Population . "</td>";
                                echo "</tr>";
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
    
        </body>
    </html>
    

    I personally would NOT go to a forum and grab scripts that other people are asking help on, for you don't know where exactly they are at with their problem(s) or that if they are committed in resolving their problem. I have seen many people who post for help on their problems and never acknowledge they have resolved the issue. Do you really want to copy a script from someone like that? I know I wouldn't. Another thing that I have learned over the last couple of years, is while you might belong to many help forums the key to getting a resolution is to post on only ONE forum and have plenty of patience. People who answer you questions are doing out of their desire to help people and they too have other important things that have to get done first. Someone will eventually answer you question. One last thing make your topic relevant to your problem, something like "I am having a problem with updating in PHP PDO?". While that might even be a little vague, it's way better than "HELP! I Have a Problem". 

  5.         $query = "SELECT count(1) FROM myTable WHERE score > :user_score";
            $stmt = $pdo->prepare($query);
    
            $stmt->execute([':user_score' => $user_score]);
            $ranks = $stmt->fetchColumn();
    

    Why not simply do something like the above? (Pull the score separately from the query is what I would do) 

     

    That might have to be >= rather than > ? 

  6. I think you're confusing what is clickable and what is not:

    https://jsfiddle.net/Strider64/db8NB/

     

    While that isn't the same thing it should show you what I'm talking about.

    $(function () {
    
        var myButton1 = $('.myButton1'), // The Button:
            box1 = $('.box1'),
            myButton2 = $('.myButton2'),
            box2 = $('.box2');
    
        myButton1.on('click', function () {
    
            $(this).toggleClass('off');
            if ($(this).hasClass('off')) {
                moveRight(); // Animate Box to the Right:
            } else {
                moveLeft(); // Move Box back to the Original Position:
            }
        });
    
        myButton2.hover(function () {
            box2.animate({
                'left': '300px'
            });
            $(this).css('background-color', 'orange');
        }, function () {
            box2.animate({
                'left': 0
            });
        });
    
        function moveLeft() {
            box1.animate({
                'left': 0
            });
        }
    
    
        function moveRight() {
            box1.animate({
                'left': '300px'
            });
        }
    
  7. I also add that php's DateTime Class (server side) is more robust in my opinion in handling date and time. I simple little Ajax script to pull that into JavaScript and display (maybe a little fine tuning with JS). 

  8. What I do is put all my output as arrays and send them down to a json function and any errors are thrown to an error function then to output.

     

    Like so ->

    <?php
    
    require_once 'lib/includes/utilities.inc.php';
    
    use website_project\trivia_game\OutputQA;
    
    /* Makes it so we don't have to decode the json coming from JQuery */
    header('Content-type: application/json');
    
    $gamePlay = new OutputQA();
    
    $id = filter_input(INPUT_POST, 'id');
    
    if (isset($id)) {
        $data = $gamePlay->readQA($id);
        if ($data) {
            $output = $data[0];
            output($output);
        } else {
            $output = 'eof';
            errorOutput($output, 410);
        }
    }
    
    $q_num = filter_input(INPUT_POST, 'q_num');
    $answer = filter_input(INPUT_POST, 'answer');
    
    
    if (isset($q_num) && isset($answer)) {
    
        $result = $gamePlay->checkDailyTen($q_num, $answer);
    
        if ($result) {
            output($result);
        } else {
            $output = ['eof' => TRUE, 'message' => 'There are no more questions'];
            errorOutput($output, 410);
        }
    }
    
    /*
     * Set error code then send message to Ajax / JavaScript
     */
    
    function errorOutput($output, $code = 500) {
        http_response_code($code);
        echo json_encode($output);
    }
    
    /*
     * If everything validates OK then send success message to Ajax / JavaScript
     */
    
    function output($output) {
        http_response_code(200);
        echo json_encode($output);
    }
    

    It just something that keeps me thinking straight....it might help  :confused:

  9. There a plenty of books that just deal with design patterns, though boring can be useful.  An author like Larry Ullman a good PHP author can show you how to get going in OOP and I know Ullman will even recommend a few of these other authors of design patterns.

     

    What I do with something that you are trying to implement is break it down to its simplest core. In this case you want to write a user's registration. Well that requires interactivity with a database table of some sort. The first thing you will have to do is Create the db Table, next it would be nice to be able to Read from the db Table, after that it would be nice to be able to edit certain fields so you want to be able to Update the db Table, and finally you want to give the administrator (and maybe the user themselves) the ability to Delete the record for the db Table.

     

    So what do we have? We have CRUD and stick a interface on it then you get iCRUD.

    <?php
    namespace login_project\database;
    
    /* The iCRUD interface.
     * The interface identifies four methods:
     * - create()
     * - read()
     * - update()
     * - delete()
     */
    interface iCRUD {
    
      public function create($data);
    
      public function read();
    
      public function update($data);
    
      public function delete($id=NULL);
      
    }
    

    While this a basic interface that I am sure pales in comparison to other interfaces, the purpose of it to force you to build your class in a precise manner and you'll find that it can be used for other than an user's registration class. If you have a good IDE then you have an added bonus for it will give you hints and turn green on methods that you have correctly implemented in your class. Though like I said Larry Ullman or other authors of design patterns will be able to you better explanation than I. 

  10. I agree with the above for that a good way in not getting trip up with boolean comparisons. A simple trick that I do when writing if statements is I use === instead of == that way if I forget an equal sign it'll still work (someone told me this a long time ago when I was asking for help). This will work 95% of the time, but there are some instances that you can't use a direct comparison (exactly the same value) and in the situation you need either to use the == or === depending in whatcha doing, but I really haven't found that to be a hiccup. I don't do much regex and I am guessing that would be where that comes into play?. 

  11. <?php 
    $timestamp = strtotime($_GET['time_input']) + 60*60;
    $time = date('H:i', $timestamp);
    echo $time;
    ?>
    

    $_GET['time_input'] will be the name of your textbox in your form.

     

    If the date is coming from a form most people use post for it's more secure, so it would be better to use $_POST['time_input'].  Though to be honest I have I'm still a little confused on the original poster's post and this in what the OP means set the time in the form? Does it want to be incremented one hour before being made a selection in the form (giving the user an option) or be already set? 

  12. You could manipulate the css with javascript, but why would you? Answer: You basically wouldn't unless you wouldn't, but there are small exceptions to the rule (Like developing an online quiz). It also cumbersome to use JavaScript to modify the css, when it can be directly changed in css and I would bet most developers add/remove css classes (or ids) than manipulate the css directly in javascript.  

  13. I been using Netbeans since I was mainly a PC user, but I switched over to the iMac and found it even easier to setup. The one downfall that I found writing PHP under Windows is that Windows isn't unix based thus setting up Netbeans (more like the local server in setting it up) takes a little bit of workarounds and/or knowing a little bit of DOS (Something I knew since DOS 3.1 LOL ::) ). However, once you get it setup there should be no problems and things become second nature; however, I found setting up a website on a remote server sometimes a little baffling until I remember I design/developed on a Windows based computer.  :happy-04:

  14. If you want to figure out duration of time you need actually timestamps like already stated or the Date and Time. 

     

    Look into the DateTime class at php.net for it has a lot of built in functions that can be used, so you don't have to reinvent the wheel.

     

    Here's an example -> 

    <?php
    date_default_timezone_set("America/Detroit");
    $start = '2016-09-11 12:45:00';
    $end = '2016-09-13 12:59:52';
    
    $date1 = new \DateTime($start);
    echo $date1->format("F d, Y");
    
    $date2 = new \DateTime($end);
    $diff = $date1->diff($date2);
    
    echo "<pre>" . print_r($diff, 1) . "</pre>";
    
    
  15. I personally would use true instead of false for I find it easier to comprehend and it might be helpful just to have a security access level that grants permissions to the user to do certain things based on their security level.

     

    For example this is my login script for my website(s):

            $db = DB::getInstance();
            $pdo = $db->getConnection();
            /* Setup the Query for reading in login data from database table */
            $this->query = 'SELECT id, username, password, security_level, first_name, last_name, email, home_phone, cell_phone, gender, birthday FROM users WHERE username=:username';
    
            try {
                $this->stmt = $pdo->prepare($this->query); // Prepare the query:
                $this->stmt->execute([':username' => $data['username']]); // Execute the query with the supplied user's parameter(s):
            } catch (Exception $ex) {
                die("Failed to run query: " . $ex->getMessage()); // Do Not Use in Production Website - Log error or email error to admin:
            }
    
            $this->stmt->setFetchMode(PDO::FETCH_OBJ);
            $this->user = $this->stmt->fetch();
    
            if ($this->user) {
                $this->loginStatus = password_verify($data['password'], $this->user->password); // Check the user's entry to the stored password:
                unset($data['password'], $this->user->password); // Password(s) not needed then unset the password(s)!:
            } else {
                return FALSE;
            }
    
            if ($this->loginStatus) {
                $_SESSION['user'] = $this->user; // Set the session variable of user:
                return TRUE;
            } else {
                return FALSE;
            }
    

    If the user's credentials check out then I simple put $_SESSION['user'] in my configuration file like this:

    /* Use $user for sessions variable */
    $user = isset($_SESSION['user']) ? $_SESSION['user'] : NULL;
    

    Then I can simple do this I want to grant access to a certain security level like this:

    if ( $user && $user->security_level === 'member") {
      /* Write code here for user with the security level here */
    }

    The possibilities are endless

     

    another example:

    if ( $user && $user_security_level !== 'member' ) {
      header('Location: index.php'); // Sorry none members not allowed:
      exit();
    }
  16. I think I have found the reason for the 500 error if nothing else, my host only supports up to php 5.4 so the verify and hash are not supported but that's another issue. but the above code is running but I am still testing the is_valid you suggested.

    You can download the password_hash/password_verify for PHP 5.3.7 and PHP 5.4 https://github.com/ircmaxell/password_compat

×
×
  • 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.