如何在php中创建几个表单字段?

时间:2013-05-27 11:22:25

标签: javascript php field required

我在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>

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

请帮忙。

2 个答案:

答案 0 :(得分: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>

Demo here>>

答案 1 :(得分:1)

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

并按如下所示制作下拉列表。

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

这会照顾好事情。 您还可以从this link

中探索validate.js

* 更新:*

此外,您可以对现代浏览器使用 html5 验证。

<input type="text" name="username" required="required">