if $ var == blank set make it“XXX”?

时间:2012-06-25 10:57:52

标签: php forms

大家好,早上好。这是我关于这个主题的第二篇文章,因为第一次,事情仍然没有奏效,我现在已经尝试解决这个问题大约4/5天......

我有一个名为'edit.php'的文件,在这个文件中是一个表单;

<?php 
$company = $_POST["company"];
$phone = $_POST["phone"];
$colour = $_POST["colour"];
$email = $_POST["email"];
$website = $_POST["website"];
$video = $_POST["video"];
$image = $_POST["image"];
$extension = $_POST["extension"];
?>

<form method="post" action="generate.php"><br>
<input type="text" name="company" placeholder="Company Name" /><br>
<input type="text" name="slogan" placeholder="Slogan" /><br>
<input class="color {required:false}" name="colour" placeholder="Company Colour"><br>
<input type="text" name="phone" placeholder="Phone Number" /><br>
<input type="text" name="email" placeholder="Email Address" /><br>
<input type="text" name="website" placeholder="Full Website - Include http://" /><br>
<input type="text" name="video" placeholder="Video URL" /><br>
<input type="submit" value="Generate QuickLinks" style="background:url(images/submit.png) repeat-x; color:#FFF"/>
</form>

然后,在提交表单时,它使用已输入的变量创建文件。已填写的字段继续成为链接,我需要能够说'如果字段留空,则将'XXX'作为默认值'。有没有人有任何想法?我真的觉得我已经尝试了一切。我将在生成链接的.php文件下面放一个片段......

<?php 
 $File = "includes/details.php"; 
 $Handle = fopen($File, 'w');
 ?>

 <?php 
 $File = "includes/details.php";
 $Handle = fopen($File, 'w');
 $Data = "<div id='logo'>
          <img width='270px' src='images/logo.png'/img>
          <h1 style='color:#$_POST[colour]'>$_POST[company]</h1>
          <h2>$_POST[slogan]</h2>
          </div>

<ul>
       <li><a class='full-width button' href='tel:$_POST[phone]'>Phone Us</a></li>
       <li><a class='full-width button' href='mailto:$_POST[email]'>Email Us</a></li>
       <li><a class='full-width button' href='$_POST[website]'>View Full Website</a></li>
       <li><a class='full-width button' href='$_POST[video]'>Watch Us</a></li>
</ul>

 \n";

我真的很期待任何回应...

2 个答案:

答案 0 :(得分:2)

这很简单。

在你的php中,检查它是否为空:

(我将用电话号码输入演示)

if(empty($_POST['phone']))

//so here, you know it's empty (has a value of "") so now you can set it to whatever...
$phone="xxx";

}//end of empty phone
else{

$phone=$_POST['phone'];//set it

}//end of phone input not empty

答案 1 :(得分:1)

我建议更换

$_POST[varname]  

($_POST[varname]) ? htmlspecialchars($_POST[varname]) : 'XXXX';

的简短版本
if(isset($_POST[varname]))
    htmlspecialchars($_POST[varname]);
else
    'XXXX';

希望对你有所帮助。