![](/img/trans.png)
[英]How to make sure that inputs stay filled when a popup comes up (PHP/HTML)
[英]How to make sure all fields are filled in php?
你好。 我試圖寫一些php將輸入導出為csv格式。 它確實工作得很好,除了它不檢查以確保所有字段都已填充。 我如何確保它們都已滿。
<?php
$txt = "report.csv";
$fh = fopen($txt, 'a+');
if (isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['version']) && isset($_POST['description'])) { // check if both fields are set
$first = $_POST['firstname'];
$last = $_POST['lastname'];
$version = $_POST['version'];
$description = $_POST['description'];
$q = "\"";
$c = ",";
$check=$first.$last.$version.$descrption;
$txt=$q.$first.$q.$c.$q.$last.$q.$c.$q.$version.$q.$c.$q.$description.$q;
if (strpos($check, '"') !== false){
echo file_get_contents("/quotes.html");
} else {
file_put_contents('report.csv',$txt."\n",FILE_APPEND); // log to data.txt
echo file_get_contents("yay.html");
exit();
}
} else {
echo file_get_contents("notfilled.html");
}
?>
HTML
<form action="problem.php" method="POST">
<label for="fname"><h3>First Name</h3></label>
<input type="text" id="fname" name="firstname">
<label for="lname"><h3>Last Name</h3></label>
<input type="text" id="lname" name="lastname">
<label for="version"><h3>Version</h3></label>
<select id="version" name="version">
<option value="0.1">0.1</option>
<option value="0.2">0.2</option>
<option value="Other">Other</option>
</select>
<label for="description"><h3>Description</h3></br><p1>Please describe your problem with details. Explain what the problem is how to reproduce it.</label>
<textarea id="description" name="description" style="height:100px"></textarea>
<input type="submit" value="Submit">
</form>
您需要添加!empty
以檢查值是否為空。 如果您也想檢查其他情況,例如名稱不應該是數字,那么您也應該使用regex
。
if (!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['version']) && !empty($_POST['description'])) {
對您發表評論Whats the difference from !empty and isset
?
假設你有一個變量
$test= "";
if(empty($test)) // it return true because "" is empty
if(isset($test)) // return true because $test is defined
而
if(empty($anotherTest)) // return true because its null
if(isset($anotherTest)) // return false because is not defined
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.