Javascript 如何生成php中所需的几个表单字段?

Javascript 如何生成php中所需的几个表单字段?,javascript,php,field,required,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">

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

联系页面
姓名:
您的浏览器:
Internet Explorer
Mozilla
其他
您使用的软件:
Microsoft Word
Microsoft Excel
Adobe Photoshop
年龄: 10-15
16-20
21-90
其他意见:
这是我的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>

非常感谢。

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

并按如下要求制作下拉列表

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

这会解决问题的。 您还可以从中了解validate.js

*更新:*

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

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

试试这个:

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

联系页面
函数validateForm()
{
var x=document.forms[“我的表格”][“名称”].value;
var y=document.forms[“我的表单”][“注释”]。值;
如果(x==null | | x==“”)
{
警告(“必须填写姓名”);
返回false;
}
如果(y==null | | y==“”)
{
警告(“必须填写评论”);
返回false;
}
}
姓名:
其他意见:
这将在同一页上验证,因此这将是最好的

编辑:

仅针对一种情况,请尝试以下操作:

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

联系页面
函数validateForm()
{
var x=document.forms[“我的表格”][“名称”].value;
var y=document.forms[“我的表单”][“注释”]。值;
var计数=0;
如果(!(x==null | | x==“”)
{ 
计数++;
}
如果(!(y==null | | y==“”)
{ 
计数++;
}
如果(计数<1){
警告(“请至少填写一个字段”);
返回false;
}
}
姓名:
其他意见:

@Daenarys请停止在与JS无关的问题上添加无关的jQuery/javascript。