reactjs - 一旦按下“提交”,NextJs ContactUs 表单就会重定向到外部 URL
问题描述
我创建了联系我们表单,我希望我网站的用户在填写表单后按下“提交”按钮后重定向到外部链接。提交按钮位于代码的末尾。这是代码:
import {
Button,
FormControl,
FormLabel,
Input,
ModalHeader,
ModalFooter,
ModalBody,
ModalCloseButton,
useToast,
} from '@chakra-ui/react'
import { useState } from 'react'
import { API } from "aws-amplify"
import { createCandidate } from '../src/graphql/mutations'
export const ContactForm = ({ initialRef, onClose }) => {
const toast = useToast()
const [formState, setFormState] = useState({
name: '',
designation: '',
companyname: '',
email: '',
phone: '',
comments: '',
})
const handleContactFormSubmit = async (e) => {
e.preventDefault()
const { name, designation, companyname, email, phone, comments} = formState
if (name && designation && companyname && email && phone && comments) {
try {
await API.graphql({
query: createCandidate,
variables: {
input: {
name,
designation,
companyname,
email,
phone,
comments,
},
},
})
toast({
title: 'Congratulations',
position: 'top-right',
description: 'Successfully submitted!',
status: 'success',
duration: 5000,
isClosable: true,
})
onClose()
} catch (e) {
toast({
title: 'Error',
position: 'top-right',
description: e.message,
status: 'error',
duration: 5000,
isClosable: true,
})
}
} else {
toast({
title: 'Uh-Oh ',
position: 'top-right',
description: 'Please verify all fields are filled out.',
status: 'error',
duration: 5000,
isClosable: true,
})
}
}
return (
<>
<ModalHeader>Enter Your Details</ModalHeader>
<ModalCloseButton />
<form onSubmit={handleContactFormSubmit}>
<ModalBody pb={6}>
<FormControl>
<FormLabel>Name</FormLabel>
<Input
ref={initialRef}
placeholder=""
value={formState.name}
onChange={(e) =>
setFormState({ ...formState, name: e.target.value })
}
/>
</FormControl>
<FormControl mt={4}>
<FormLabel>Designation</FormLabel>
<Input
placeholder=""
value={formState.designation}
onChange={(e) =>
setFormState({ ...formState, designation: e.target.value })
}
/>
</FormControl>
<FormControl mt={4}>
<FormLabel>Company Name</FormLabel>
<Input
placeholder=""
value={formState.companyname}
onChange={(e) =>
setFormState({ ...formState, companyname: e.target.value })
}
/>
</FormControl>
<FormControl mt={4}>
<FormLabel>Email</FormLabel>
<Input
placeholder=""
type="email"
value={formState.email}
onChange={(e) =>
setFormState({ ...formState, email: e.target.value })
}
/>
</FormControl>
<FormControl mt={4}>
<FormLabel>Phone Number</FormLabel>
<Input
placeholder=""
minLength="10"
value={formState.phone}
onChange={(e) =>
setFormState({ ...formState, phone: e.target.value })
}
/>
</FormControl>
<FormControl mt={4} >
<FormLabel>I would like to know about</FormLabel>
<Input
placeholder=""
value={formState.comments}
onChange={(e) =>
setFormState({ ...formState, comments: e.target.value })
}
/>
</FormControl>
</ModalBody>
<ModalFooter>
<Button colorScheme="red" mr={3} type="submit">
Submit
</Button>
<Button onClick={onClose}>Cancel</Button>
</ModalFooter>
</form>
</>
)
}
我一直在寻找在线参考资料,但找不到指导如何在按下提交后重定向到外部链接的解决方案。
解决方案
推荐阅读
- keyup - 在 keyup children child 上检索新数据到 div
- javascript - 有什么方法可以在不使用 position : fixed 的情况下修复输入标签?
- java - 如何在 Android Studio 中更改屏幕方向后修复工具栏偏移
- python - 当 csv 具有大列时,Logstash 进程被终止
- c++ - c++ 中的数组需要什么数据类型?
- mysql - 如何在 Laravel Sail docker-compose.yml 中添加 mysql 配置?
- python-3.x - 基于标量的彩色二维绘图线
- r - 使用蒙特卡罗方法从密度核计算均值和方差
- php - PHPMyAdmin 和 ed25519
- .net - 数据库设计 - 使用 EF CORE 的记录之间的依赖关系