2013-05-27 29 views
-1

我有這種形式的index.php文件:如何在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:

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

<? 

//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="<?=$secs;?>;URL=<?=$redirect;?>"> 

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

<? 
} 
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. 

<? 
} 

?> 

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

我想使「您的瀏覽器」和/或「其他意見:「一個必填字段,所以如果訪問者至少沒有填寫其中的一個,他將被重定向到某個頁面,然後請填寫必填字段。如果他填寫了其中的任何一個,表格應提交成功。我知道有一些基本的編輯需要,但不幸的是,我只是在學習,不能自己做。

請幫忙。

+1

@Daenarys請停止對不相關的JS問題,將不相關的jQuery/JavaScript的。 – vard

回答

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 forms中使用jquery進行驗證。嘗試在你的html代碼中包含jquery驗證。 所有你需要做的只是包括最新版本的jquery和來自github

的validate.js,並使下拉列表如下所示。

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

而且這將照顧的事情。 你還可以遊覽約validate.js從this link

* 更新:*

您也可以使用HTML5 驗證現代瀏覽器。

<input type="text" name="username" required="required"> 
相關問題