PHP 表单需要两个文本字段之一
我意识到关于 .asp 页面有类似的线程,但是我使用的是 html 表单,其中 php 涉及导致调查的提交操作。
我正在开发的客户希望填写其中一个文本字段,但这两个字段都不是硬性要求,必须填写其中之一。可以是两者中的任何一个,但是两者都不能留空。
我不想使用jquery,但是如果有必要我会学习如何使用。我对这一切都是全新的,只是创建表单并将其连接到数据库就需要大量的学习,哈哈。
感谢您的帮助,下面我发布了 HTML 表单及其链接到的 php 脚本。
<form action="insert.php" method="post">
Name: <input type="varchar" name="name" />
<span style="color: #F00">or</span> Email:
<input type="varchar" name="email" />
<input type="submit" />
</form>
PHP'insert.php
<?php
$con = mysql_connect("localhost","gobagtoo_1","1");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("gobagtoo_1", $con);
$sql="INSERT INTO name_email (name, email)
VALUES
('$_POST[name]','$_POST[email]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
$url = 'tool.html';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
mysql_close($con)
?>
M
I realize there are similar threads regarding .asp pages, however I am using an html form with php involved with the submit action leading into a survey.
The client I am developing for would like either one of the text fields to be filled out, while neither one is hard required one of the two must be filled out. It can be either of the two, however both cannot be left blank.
I would prefer to not use jquery, however if this is necessary I will learn how. I am brand new to all of this and just creating the form and connecting it to the database took a whole lot of learning lol.
Thank you for any help, below I have posted the HTML form, and the php script it is linked to.
<form action="insert.php" method="post">
Name: <input type="varchar" name="name" />
<span style="color: #F00">or</span> Email:
<input type="varchar" name="email" />
<input type="submit" />
</form>
The PHP 'insert.php
<?php
$con = mysql_connect("localhost","gobagtoo_1","1");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("gobagtoo_1", $con);
$sql="INSERT INTO name_email (name, email)
VALUES
('$_POST[name]','$_POST[email]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else
$url = 'tool.html';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
mysql_close($con)
?>
M
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 JavaScript 在客户端(您看到表单的地方)和服务器端(提交表单后的 PHP)完成此操作。
在客户端:
1. 页面添加函数validateForm。
2. 将 id 属性添加到您的姓名和电子邮件元素。
3. 将 onsubmit 元素添加到表单标记中。
在 PHP 方面:
You should accomplish this both on the client side (where you see the form) with javascript and on the server side (the PHP after you submit the form).
On the client side:
1. Add the function to the page, validateForm.
2. Add id attributes to your name and email elements.
3. Add an onsubmit element to your form tag.
On the PHP side: