Jump to content

iwato

Members
  • Content Count

    1,435
  • Joined

  • Last visited

  • Days Won

    4

iwato last won the day on May 13

iwato had the most liked content!

Community Reputation

18 Good

2 Followers

About iwato

  • Rank
    Dedicated Member

Previous Fields

  • Languages
    HTML, CSS, Javascript, PHP, MySQL and Spoken Language

Contact Methods

  • AIM
    moogoonghwa
  • Website URL
    http://www.grammarcaptive.com
  • Yahoo
    iddor
  • Skype
    kiusau

Profile Information

  • Gender
    Male
  • Location
    Seattle, Washington USA 98104

Recent Profile Visitors

19,183 profile views
  1. iwato

    A Problem of Scope

    Very nice. Have a great weekend! Roddy
  2. iwato

    A Problem of Scope

    DILEMMA: What I would like following function to do is keep iterating until it has satisfied the condition: abs($random_lum - $contrast_lum) > $lum_differential What it does instead is return NULL and stops, when it is unable. function select_color_pair($base, $lum_differential =0.25){ $color_pair = array(); $result = array( 'rgb' => [], 'hex' => '' ); for( $i=0; $i<3; $i++ ){ $rand = mt_rand(0, 255); $result['rgb'][] = $rand; $dechex = dechex($rand); if(strlen($dechex) < 2){ $dechex = '0' . $dechex; } $result['hex'] .= $dechex; } if ($base == true) { $random_lum = COLOR::fromRGB($result['rgb'])->getLuminance(); $contrast_color = COLOR::fromRGB($result['rgb'])->invert(); $contrast_lum = COLOR::fromHex($contrast_color)->getLuminance(); if (abs($random_lum - $contrast_lum) > $lum_differential) { $random_color = COLOR::fromRGB($result['rgb'])->getHex(); return $color_pair = ['text_color' => $random_color, 'bg_color' => $contrast_color]; } else { select_color_pair($base); } } else { $random_lum = COLOR::fromHex($result['hex'])->getLuminance(); $contrast_color = COLOR::fromHex($result['hex'])->invert(); $contrast_lum = COLOR::fromHex($contrast_color)->getLuminance(); if (abs($random_lum - $contrast_lum) > $lum_differential) { return $color_pair = ['text_color' => strtoupper($result['hex']), 'bg_color' => strtoupper($contrast_color)]; } else { select_color_pair($base); } } } Any suggestions? Roddy
  3. Yes, it works! Hooray! Hooray! Roddy
  4. iwato

    A Problem of Scope

    function select_color_pair($lum_differential, $base = true, $type = true) { function random_color($base, %type){ ... Do something with $base and $true } } QUESTION: Is the second appearance of the parameters $base and $type necessary? Roddy
  5. So, if I had written $result = array( 'rgb' => [], 'hex' => '' ); then I would not have had to rewrite the function. Is this correct? Roddy
  6. So, are you saying that $result['rgb'][$col] is being read as NULL[$col]? If so, what happened between PHP 5.3.8 and PHP 7.2 that changed $result['rgb'] from an element of an array to a string? Roddy
  7. BACKGROUND: The following newly created function works splendidly with PHP 5.3.8, but it fails nearly totally in PHP 7.2. Still there is no error message. The FUNCTION: function random_color($base = true, $type = true){ $result = array( 'rgb' => '', 'hex' => '' ); foreach( array('r', 'b', 'g') as $col){ $rand = mt_rand(0, 255); $result['rgb'][$col] = $rand; $dechex = dechex($rand); if(strlen($dechex) < 2){ $dechex = '0' . $dechex; } $result['hex'] .= $dechex; } if ($base == true) { if ($type == true) { return $result['rgb']; } else { return join(",", $result['rgb']); } } else { return strtoupper($result['hex']); } } QUESTION: What must I change to get it to work with PHP 7.2. HINT ONE: The function delivers well when the $base parameter is set to false. Otherwise, it returns only one of the three RGB colors no matter the value of $base or $type. HINT TWO: There is an error message after all. Simply, I was looking in the wrong place. [08-Aug-2019 18:04:40 UTC] PHP Warning: Illegal string offset 'r' in ..../includes/random_color.incl.php on line 15 [08-Aug-2019 18:04:40 UTC] PHP Warning: Illegal string offset 'b' in ..../includes/random_color.incl.php on line 15 [08-Aug-2019 18:04:40 UTC] PHP Warning: Illegal string offset 'g' in ..../includes/random_color.incl.php on line 15 ANSWER: function random_color($base = true, $type = true){ $result = array( 'rgb' => '', 'hex' => '' ); $keys = ['r', 'b', 'g']; foreach( $keys as $key => $value ){ $rand = mt_rand(0, 255); $result['rgb'][$value] = $rand; $dechex = dechex($rand); if(strlen($dechex) < 2){ $dechex = '0' . $dechex; } $result['hex'] .= $dechex; } if ($base == true) { if ($type == true) { return $result['rgb']; } else { return join(",", $result['rgb']); } } else { return strtoupper($result['hex']); } } NEW QUESTION: But why? Roddy
  8. It worked! As is often the case, I am heartily embarrassed. Sometimes I get so lost in the trees that I can no longer see the forest. The new resetForm() function: function resetForm() { $.ajax({ type: "POST", url: './php/captcha_reset.php', data: {dataRequest: 'requestInstance'}, statusCode: { 404: function() { alert( "Page not found" ); }}, success: function(jsonData) { jsonObj = JSON.parse(jsonData); captcha_sa = jsonObj.captcha_ra; $('#captcha_answer').val(''); $('#captcha_question').html(jsonObj.captcha_rq); $('#captcha_success').css('display', 'none'); $('#captcha_error').css('display', 'none'); } }); } Have a great day! Roddy
  9. That was my idea too, but I could not get it to work after two-and-one-half hours of experimentation. I have decided to provide all of the code with the exception of the modified PHP class, and add the following comments. The submitForm() function works well with each new page load. The resetForm() function works well with each button click. The submitForm() function fails to work after the resetForm() function has been activated. In effect, the function cannot match the word-number phrase with the number that is entered by the visitor and the error message results. Please skip to the dilemma for a quick technical understanding of the problem. The CODE //Create the first instance on page load and assign values. require_once("./php/classes/class.imageless_captcha.php"); $captcha_s = new ImagelessCaptcha(3, false, false); $captcha_sq = $captcha_s->formPhrase(); $captcha_sa = $captcha_s->getInt(); /*****************************************/ //Load the submitForm() and resetForm() functions. <script src='./javascript/imageless_captcha.js'></script> //The loaded functions. function submitForm(confirmCode) { var visitorConfirmation = confirmCode; if (visitorConfirmation == captcha_sa) { alert('Send search info for further processing.'); $('#captcha_success').css('display', 'block'); $('#captcha_error').css('display', 'none'); } else { $('#captcha_error').css('display', 'block'); $('#captcha_success').css('display', 'none'); } } function resetForm() { $.ajax({ type: "POST", url: './php/captcha_reset.php', data: {dataRequest: 'requestInstance'}, statusCode: { 404: function() { alert( "Page not found" ); }}, success: function(jsonData) { jsonObj = JSON.parse(jsonData); $('#captcha_answer').val(''); $('#captcha_question').html(jsonObj.captcha_rq); $('#captcha_success').css('display', 'none'); $('#captcha_error').css('display', 'none'); } }); } /*****************************************/ // Create a second instance of the ImagelessCaptcha class for the resetForm() function. <?php ini_set('log_errors', 1); ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log'); ini_set('html_errors', 1); ini_set('display_errors', 0); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $captcha_reset = array(); if ($_POST['dataRequest']) { $dataRequest = filter_var($_POST['dataRequest'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); if ($dataRequest == 'requestInstance') { require_once('./classes/class.imageless_captcha.php'); $captcha_r = new ImagelessCaptcha(3, false, false); $captcha_reset['captcha_rq'] = $captcha_r->formPhrase(); $captcha_reset['captcha_ra'] = $captcha_r->getInt(); } } echo json_encode($captcha_reset); } ?> /******************************************/ A modified version of the ImagelessCaptcha class not included here. /*****************************************/ //Embed the success and error messages. <div id='captcha_error' style='display:none;'> <p>Error: The number that you entered was incorrect. Please try again.</p> </div><!-- end div#captcha_error --> <div id='captcha_success' style='display:none;'> <p>Congratulations! Your search request has been sent.</p> </div><!-- end div#captcha_success --> //Embed the captcha form <form> <div id='captcha_messaging'> <label id='captcha_question'></label><br> <input id='captcha_answer' type="text" value="" > </div><!-- end div#captcha_messaging --> <div id='captcha_buttons'> <input id='captcha_submit' type='button' style='width:100px;' value='submit'> <input id='captcha_reset' type='button' style='width:100px;' value='reset'> </div> </form> /*****************************************/ // Assign the PHP outcomes to their Javascript counterparts. <script> var captcha_sq = <?php echo "'" . $captcha_sq . "'"; ?>; var captcha_sa = Number(<?php echo $captcha_sa; ?>); $('#captcha_question').html(captcha_sq); </script> /*******************************************/ // Call the submitForm() and resetForm() functions <script> $(document).ready(function() { $('#captcha_submit').on('click', function() { confirmCode = Number($('#captcha_answer').val()); submitForm(confirmCode); }); $('#captcha_reset').on('click', function() { resetForm(); }); }); </script> The DILEMMA Replace the value of the captcha_sa variable in the submitForm() function with the value of newly generated captcha_ra variable generated by the resetForm() function. function submitForm(confirmCode) { var visitorConfirmation = confirmCode; if (visitorConfirmation == captcha_sa) {...} ... } } Please advise. Roddy
  10. BACKGROUND: I have a javascript document that is dynamically loaded when a certain condition is met. In this document there are two functions: the first function is called submitForm(), and the second is called resetForm(). Each of these functions is triggered separately by a different button on the same HTML webpage. Both functions rely on the same PHP class, but each relies on data created by a different instance containing different data. The first instance is created at run time and is ultimately read into the submitForm() function. The second instance is created at the visitor's discretion via an AJAX call that is made when the resetForm() function is triggered. The success function of the AJAX call made by the resetForm() function generates a value needed by the submitForm() function when it is run for a second time after the resetForm() function has already been called once. QUESTION: How do I get the resetForm() function to return a value generated by its AJAX call's success function in a manner that it can be used by the submitForm() function? Sorry, no code, as the whole process is even a little more complicated than what has just been explained.. Please advise. Roddy
  11. Have a great week end! Roddy
  12. QUESTION THREE: By way of confirmation, may I use the same require_once() function as many times and places as I like during the same browser session with good confidence that it will be included/required only once? Roddy
  13. A Ha! A "hitherto by me yet to be employed" technique is now on the table. Please do not run away for the weekend just yet. QUESTION ONE: Are you suggesting that the .js file extension is just a file accounting convenience and has no effect on whether Javascript is run? QUESTION TWO: Are there any additional security problems associated with setting the server to run javascript (.js) files with PHP? Roddy
  14. I do not have what you are suggesting, but you have answered my question with regard to multiple include statements. I do have one more question before you disappear for the week end. Will PHP used to create new instances of the same previously called PHP class and to assign values to Javascript variables be properly read when the Javascript file is loaded. Or, must the assignment be achieved via AJAX or some other, hitherto by me yet to be employed, technique? Roddy
  15. Not exactly! I have modified a class that someone else designed for a very different environment. Right now I am concerned about where to include the class, so that its inclusion does not become redundant on the one hand, and so that it is always and everywhere available on the other. There is a three step process involved. 1) Load the HTML mainpage. 2) Call a Javascript document that loads any of three search <div>s on the mainpage into the mainpage's main <div>. 3) Call a another Javascript document that captures the search data and sends it off to a PHP file via AJAX for processing on the mainpage. Roddy
×
×
  • Create New...