我想在自定义表单字段留空时放置默认电子邮件地址。我无法正确使用代码。我使用电子邮件地址代替真实的电子邮件地址。
<?php if(get_field('cemail')) { ?>
<?php
$email = (get_field('cemail'));
if($email!=""){
echo 'email address' ;
}
?>
答案 0 :(得分:1)
你可以这样做;
<?php
$email = $_GET["email"];
if($email=="" || !filter_var($email, FILTER_VALIDATE_EMAIL) === false)
$email = "default_email@email.com";
echo $email;
?>
答案 1 :(得分:1)
如果您只想在电子邮件字段为空时替换默认值。你可以这样做:
$email = (isset($_GET['cemail'])) ? $_GET['cemail'] : "defaultemail@email.com";
不要忘记确保提交的值是有效的电子邮件地址,并且如果值将进入数据库,请始终清理输入!
答案 2 :(得分:1)
如果字段为空,如何显示默认值?
我假设你的程序方法get_field()
只是返回全局变量GET
或POST
参数的值,所以......
这可以通过称为三元表达式的一行来实现,下面显示了一个例子。
<form action="/member.php" method="post">
<input name='cemail' value="<?php echo (!empty(get_field('cemail')) ? get_field('cemail') : 'default@me.com'; ?>" id="cemail">
</form>
我们首先检查字段是否为空(!
),然后在三元表达式中使用该结果来指示要执行的操作。
以下是一些可帮助您进一步了解上述情况的来源:
答案 3 :(得分:0)
试试这个:
<?php if(get_field('cemail'))
{
$email = (get_field('cemail'));
if(trim($email) == "")
$email = 'default@email.com';
}
echo $email;
}
?>