我在互聯網上找到了這段代碼,我想要這些輸入:名稱復選框,包含 5 個專案
此代碼將復選框中選擇的用戶資訊和他的姓名保存在文本檔案中
有人可以幫我解決這個問題嗎?
<!DOCTYPE html>
<?php
if(isset($_POST['submit'])){
$Name = "Username:".$_POST['username']."
";
$Pass = "Password:".$_POST['password']."
";
$file=fopen("saved.txt", "a");
fwrite($file, $Name);
fwrite($file, $Pass,);
echo fwrite($file,"*************************",);
fclose($file);
}
?>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.check').click(function() {
$('.check').not(this).prop('checked', false);
});
});
</script>
<BODY>
<form action = "post.php" method="POST">
<p>
<label>Login Name:</label><input type = "text" name = "name" /><br>
<input type="checkbox" type = "password" name="pwd[]" class="check" value="male"> Male
<input type="checkbox" type = "password" name="pwd[]" class="check" value="female"> Female
<input type="checkbox" type = "password" name="pwd[]" class="check" value="other"> Other
<br/>
<br/>
</p>
<input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
<input type = "reset" id = "reset" value = "reset"/>
</form>
</BODY>
</html>
uj5u.com熱心網友回復:
我認為首先你應該學習 php 的基礎知識,然后嘗試它,因為我可以看到代碼中存在語法錯誤和邏輯錯誤,即使你從其他地方復制了它,你應該在發布之前修復它們并嘗試它甚至沒有嘗試代碼,只是直接在這里發布。
uj5u.com熱心網友回復:
您的帖子陣列包含帶有您輸入欄位名稱的專案。這將起作用
<!DOCTYPE html>
<?php
if(isset($_POST['submit'])){
$Name = "Username:".$_POST['name']."
";
$Pass = "Password:".$_POST['pwd']."
";
$file=fopen("saved.txt", "a");
fwrite($file, $Name);
fwrite($file, $Pass,);
echo fwrite($file,"*************************",);
fclose($file);
}
?>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.check').click(function() {
$('.check').not(this).prop('checked', false);
});
});
</script>
<BODY>
<form action = "post.php" method="POST">
<p>
<label>Login Name:</label><input type = "text" name = "name" /><br>
<input type="checkbox" type = "password" name="pwd" class="check" value="male"> Male
<input type="checkbox" type = "password" name="pwd" class="check" value="female"> Female
<input type="checkbox" type = "password" name="pwd" class="check" value="other"> Other
<br/>
<br/>
</p>
<input type = "submit" name="submit" id = "submit" value = "submit"/>
<input type = "reset" id = "reset" value = "reset"/>
</form>
</BODY>
</html>
uj5u.com熱心網友回復:
您首先必須了解表單的作業原理。
您要求 和 的后$_POST["username"]資料$_POST["password"]。這些“名稱”由相應輸入的“名稱”屬性定義。因此,要獲得用戶名,您必須有一個像<input type="text" name="username" />.
現在看看您自己的 HTML:您已經為用戶名定義了一個 Input,但它有 Attribute name="name"。這將導致 Post-Data 存盤在$_POST["name"].
此外,您只有一個輸入,但要求輸入兩個值(用戶名和密碼)。
至于您的復選框,我不明白它們名稱的原因name="pwd[]",但您可能也應該更改它們。您還指定了兩種型別,但第二種型別type="password"無法識別。
編輯:我再次檢查并決定完全修改您的代碼:
<!DOCTYPE html>
<?php
if(isset($_POST['submit'])){
$Name = "Username:".$_POST['username'];
$gender = "Gender:".$_POST['gender'];
$file=fopen("saved.txt", "a");
fwrite($file, $Name);
fwrite($file, $Pass);
fwrite($file,"*************************");
fclose($file);
}
?>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.check').click(function() {
$('.check').not(this).prop('checked', false);
});
});
</script>
<form action = "post.php" method="POST">
<p>
<label>Login Name:</label><input type = "text" name = "username" />
<br>
<input type="checkbox" name="gender" class="check" value="male"> Male
<input type="checkbox" name="gender" class="check" value="female"> Female
<input type="checkbox" name="gender" class="check" value="other"> Other
<br/>
<br/>
</p>
<input type = "submit" name="submit_btn" id = "submit" value = "submit"/>
<input type = "reset" id = "reset" value = "reset"/>
</form>
</body>
</html>
我建議您了解有關 HTML 表單的更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419167.html
標籤:
下一篇:如何減少差距原因<br>
