Good html color selections

  

Call javascript from clicking a button, without submitting a form.

<button type="button" class="mybuttonclass" onClick="jsfunction('arg')">Click to take non-submit action</button>
type="button" is not reduntant in this case. It is specifcally to allow a "button" tag to do something other than submit a form.
Inside a form, <button>xxx</button> (without the type="button") defaults to the "submit" action.
In or out of a form, <button type="button" onClick="jsfunction('arg')>xxx</button> performs the defined non-submit action.
Side note: the newer <button>xxx</button> can include images (and other things?), whereas the older <input type="button"> cannot.

Forms

<form action="somepage.php" method="post">
<input type="text" name="mytextfield" value="defaultvalue"><br>
<input type="hidden" name="myhiddenfield" value="hiddenvalue">
<select name="myselectfield">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select><br>
<!-- Radio buttons with the same 'name' are mutually exclusive -->
<input type="radio" name="myradiogroup" value="option1" checked>Option 1<br>
<input type="radio" name="myradiogroup" value="option2">Option 2<br>
<input type="radio" name="myradiogroup" value="option3">Option 3<br>
<!-- A checkbox can be checked or unchecked independently -->
<input type="checkbox" name="mycheckbox" value="checkvalue" checked>Check me!<br>
<!-- Resetting the form returns all fields to their default values -->
<input type="reset" value="Reset Form"><br>
<!-- Submitting the form sends the named values to somepage.php -->
<input type="submit" value="Submit Form">
</form>


Option 1
Option 2
Option 3
Check me!

name="myname" is referenced in PHP with $_POST["myname"].

Submit a form with javascript

<head>
<script type="text/javascript">
function submitMyForm() {
document.getElementById('myform').submit();
}
</script>
</head>

<form id="myform" action="somepage.php" method="post">
<input type="text" name="mytextfield" value="defaultvalue"><br>
<input type="button" value="Submit Form" onClick="document.getElementById('myform').submit();">
</form>

Pattern for using forms with PHP

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$myfieldvalue = $_POST['mytextfield'];
}
?>

<form action="thispage.php" method="post">
<input type="text" name="mytextfield" value="metal rules"><br>
<input type="submit" value="Submit Form">
</form>

Returning associative array from a prepared statement

$sql = "SELECT * FROM Valor.`Table` WHERE field1 = ? AND field2 = ?;";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $var1, $var2);
$stmt->execute();
$result = $stmt->get_result();
$all_rows = $result->fetch_all(MYSQLI_ASSOC);
foreach($all_rows as $row){
$size = $row['size'];
$alignment = $row['alignment'];;
}
$stmt->close();

PHP Recursive Folder Traversing

function getRecursiveFilenames($dirPath) {
// Create a RecursiveDirectoryIterator
$directoryIterator = new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::SKIP_DOTS);
// Flatten the directory structure into a single linear iterator
$iteratorIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::LEAVES_ONLY);
$filenames = [];
foreach ($iteratorIterator as $file) {
// Use getPathname() to get the full path and filename
$filenames[] = $file->getPathname();
// Alternatively, use getFilename() for just the file name
// $filenames[] = $file->getFilename();
// Or use getRealPath() for the absolute path
// $filenames[] = $file->getRealPath();
// Or use SplFileInfo methods for other info
// $filenames[] = $file->getBasename();
}
return $filenames;
}

Get Files Matching a Pattern and loop through the lines of each file

$pattern = '/^.*.html$/';
$files = preg_grep($pattern, scandir("Creatures/SourceWebsites/aonprd"));
foreach ($files as $filename) {
$lines = file("Creatures/SourceWebsites/aonprd/$filename", FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
$line = trim($line);
}
}

Pattern Matching

$pattern = '/.*>(.*)/';
$matches = [];
preg_match($pattern, $aln, $matches);
$aln = $alignments[$matches[1]] ?? $matches[1];
The pattern captures everything after the first occurrence of ">".
$matches[0] contains the entire matched string, while $matches[1] contains the part captured by the parentheses, which is the text after ">".
The ?? operator is the null coalescing operator. It returns the value on its left if it exists and is not null; otherwise, it returns the value on its right. In this case, if $alignments[$matches[1]] is set and not null, it will be used; otherwise, $matches[1] will be used.
This is a common pattern for looking up a value in an associative array and providing a default if the key is not found.

Watch This Space

Watch This Space

Watch This Space

Watch This Space

Watch This Space

Watch This Space