Jump to content
Larry Ullman's Book Forums


  • Posts

  • Joined

  • Last visited

  • Days Won


Everything posted by Wagtail

  1. Ok, thanks Larry. I think I'll first have to understand Yii a bit more before trying to integrate it with jQuery Mobile. Cheers!
  2. Hi Larry and forum members, how are you all? I am learning the Yii framework and am wondering if it is possible to integrate Yii with jQuery Mobile? Do you think this can be done? Thank you for your time!
  3. HartleySan, thank you very much for getting back to me. I think I'll need to use a similar technique for the time being. I appreciate your help and hope you're well. Cheers.
  4. Hello forum members, here is a link to Stack Overflow which demonstrates how to use prepared statements with the IN operator: http://stackoverflow.com/questions/11938876/mysqli-prepared-statements-with-in-operator Could someone please have a look at the post (the first answer)? Would you recommended this technique or should I use something else? Thank you!
  5. Thanks HartleySan, I really appreciate you answering all of my questions. It's nice to get help from someone with your level of expertise. My "coding skills" are a lot better than what they were a year ago (at least I hope so), but there's still so much more to learn. I sometimes find it difficult to keep track of all of these functions and methods and classes etc etc.. Anyway, let me get on with it. Thanks again for helping me
  6. Hi HartleySan, thanks for replying. Thank you, I'll give that a try. Yes, but why is a request to the same page a bad thing? Should I always send an Ajax request to another PHP file? I'm wondering what to do about sessions times when a user has JavaScript disabled. Must I keep the script, for example, add_cart.php in my existing file and place a copy of it in an external file for the Ajax request? How can I ensure that both scripts are the same should I make a change in one? Different variable or database column names etc.. The external PHP file might not have access to all of the variables that have been defined in the existing file. Must I send all of this info with the Ajax request? Sorry for all of these questions. Thanks again.
  7. Hello forum members, I have a form which has a select menu, allowing users to update the quantities in their shopping cart, similar to the E-Commerce example in chapter 19. Now I'd like to add Ajax functionality to the page. I have tried the $.ajax() and .load() methods but haven't had any luck so far. Surely I can use these Ajax methods to post data to the same page, so that a page load isn't necessary? Any ideas will be more than welcome. Thank you.
  8. Cool, that seems to work. I didn't know about that method. Thank you for helping me.
  9. Thanks for all of the info. If I do an Ajax request to another PHP script, as in chapter 15, do you know how I should handle the header function? Instead of: if (response == 'CORRECT') { // Hide the form: $('#login').hide(); // Show a message: $('#results').removeClass('error'); $('#results').text('You are now logged in!'); } I would need to do: if (response == 'CORRECT') { header('location: /mysite/$name/$id'); exit (); } But clearly this won't work? I am a bit lost... Thank you for your help.
  10. Hi HartleySan, thanks for getting back to me. Looking at the console, the response I get back is the complete page and not a specific string such as "INVALID_EMAIL". When I do make a request to another PHP file, then a string is returned. The reason why I want to make a request to the same page is because once the form has been successfully filled out, I need to call the header function, which includes variables that aren't present in the external PHP file. Thank you for helping me. My setup looks like this: <!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>Login</title> <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" /> <script type="text/javascript" src="js/jquery-1.6.1.min.js" charset="utf-8"></script> </head> <body> <!-- Script 15.8 - login.php --> <?php if (isset($_GET['email'], $_GET['password'])) { // Need a valid email address: if (filter_var($_GET['email'], FILTER_VALIDATE_EMAIL)) { // Must match specific values: if ( ($_GET['email'] == 'email@example.com') && ($_GET['password'] == 'testpass') ) { // Set a cookie, if you want, or start a session. // Indicate success: echo 'CORRECT'; header('location: /mysite/$name/$id'); exit (); } else { // Mismatch! echo 'INCORRECT'; } } else { // Invalid email address! echo 'INVALID_EMAIL'; } } else { // Missing one of the two variables! echo 'INCOMPLETE'; } ?> <h1>Login</h1> <p id="results"></p> <form action="login.php" method="post" id="login"> <p id="emailP">Email Address: <input type="text" name="email" id="email" /><span class="errorMessage" id="emailError">Please enter your email address!</span></p> <p id="passwordP">Password: <input type="password" name="password" id="password" /><span class="errorMessage" id="passwordError">Please enter your password!</span></p> <p><input type="submit" name="submit" value="Login!" /></p> </form> <script> $(function() { var url = window.location.pathname; // Hide all error messages: $('.errorMessage').hide(); // Assign an event handler to the form: $('#login').submit(function() { // Initialize some variables: var email, password; // Validate the email address: if ($('#email').val().length >= 6) { // Get the email address: email = $('#email').val(); // Clear an error, if one existed: $('#emailP').removeClass('error'); // Hide the error message, if it was visible: $('#emailError').hide(); } else { // Invalid email address! // Add an error class: $('#emailP').addClass('error'); // Show the error message: $('#emailError').show(); } // Validate the password: if ($('#password').val().length > 0) { password = $('#password').val(); $('#passwordP').removeClass('error'); $('#passwordError').hide(); } else { $('#passwordP').addClass('error'); $('#passwordError').show(); } // If appropriate, perform the Ajax request: if (email && password) { // Create an object for the form data: var data = new Object(); data.email = email; data.password = password; // Create an object of Ajax options: var options = new Object(); // Establish each setting: options.data = data; options.dataType = 'text'; options.type = 'get'; options.success = function(response) { // Worked: if (response == 'CORRECT') { // Hide the form: $('#login').hide(); // Show a message: $('#results').removeClass('error'); $('#results').text('You are now logged in!'); } else if (response == 'INCORRECT') { $('#results').text('The submitted credentials do not match those on file!'); $('#results').addClass('error'); } else if (response == 'INCOMPLETE') { $('#results').text('Please provide an email address and a password!'); $('#results').addClass('error'); } else if (response == 'INVALID_EMAIL') { $('#results').text('Please provide your email address!'); $('#results').addClass('error'); } }; // End of success. options.url = url; // Perform the request: $.ajax(options); } // End of email && password IF. // Return false to prevent an actual form submission: return false; }); // End of form submission. }); // End of document ready. </script> </body> </html>
  11. Hello forum members, I am using Ajax with one of my forms after having gone through chapter 15: Introducing jQuery. Script 15.10 - login.js makes a request to a PHP file - login_ajax.php. The Ajax options include this line: options.url = 'login_ajax.php'; Instead of making a request to another PHP file, I need to make a request to the same PHP page, which includes the login_ajax.php script. I'm using URL rewriting and so the options.url line looks like: options.url = '/mysite/somedirectory/blah'; Is this correct? I've kept the other Ajax options as they are, for example, options.type = 'get'; So far this isn't working - I don't get any response when the submit button is clicked. What might the problem be? Please let me know if you require more info. Thank you for any help.
  12. Hi HartleySan, Thank you for your quick response, it appears to be working now. I must say that creating and using functions does trip me up sometimes, I guess I need to practise a lot. Anyway, thanks again and I hope you're having a good week. Cheers.
  13. Hi Larry and HartleySan, would one of you know how I can make a variable available outside of the make_text_input function? For example, how would I be able to access the $filtered array outside of the function? The function should validate every text input and then assign said variable to the $filtered array. Then I can do something like this outside of the make_text_input function: if (!empty($filtered['first-name']) && !empty($filtered['last-name'])) { // create email body and send email } Thank you in advance. function make_text_input($name, $label, $errormsg) { if (isset($_POST[$label])) { $filtered = array(); $errors = array(); $filtered[$label] = filter_var($_POST[$label], FILTER_SANITIZE_STRING); if (!$filtered[$label]) { $errors[$label] = $errormsg; } } }
  14. Hello everyone, this is a quick question about the PHP include() function. Is it possible to pull in specific sections of an include file? Such as: include ("inc/menu.php #div1"); The following code snippets are both in the same include file but they need to be called separately. Should I wrap these in functions? The table can probably be assigned to a variable. # Script 10.5 - #5 // Determine the sort... // Default is by registration date. $sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd'; // Determine the sorting order: switch ($sort) { case 'ln': $order_by = 'last_name ASC'; break; case 'fn': $order_by = 'first_name ASC'; break; case 'rd': $order_by = 'registration_date ASC'; break; default: $order_by = 'registration_date ASC'; $sort = 'rd'; break; } echo '<table align="center" cellspacing="0" cellpadding="5" width="75%"> <tr> <td align="left"><b>Edit</b></td> <td align="left"><b>Delete</b></td> <td align="left"><b><a href="view_users.php?sort=ln">Last Name</a></b></td> <td align="left"><b><a href="view_users.php?sort=fn">First Name</a></b></td> <td align="left"><b><a href="view_users.php?sort=rd">Date Registered</a></b></td> </tr> '; Thanks in advance!
  15. Great, that should make things a lot easier. Thank you for your prompt response and I hope you have a good weekend. Cheers.
  16. Hi HartleySan, how are you doing? I have a question about URL rewriting. Hope you can give me some advice. The names in my URLs are separated with dashes, for example, /world-football-association/. I then use str_replace() to replace the dashes with spaces before I use the names in a database query. But what about those names in the database that do have dashes between them? Must I first loop all of the names into an array and then not run the str_replace() function on those that do have dashes as a separator? Or is there some other technique that I should use? Thank you for your time!
  17. Hi Larry, thank you for replying to my post. Sorry, you are right about that, it should be 'firstname'. However, I did include my validation code $scrubbed[$label] = filter_var($_POST[$label], FILTER_SANITIZE_STRING); if ( ! $scrubbed[$label]) { $errors[$label] = $errormsg; } Not sure if I should do something like this: // Clean the form data: $scrubbed = array_map('spam_scrubber', $scrubbed); // Minimal form validation: if ($scrubbed[$label] == $scrubbed['firstname']) { Do you think that it is possible to move the final validation code outside of the "make_text_input" function - the part between // Clean the form data and // Clear $scrubbed (so that the form's not sticky)? Otherwise the confirmation message and sending of email are repeated for each text input. It would be great if this could work! Thank you.
  18. Hello everyone, I'm using a function to create all of my text inputs. The problem is with the final form validation which entails creating and sending the email. When I submit the form, an error message appears "Undefined index: first-name". This is most likely because I'm using, for example, $scrubbed[$label] and not $scrubbed['first-name']. I've also noticed that the confirmation message "Thank you for contacting me. I will reply some day." appears multiple times - once for each form input. Most likely the email is also sent multiple times. Does anyone know how I should perform the final form validation? Thank you for any help! Sources: CH10 create functions - Script 10.3 - sticky2.php php for the web visual quickstart guide 4e CH13 security methods - Script 13.1 - email.php #2 phy and mysql for dynamic websites visual quickstart4e <?php function spam_scrubber($value) { // List of very bad values: $very_bad = array('to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:'); // If any of the very bad strings are in // the submitted value, return an empty string: foreach ($very_bad as $v) { if (stripos($value, $v) !== false) return ''; } // Replace any newline characters with spaces: $value = str_replace(array( "\r", "\n", "%0a", "%0d"), ' ', $value); // Return the value: return trim($value); } // End of spam_scrubber() function. function make_text_input($name, $label, $errormsg) { if (isset($_POST[$label])) { $scrubbed = array(); $errors = array(); $scrubbed[$label] = filter_var($_POST[$label], FILTER_SANITIZE_STRING); if ( ! $scrubbed[$label]) { $errors[$label] = $errormsg; } // Clean the form data: $scrubbed = array_map('spam_scrubber', $scrubbed); // Minimal form validation: if ($scrubbed['first-name']) { // Create the body: $body = "Name: {$scrubbed['name']}\n\n"; // Make it no longer than 70 characters long: $body = wordwrap($body, 70); // Send the email: mail('your_email@example.com', 'Contact Form Submission', $body, "From: {$scrubbed['email']}"); // Print a message: echo '<p><em>Thank you for contacting me. I will reply some day.</em></p>'; // Clear $scrubbed (so that the form's not sticky): $scrubbed = array(); $errors = array(); } } print '<label>' . $name . ':</label> '; if (isset($errors[$label])) echo $errors[$label]; echo'<input id="' . $label . '" name="' . $label . '" value="'; if (isset($scrubbed[$label])) echo $scrubbed[$label]; echo'" />'; } make_text_input('First Name', 'firstname', 'please enter your first name' ); make_text_input('Last Name', 'lastname', 'please enter your last name'); make_text_input('Email', 'email', 'please provide your email address');
  19. Hello forum members, I realize that the book PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition) does not cover URL rewriting, but I hope I can still get some advice on this technique. I can have an URL in my HTML such as: http://www.mystore.com/France/Paris which gets redirected to: http://www.mystore.com/index.php?country=France&city=Paris But what must I do about a pagination link such as: // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="index.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . ''; if(isset($city)) { echo'&city='.$city.''; } echo'&sort='.$sort.'">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. Surely I can't put the above code into my .htaccess file? Thank you in advance for any help.
  20. Hi Larry and HartleySan, thank you for assisting me with this and I really do appreciate the help. It's all working fine now.
  21. Ok, the query below in phpmyadmin returns no rows: SELECT f.forum_name FROM forums AS f WHERE f.forum_name = 'larry's forum' The message is: MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0012 sec ) This query works fine: SELECT f.forum_name FROM forums AS f WHERE f.forum_name = 'larry\'s forum' Thank you!
  22. Ok, let's forget about mysqli_real_escape_string for the moment. If I use FILTER_SANITIZE_STRING the apostrophe becomes '. Having a query such as $q = "SELECT ... WHERE name = 'larry's forum' "; doesn't work. At least not when I tried it in phpmyadmin. I need the apostrophe in the query.
  • Create New...