-1

我在 index.php 文件中有这个表格:

<HTML>
<HEAD>
    <TITLE>Contact Page</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>

<table border="0" cellpadding="0" cellspacing="3">
    <form method="post" action="thankyou.php">
        <tr>
            <td>Name:</td>
            <td><input name="name" type="text"></td>
        </tr>

        <tr><td>Your Browser:</td>
            <td>
                <select name="Browser">
                    <option value="Internet Explorer" selected>Internet Explorer
                    <option value="Mozilla">Mozilla
                    <option value="Other">Other
                </select></td>
        </tr>



        <tr>
            <td>Software you use:</td>
            <td><input name="Microsoft Word" type="checkbox" value="yes">Microsoft Word<br>
                <input name="Microsoft Excel" type="checkbox" value="yes">Microsoft Excel<br>
                <input name="Adobe Photoshop" type="checkbox" value="yes">Adobe Photoshop<br>

            </td>
        </tr>

        <tr>
            <td>Age:</td>
            <td>
                <input name="Age" type="radio" value="10-15">10-15<br>
                <input name="Age" type="radio" value="16-20">16-20<br>
                <input name="Age" type="radio" value="21-90">21-90<br>
            </td>
        </tr>

        <tr><td>Other Comments:</td>
            <td><textarea name="Other Comments" rows=10 cols=30></textarea></td>
        </tr>

        <tr><td>&nbsp;</td><td><input type="submit" value="Send Message"></td>
        </tr></form>
</table>

这是我的thankyou.php:

    <?php
//This command imports the values from contact.php. Please do not touch.
@import_request_variables("gpc");

//The email address the message will be sent to
$youremail = "youremail@yoursite.com";

//The subject of the email you will receive;
$subject = "Our Survey";

//The page your visitor will be redirected to.
$redirect = "http://www.yoursite.com";

//Time until the page redirects your visitor (seconds)
$secs = "5";

//This takes all of the information from the form and sorts it out. Please leave as is.
foreach ($_POST as $name => $value) {
    $thetext = $thetext . "$name : $value\n";
}
?>

<HTML>
    <HEAD>
        <TITLE>Thank you</TITLE>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    </HEAD>
    <table cellpadding=0 cellspacing=0>
        <tr><td>

<?php
//Checks to see if the name field is empty. You can delete or add fields as needed.

if ((!empty($name))) {

    $name = stripslashes($name);
    $message = stripslashes($message);
//This is where the email is sent using your values from above.
    mail("$youremail", "$subject", "$thetext");
    ?>

                    <meta http-equiv="refresh" content="<?php = $secs; ?>;URL=<?php = $redirect; ?>">

                    Thank you, we have recieved your message.
                    <p>
                        You are now being redirected to our <a href="<?php = $redirect; ?>">homepage</a>.

    <?php
} else {
    ?>

                        We require your name email address and a message in order to reply to your message. Please click <a href="javascript:history.back(1);">here</a> or your browsers back button to try again.

                        <?php
                    }
                    ?>

            </td></tr>
    </table>

我想将“您的浏览器”和/或“其他评论:”设为必填字段,因此如果访问者至少没有填写其中一个,他将被重定向到某个页面,该页面会说,请填写必填项字段。如果他填写其中任何一个,表格应该会成功提交。我知道需要一些基本的编辑,但不幸的是我只是在学习,自己做不到。

请帮忙。

4

2 回答 2

2

尝试这个:

<HTML>
<HEAD>
<TITLE>Contact Page</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<script>
function validateForm()
{
var x=document.forms["my_form"]["name"].value;
var y=document.forms["my_form"]["comments"].value;
if (x==null || x=="")
  {
  alert("name must be filled out");
  return false;
  }
  if (y==null || y=="")
  {
  alert("comments must be filled out");
  return false;
  }



}
</script>

<table border="0" cellpadding="0" cellspacing="3">
<form method="post" action="thankyou.php" name="my_form" onsubmit="return validateForm()">
<tr>
<td>Name:</td>
<td><input name="name" type="text"></td>
</tr>


<tr><td>Other Comments:</td>
<td><textarea name="comments" rows=10 cols=30></textarea></td>
</tr>

<tr><td>&nbsp;</td><td><input type="submit" value="Send Message"></td>
</tr></form>
</table>

这将在同一页面上验证,因此最好。

编辑:

只有一个,试试这样:

<HTML>
<HEAD>
<TITLE>Contact Page</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<script>
function validateForm()
{
var x=document.forms["my_form"]["name"].value;
var y=document.forms["my_form"]["comments"].value;
var count = 0;
  if (!(x==null || x==""))
  { 
    count++;
  }
  if (!(y==null || y==""))
  { 
     count++;
  }
  if(count < 1){
    alert("Please fill at least one field");
    return false;     
  }



}
</script>

<table border="0" cellpadding="0" cellspacing="3">
<form method="post" action="thankyou.php" name="my_form" onsubmit="return validateForm()">
<tr>
<td>Name:</td>
<td><input name="name" type="text"></td>
</tr>


<tr><td>Other Comments:</td>
<td><textarea name="comments" rows=10 cols=30></textarea></td>
</tr>

<tr><td>&nbsp;</td><td><input type="submit" value="Send Message"></td>
</tr></form>
</table>

演示在这里>>

于 2013-05-27T11:32:39.083 回答
1

您可以在 html/php 表单中使用 jquery 进行验证。尝试在您的 html 代码中包含 jquery 验证。您所要做的就是包含最新版本的 jquery 和来自github的 validate.js

并使下拉列表需要如下。

<select name="Browser" required="true">

这将解决问题。您还可以从此链接探索 validate.js

*更新:*

您还可以对现代浏览器使用html5验证。

<input type="text" name="username" required="required">
于 2013-05-27T11:32:06.130 回答