Jump to content

HenryCan

Members
  • Content Count

    48
  • Joined

  • Last visited

Everything posted by HenryCan

  1. Thank you, mac_gyver and ginerjm. I imitated the approach you suggested and everything is working fine now.
  2. I want the checkboxes to be INITIALLY checked but if the user unchecks some of them before pressing Submit and then I have to redisplay the form to show error messages in other fields, I need to display the ones he unchecked as unchecked when I display the errors. If I just hard-code "checked" in the checkbox description, I don't see how I'm going to uncheck them programmatically and keep them unchecked on the form.
  3. How do I turn a checkbox on or off programatically? My form has several checkboxes that are grouped together and I want them to be turned on initially when the user first sees the form.I can do that very easily by just including "checked" in the HTML for the checkbox. But if the user turns off the checkbox before he presses Submit, I want to make sure I show the ones that are off as off when I redisplay the form as a result of errors in other fields, like textareas. How do I turn the checkbox off or on within my PHP code?
  4. This sample program, which I use as a quick test of my database functionality, may be of some help to you. (I hope my esteemed colleagues on this forum will tell us if they find any major shortcomings to this code; it may not be the best style but it gets the job done.) <?php /* A standalone test file to see if we can connect to the database and read and display a table. */ /* Connect to the database. */ try { $db = new PDO('mysql:host=localhost;dbname=id1556139_topics;charset=utf8', 'id1556139_henry', 'password'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } catch (PDOException $excp) { echo "Error getting database connection: " . $excp->getMessage(); die(); } /* Read a simple table and store the data in an array. Display each row as it is fetched. */ $debug = 0; echo "<p>This report lists all members who choose to appear in the fake member directory.\n</p>"; echo "<p>Dashes in the table represent values that are unknown or not applicable. For example, if a comment of a member is not provided, a dash is used to stand in for the unknown value.</p>\n"; echo "<table class='generated'>\n"; echo "<tr class='heading'><th>ID</th><th>Name</th><th>Email</th><th>Phone</th><th>Comment</th></tr>\n"; try { /* Display table loop */ $stmt = $db->query("select id, name, email, phone, ifnull(comment,'-') as comment from Contact_Info_Fake order by id"); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $id = $row['id']; //store the ID from the current row of the Contact_Info table $name = $row['name']; //store the member name from the current row of the Contact_Info table $phone = $row['phone']; //store the phone number from the current row of the Contact_Info table $email = $row['email']; //store the email address from the current row of the Contact_Info table $comment = $row['comment']; //store the comment from the current row of the Contact_Info table if ($debug) { echo "Fetch<br/>"; echo " ID: $id<br/>"; echo " Name: $name<br/>"; echo " Phone: $phone<br/>"; echo " Email: $email<br/>"; echo " Comment: $comment<br/>"; } Write_One_Member($id, $name, $phone, $email, $comment); //write the information for a single meeting } } catch (PDOException $excp) { echo "Error reading table Contact_Info_Fake: " . $excp->getMessage(); } echo "</table>"; exit(0); function Write_One_Member($id, $name, $phone, $email, $comment) { global $debug; if ($debug) { echo "<p>Write_One_Member:<br/>"; echo " ID: $id<br/>"; echo " Name: $name<br/>"; echo " Phone: $phone<br/>"; echo " Email: $email<br/>"; echo " Comment: $comment<br/>"; } echo "<tr><td>$id</td><td>$name</td><td>$phone</td><td>$email</td><td>$comment</td></tr>\n"; } ?> I was going to provide the SQL to create my little test table, Contact_Info_Fake, but it seems phpMyAdmin has dropped the ability to export that information to an external file so I'm going to have to leave that part to your imagination. Ask here if you need more information about that or any aspect of my PHP code.
  5. I've just ported a system I wrote a few years back into PHP into a new environment that has PHP 7.1.4 instead of PHP 5.x. My programs used to work perfectly but are now throwing errors, many of which I've never seen before. I am not a fluent PHP programmer although I have programmed in a variety of languages, including Java, so I'm not new to programming by any means. I was reading a short tutorial about PHP error handling and they suggested writing an error-handling function and invoking it as follows: //Error handler function function customError($errno, $errstr, $errfile, $errline, $errcontext) { echo "<b>Error:</b> [$errno] $errstr<br>"; echo "<b>File/Line:</b> $errfile/$errline<br>"; //echo "<b>Error context:</b><br>"; //print_r($errcontext); echo "....Ending Program"; die(); } //Set error handler set_error_handler("customError",E_ALL); I did that and got this: Error: [8] Undefined index: report_layout File/Line: /storage/h13/139/1556139/public_html/SideTrekkedContributions.php/28 In the first line of that, am I correct in assuming that the [8] denotes the severity level of the error, specifically E_NOTICE, which is defined as "Run-time notices. The script found something that might be an error, but could also happen when running a script normally."? Or is the 8 uniquely associated with an "Undefined index" error in a world where every distinct error has a specific number associated with it, like 37 for division by zero and 412 for array index out of bounds? I think it must be the former and that there will be all kinds of errors that have the number 8 but my knowledge of PHP is sketchy so I want to be sure. Now, assuming I'm right and that 8 is the severity of the error, what should one normally do with E_NOTICE errors? Ideally, I'd like PHP not to display this message and I'd like to get rid of the message by doing whatever I need to do to prevent PHP from thinking there's anything wrong (as opposed to telling it not to show me errors below a certain severity). In this particular case, the line of code identified as the source of the error is this: switch($_POST['report_layout']) { I'm darned if I can see what's wrong with this. I never got an error of any kind about this code on the old system. But maybe the old system wanted to show me messages like this but had minor errors suppressed; I really don't know. I tried initializing that variable to null $report_layout = null; but that did NOT keep the error from appearing. I'm at a loss how to satisfy PHP that the variable has been appropriately initialized so it doesn't have to tell me that there's an undefined index. Can anyone enlighten me on the points I've raised?
  6. I'm developing a form which is essentially a simple set of radio buttons. Conceptually, it is like this: Please select a theme from the list: o Black o Blue o Red [submit] [Reset] I'm actually showing a slideshow of images showing the appearance of each of the themes in a slideshow that only shows one image at a time. I want my users to click on the image that represents the theme they want and, ideally, not have to click on the Submit button at all. Then I will save the name of the theme they chose in a cookie (if cookies are enabled). Many years ago, I dabbled in things like CGI and I have a vague recollection, possibly faulty, that it's not difficult to make a form that has only one set of radio buttons treat the selection of one of the radio buttons as a Submit. I don't remember how to do it though. Can anyone advise me on whether it is indeed possible and, if it is, how I make the selection of the radio button cause the form to be submitted?
  7. Please see my reply to Davidannis. I really don't want to bring JavaScript into the situation since some visitors will certainly have it turned off. I think your approach would make me send the visitor to the Preferences page if he hasn't already set some Preferences and I'm reluctant to do that. Also, what do you mean be having one cookie be an array with all the settings? Is there some reason that a single complex cookie is a much better choice than several simple cookies? And aren't I going to run into issues with the max cookie size of 255 bytes? (At least I think that's what it was back in the early days; perhaps it has been increased in recent years.) I don't actually know how much data I will have to save yet. I'm prototyping a design of my own creation. I may end up letting my visitors choose preferences for a whole bunch of things or maybe just a small handful. It depends on the feedback I get as I test. If they want more control, I'll have to save more data. Multiple simple cookies, each with a clear purpose, seems a little easier at first glance than having to make up a lot of very short codes for everything (assuming many values to be saved) and then packing and unpacking/parsing all of that information once I retrieve the cookie. But that's just an initial reaction. I'm open to trying to cram everything into a single cookie if there is some large benefit to it.
  8. Please see my reply to Davidannis. I don't think I'm likely to generate enough preferences to need a database table. In any case, I'm not awfully worried about database work; I've done lots of that. Some of it was even with PDO and MySQL, both of which are available in my current environment ;-) Hmm. Am I understanding you correctly? Are you proposing a database table INSTEAD of cookies? Or are you suggesting them as a supplement to the cookies? If the latter, what is the table going to do that the cookie doesn't?
  9. The gist of it is that you can't fetch a newly-created cookie immediately after creating it: you have to send a new batch of headers before you can possibly see the new cookie. These are the top two search engine hits to my question: http://stackoverflow.com/questions/6663859/check-if-cookies-are-enabled -and- http://stackoverflow.com/questions/9448424/detect-if-cookies-are-enabled-in-php All the replies seem to get criticized for something or another, although I may be misunderstanding what I'm reading. As for your suggestion, I'm struggling a bit with how to distinguish between the case of: 1. no cookie has been set because the visitor has never been here before 2. no cookie has been set because the visitor has disabled cookies (or someone did it for them and they don't realize they are disabled) I'd like to avoid offering the visitor a Preferences page if it isn't possible for me to save those preferences in a cookie. If they arrive on the Preferences page on their own, I'd like to be able to write code that tells them that cookies are disabled so they will need to enable them before trying to save any preferences. I'd show that message right at the top in bold different coloured text so they couldn't miss it and would probably disable the Submit button as well, only re-enabling it if they can actually save a cookie. I'd rather not force visitors to go to the Preferences page if I can avoid it. Most of them will be perfectly happy with the default values that get set if no Preferences have been selected. I don't want to pressure them to enable cookies just so that I can store Preferences that are identical with default values. I certainly don't want to keep nagging them on every page that cookies are turned off and they should turn them on since many visitors will want to go with values that aren't the default I'm fine with giving them default values if no cookies exist because they haven't been to the site before and haven't chosen any Preferences. I'd rather not force everyone to go to the Preferences page, even if all they have to do is click Submit to save the defaults. However, I might be arm-twisted into going for that if it's the only reasonable way forward.
  10. I want to give the visitors to my site the option of setting certain preferences and remembering what was chosen from one visit to the next. They might have the choice of setting the backgrond colour of the web page or the font or whatever. I've already written and tested code to write standard cookies that preserve their choices. However, I'm wondering if that's the best way or if there is a better approach. I don't want visitors to have to re-choose their preferences every time they arrive at the site, which would appear to be the result of using a session cookie stored on the server. If standard cookies ARE the best solution for my situation, how can I test if the visitor has cookies enabled on their browser? I know that some people definitely don't enable cookies and I'm quite prepared to advise them that they won't be able to save preferences until they enable cookies. However, I first need to know how I can tell definitively if cookies are enabled. Most of the simple code examples I've found by googling simply try to write a cookie and then try to read it; if both succeed, they think the they've established whether cookies are enabled. The more sophisticated examples, particularly those at stackoverflow, come closer but all of them seem to be critiqued by someone to the effect that they won't work in this or that particular situation. Given how long cookies and PHP have been in existence, I have to believe that SOMEONE has figured out a definitive way of testing whether cookies are enabled that works in all reasonable circumstances. Can anyone share that technique with me?
  11. I may be jumping the gun a bit in giving the Ch0cu3r a "Best Answer" for his reply but it will definitely get me started. Thank you for suggesting this tutorial. I've only skimmed it but it looks like it will cover the necessary ground.
  12. I'd like to write - or acquire - code that displays a simple table (name, phone number, email address, plus a comments field) on a web page in a password-protected page and allows a user to add his own information, update it or delete it. I figure hundreds of people and companies have written something like this so I'd like to find either an example I can imitate or even an existing package that I can simply customize to the specifics for my own table. Can anyone help me with that? Or am I going to have to reinvent the wheel for the gazillionth time and write it myself?
  13. I'm an experienced programmer with several languages (COBOL, REXX, Java, Javascript, etc.) under my belt over many years but I know very little about PHP. I dabbled in it a bit a year or two back and got it to do what I wanted to do but I'm not sure how to do what I currently need. A bit of context: I have designed a website where the user can choose between various page styles; each style invokes different CSS to give them a different visual experience of the site. They can select the style they want via View/Style in Internet Exploder and View/Page Style in Firefox but that preference is only remembered as long as they're on that page. As soon as they move to another page on the site, they have to choose the alternate style again or live with the default style. I want them to be able to select that preference ONCE, the store it in a cookie specifically for them, then keep using that style every time they visit the site. (Naturally, I want them to be able to change the style and then remember the new style as well.) There will be many visitors and each will want that same capability. I currently don't require any login to the site. (I'm toying with password-protecting some of the site down the road but that's not a factor in this page style thing that I'm talking about here. Now, my questions. I've found a variety of short articles describing how to use setCookie() to create the cookie (and delete it later) and getCookie() or the newer echo $_COOKIE["name"] to determine the cookie value. However, I am NOT seeing anything that says where I put the code to set and/or read the code within my web pages. I would really appreciate an explanation of that! I expect to write short fragments of php code to do those jobs which I will then imbed within my web pages via SSI (Server Side Includes). Is that reasonable or is there a better way? Also: 1. When the user clicks on the desired Style in the View/Style menu, how do I detect what choice they made? 2. Should the name of the cookie be something like "pageStyle" or should it be something that uniquely identifies the user so that the right cookie is retrieved when the come back? If the latter, what value should I use and how should I obtain it? I assume that something unique about the user obtained from their headers is far better than displaying a dialog and asking them for some unique identifier. I'd rather stay away from Javascript in any of this functionality since I can't rely on it being turned on. One quick aside while I'm here. Does anyone know how to change the page style if visitors use Google Chrome or Opera? I can't even FIND options for changing the page style in my copies of those two browsers and both are current versions. Do they not support differing page styles at all or do they just hide the technique to choose the style very effectively? -- Henry
×
×
  • 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.