1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-20 15:38:02 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
jbranchaud
3177917096 Add Determine Which Button Submitted The Form as an HTML TIL 2022-05-21 16:15:29 -05:00
jbranchaud
761fa46704 Add Create A Table From The Structure Of Another as a Postgres TIL 2022-05-21 15:58:04 -05:00
3 changed files with 81 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1200 TILs and counting..._
_1202 TILs and counting..._
---
@@ -353,6 +353,7 @@ _1200 TILs and counting..._
### HTML
- [Adding Alt Text To An Image](html/adding-alt-text-to-an-image.md)
- [Determine Which Button Submitted The Form](html/determine-which-button-submitted-the-form.md)
- [Disable Auto-Completion For A Form Input](html/disable-auto-completion-for-a-form-input.md)
- [Prevent Search Engines From Indexing A Page](html/prevent-search-engines-from-indexing-a-page.md)
- [Render Text As Superscript](html/render-text-as-superscript.md)
@@ -566,6 +567,7 @@ _1200 TILs and counting..._
- [Count Records By Type](postgres/count-records-by-type.md)
- [Count The Number Of Trues In An Aggregate Query](postgres/count-the-number-of-trues-in-an-aggregate-query.md)
- [Create A Composite Primary Key](postgres/create-a-composite-primary-key.md)
- [Create A Table From The Structure Of Another](postgres/create-a-table-from-the-structure-of-another.md)
- [Create An Index Without Locking The Table](postgres/create-an-index-without-locking-the-table.md)
- [Create Database Uses Template1](postgres/create-database-uses-template1.md)
- [Create hstore From Two Arrays](postgres/create-hstore-from-two-arrays.md)

View File

@@ -0,0 +1,34 @@
# Determine Which Button Submitted The Form
It is pretty common for a form to have a singular submit button. If the user
clicks 'Submit', then the form fires a `POST` off to the server, the server can
process the request, and that's it.
But what about a form that has two or more buttons? For instance, imagine some
kind of consent form where the user needs to either _Accept_ or _Reject_ some
terms.
Just like other inputs, [the `<button>` tag can take both `name` and `value`
attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-name).
```html
<form action="/terms" method="post">
<p>Something about the terms ...</p>
<div>
<label for="name">Email: </label>
<input type="email" name="email" id="email" required>
</div>
<div>
<button type="submit" name="commit" value="accept">Accept</button>
<button type="submit" name="commit" value="reject">Reject</button>
</div>
</form>
```
In addition to the `email` attribute, when the user submits the form, it will
include a `commit` attribute that has a value of either `'accept'` or
`'reject'`.
Naming it `commit` is [a convention I'm borrowing from Rails's form
helpers](https://guides.rubyonrails.org/v5.0/form_helpers.html#a-generic-search-form).
You can name it whatever makes sense to you.

View File

@@ -0,0 +1,44 @@
# Create A Table From The Structure Of Another
There are a couple ways to create a new table from the structure of another table.
One of those ways is with the [`create table as`
syntax](https://www.postgresql.org/docs/current/sql-createtableas.html).
```sql
create table dupe_table as table existing_table with no data;
```
I wouldn't recommend this approach though because it only reproduces the
columns and datatypes. The modifiers, indexes, and constraints are not
included.
The [`create table`
syntax](https://www.postgresql.org/docs/current/sql-createtable.html), on the
other hand, gives you more options and flexibility for this kind of task.
```sql
create table dupe_table (like existing_table);
```
This works just like the first statement, reproducing just the columns and
datatypes.
There are options for enhancing this statement. We can tell it to additionally
_include_ things like `defaults`, `indexes`, `constraints`, or even just
_everything_ (`including all`).
Here is what it looks like to copy the `existing_table` so that things like
`not null`, B-Tree indexes, and primary key `default` values are reproduced
along with the columns and datatypes.
```sql
create table dupe_table (
like existing_table
including defaults
including indexes
including constraints
)
```
[source](https://www.reddit.com/r/PostgreSQL/comments/uu8xcs/comment/i9e36m2/)