Skip to content

Instantly share code, notes, and snippets.

@jaroel
Created February 24, 2023 00:18
Show Gist options
  • Save jaroel/407eacf81a7d752fb0924e19d304bcda to your computer and use it in GitHub Desktop.
Save jaroel/407eacf81a7d752fb0924e19d304bcda to your computer and use it in GitHub Desktop.
Use POST data in forms in SSR with SolidStart
import {isServer} from 'solid-js/web'
import {createRouteData} from 'solid-start'
import {useRequest} from 'solid-start/server'
export default function FormPage() {
const postData = createRouteData(async () => {
const event = useRequest()
if (!isServer || event.request.method !== 'POST') {
return undefined
}
const data = await event.request.formData()
return Object.fromEntries(data) as Record<string, string>
})
return <>
<h1>SSR-only Form POST data</h1>
<form method='post' enctype='multipart/form-data'>
<input type='hidden' name='subject' value={postData()?.subject ?? 'Defense against the Dark Arts'} />
<input type='text' name='myfield' class='border' value={postData()?.myfield ?? ''} />
<button type='submit'>Submit</button>
<pre>{JSON.stringify(postData())}</pre>
</form>
</>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment