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

问题描述 投票:-1回答:2

我在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 = "[email protected]";

//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>

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

请帮忙。

javascript php field required
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>

Demo here>>


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">
© www.soinside.com 2019 - 2024. All rights reserved.