Jump to content
Larry Ullman's Book Forums

kamaboko

Members
  • Posts

    13
  • Joined

  • Last visited

Posts posted by kamaboko

  1. Hello Hartley,

     

    I tried your suggestion.  In fact, I've been messing with this for quite some time now.  I've pasted Larry's code, along with how the MySQL database is set up.  I'm using PHP 5.5.12 and MySQL 5.6.17.   I get the following error:

     

    Could not add the entry because:

    The query being run was: INSERT INTO entries (entry_id, title, entry, date_entered) VALUES (0, 'test title','test text', NOW())

     

     

    Database:

    entry_id INT UNSIGNED NOT NULL AUTO_INCREMENT

    title VARCHAR(100) NOT NULL

    entry TEXT NOT NULL

    date_entered DATETIME

     

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Add a Blog Entry</title>
    </head>
    <body>
    <h1>Add a Blog Entry</h1>
    <?php // Script 12.5 - add_entry.php
    /* This script adds a blog entry to the database. */

    if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

        // Connect and select:
        $dbc = mysqli_connect('localhost', '**', 'password');
        mysqli_select_db('myblog', $dbc);
        
        // Validate the form data:
        $problem = FALSE;
        if (!empty($_POST['title']) && !empty($_POST['entry'])) {
            $title = trim(strip_tags($_POST['title']));
            $entry = trim(strip_tags($_POST['entry']));
        } else {
            print '<p style="color: red;">Please submit both a title and an entry.</p>';
            $problem = TRUE;
        }

        if (!$problem) {

            // Define the query:
            $query = "INSERT INTO entries (entry_id, title, entry, date_entered) VALUES (0, '$title', '$entry', NOW())";
            
            // Execute the query:
            if (@mysqli_query($query, $dbc)) {
                print '<p>The blog entry has been added!</p>';
            } else {
                print '<p style="color: red;">Could not add the entry because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
            }
        
        } // No problem!

        mysqli_close($dbc); // Close the connection.
        
    } // End of form submission IF.

    // Display the form:
    ?>
    <form action="add_entry.php" method="post">
        <p>Entry Title: <input type="text" name="title" size="40" maxsize="100" /></p>
        <p>Entry Text: <textarea name="entry" cols="40" rows="5"></textarea></p>
        <input type="submit" name="submit" value="Post This Entry!" />
    </form>
    </body>
    </html>

  2. Hello,

     

    I've coded up the exercises for Chapter 7, in particular, the one regarding "Larry Ullman's Books".  When I run it, I get an Array to String conversion error message.  I thought maybe I coded something wrong, so I downloaded the code from this site to double check it.  I get the same error.  Could this be a PHP version problem?  I'm using 5.5.12 with Apache version 2.4.9.

     

    Thanks,

    K

  3. Thanks for your replies.  I don't know what it is, but chapter 8 is absolutely killing me.  I've probably read it ten times, watched a ton YouTube videos on the subject, and the importance of tracking these events still mystifies me.  In particular, this code:

    function reportEvent (e) {
        'use strict';
    
        // Get the event object:
        if (typeof e == 'undefined') e = window.event;
    
        // Get the event target:
        var target = e.target || e.srcElement;
  4. Thanks.  This is on the top of my study list.  I thought .value pertained more to things like length or text input,  not search.  That's why it never occurred to me. 

     

    Where would you suggest I find more information on how to manipulate the DOM elements?  For instance, the point that you pointed out such as --- object.value.search.

     

    Thanks,

    K

  5. Hi Hartley,

     

    Sure...below is my html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset = "utf-8">
        <title></title>
    	<!--[if lt IE 9]>
    	   <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js."></script>
    	<![endif]-->
    </head>
    <link rel="stylesheet" href="css/calc.css">
    <body>
    <form action="#" method="post" id="stringTest">
    <fieldset><legend>String Test</legend>
         </div><label for="sentence">Sentence</label>
             <input type="text" name="sentence" id="sentence"></div>
    	<div><label for="keyword">Key Word</label>
            <input type="text" name="keyword" id="keyword"></div>	 		 
        <div><label for="output">Output</label>
             <input type="text" name="output" id="output"></div>    
        <input type="submit" value="Submit" id="submit"> 
    </fieldset>
    </form>
    <script type="text/javascript" src="js/stringTest.js"></script>
    </body>
    </html>
    
  6. Hello,

     

    I've tried to create a very simple input form to search for a specific word in a string.  That said, I'm not having much luck.  Firebug is alerting me of an exception error, and I can't figure out why.  The code is below.  The error msg says it's not a function.  

    function calculate() {
           'use strict';
    	   
    	   var output;
    	   var sentence = document.getElementById('sentence');
    	   var keyword = document.getElementById('keyword');
    	   
    	   if(sentence && (sentence.value.length > 0)){
    	      if(sentence.search(keyword) != -1){  //error msg here
    		     output = keyword + " is in the sentence";
    			 }else{
    			 output = "sorry, no word exists";
    		   }
    		  document.getElementById('output').value = output; 	  
    	  }  
    	  return false;
    }
    
    function init(){
           'use strict';
    	   document.getElementById('stringTest').onsubmit = calculate;
    }
    
    window.onload = init;
    

    I'm basing this on a smaller example I got to work, but it didn't involve form input.  The following is that example.

    var str = "this is a big dog";
    var regExp = "cat";
    if(str.search(regExp) != -1){
       document.write(regExp + " was found");
    }else{
       document.write(regExp + " was not found");
    }
    

    Comments appreciated.

     

    K

×
×
  • Create New...