gpt4 book ai didi

php - 带有必填字段的 html 表单

转载 作者:可可西里 更新时间:2023-11-01 00:06:24 24 4
gpt4 key购买 nike

我有一个当前可用的表单,但想将 php 代码添加到“post”文件,因此需要 html 表单中的某些字段,否则会显示一条消息。

这是我目前的 html 代码。

<form action="contact.php" method="post">
<input type="text" class="main" name="cf_name" value="Name (Required)" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_company" value="Company" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_phone" value="Phone (Required)" size="31" />
<br />
<br />
<input type="text" class="main" name="cf_email" value="Email (Required)" size="31" />
<br />
<br />
<textarea type="text" name="cf_text0" cols="34" rows="3" class="main">Message</textarea>
<br />
<input type="image" src="images/send.jpg" height="16" width="41" border="0"
alt="Submit Form" />
</form>

这是我的 php 代码

<?php
$field_name = $_POST['cf_name'];
$field_company = $_POST['cf_company'];
$field_phone = $_POST['cf_phone'];
$field_email = $_POST['cf_email'];
$field_message = array($_POST["cf_text0"],);

$mail_to = 'callum@colmandesigns.co.nz';
$subject = 'A & J Print - Contact Form '.$field_name;

$field_message="From: {$field_name}
Company: {$field_company}
Phone: {$field_phone}
Email: {$field_email}
Message: {$field_message[0]}";


$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $field_message, $headers);

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to craig@colmandesigns.co.nz');
window.location = 'index.html';
</script>
<?php
}
?>

我想要填写姓名、电话和电子邮件字段。

谢谢,卡勒姆

最佳答案

在 HTML 中,您可以只放置“required”属性来使输入标签成为必需的!例如

<input type="text" id="someId" name="IP" placeholder="IP" required tabindex="1">

<input type="text" id="someId" name="IP" placeholder="IP" required="true" tabindex="1">

但是考虑一下,IE 和 Safari 直到现在才支持这个标签!

关于php - 带有必填字段的 html 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10219557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com