仅当填写 _POST 时才将内容发送到变量
帮助!我在将一些条件语句放入表单的 php 脚本中时遇到一些问题。
这是 HTML 的要点:
<input type="text" name="address1" id="address1" class="required" />
<input type="text" name="address2" id="address2" />
<input type="text" name="city" id="city" />
<input type="text" name="postcode" id="postcode" class="required" />
php 看起来有点像这样:
$address1 = $_REQUEST["address1"] ;
$address2 = $_REQUEST["address2"] ;
$city = $_REQUEST["city"] ;
$postcode = $_REQUEST["postcode"] ;
$message =
"address: $address1, " .
"$address2, " .
"$city, " .
"$postcode" .
$sent = mail($to, $subject, $message, 'From:' . $email_address) ;
我对“address1”和“postcode”进行了一些 jQuery 验证,因此它们不是问题,因为它们总是返回一个值。如果未填写“address2”和“city”(也许填表者没有填写地址的第二行,或者没有按照他们认为正确的方式填写城市,邮政编码就足够了)在查找他们的地址时)他们仍然在电子邮件中返回一个值(“无”)。
我希望发生的是留空的表单输入,根本不发送任何电子邮件。换句话说,如果 $address2 变量为空,请不要向我发送“空格”。例如,如果全部完成,我会得到这个,这很棒:
地址:1 Test Street,West End,Test City,AB1 23C
如果缺少一个字段,我会得到这个,这有点乱:
地址:1 Test Street, , Test City, AB1 23C
但我喜欢这个,因为它漂亮又整洁:
地址:1 Test Street, Test City, AB1 23C
我一直在尝试了解 isset 函数,但没有任何运气。我也尝试过基本的“if”语句,但似乎也没有任何进展。我必须承认,php 不是我的强项,我在这方面花了很多时间但没有成功。非常感谢您的帮助!
非常感谢,
马丁。
Help! I'm having some trouble getting some conditional statements into my php script for a form.
This is gist of the HTML:
<input type="text" name="address1" id="address1" class="required" />
<input type="text" name="address2" id="address2" />
<input type="text" name="city" id="city" />
<input type="text" name="postcode" id="postcode" class="required" />
And the php looks a bit like this:
$address1 = $_REQUEST["address1"] ;
$address2 = $_REQUEST["address2"] ;
$city = $_REQUEST["city"] ;
$postcode = $_REQUEST["postcode"] ;
$message =
"address: $address1, " .
"$address2, " .
"$city, " .
"$postcode" .
$sent = mail($to, $subject, $message, 'From:' . $email_address) ;
I've got some jQuery validation on 'address1' and 'postcode' so they're not an issue as they'll always return a value. If 'address2' and 'city' aren't filled in (maybe the form-filler doesn't have a second line to their address, or doesn't put the city in as they think -rightly - that the postcode will be sufficient in finding their address) they still return a value (of 'nothing') on the email.
What I would like to happen is for the form inputs that are left blank to email nothing at all. In other words, if the $address2 variable is empty, don't send me the "blank space". For example, if it's all complete I'd get this, which is great:
address: 1 Test Street, West End, Test City, AB1 23C
If there's a field missing I'd get this, which is a bit messy:
address: 1 Test Street, , Test City, AB1 23C
But I'd like this, because it's nice and tidy:
address: 1 Test Street, Test City, AB1 23C
I've been trying to get my head around the isset function but haven't had any luck. I've also tried a basic 'if' statement but can't seem to get anywhere with that either. I must admit, php isn't my strong point and I've spent a lot of time on this with no success. Your help is very much appreciated!
Many thanks,
Martin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于这种“参数过滤”问题,我喜欢使用 PHP 数组函数。
例如:
它的作用:
参数。
使用 array_filter 函数
逗号(
implode
函数)。使用这种方法,即使有很多参数,代码仍然易于阅读。
For this kind of "parameters filtering" problematic i like to use PHP array functions.
For example:
What it does:
parameters.
with the
array_filter
functioncomma (
implode
function).With this method the code remains easy to read even if you have a lot of parameters.
在导入到数据库之前定义一个新的 php 变量
define a new php variable before importing to db
如果变量已被设置,则 isset() 返回 true - 即已声明并且具有非 NULL 值。除了 NULL 之外,isset() 没有提及该变量的内容。
要测试变量是否真正为空(空字符串、NULL、FALSE、0、空数组等),请使用恰当命名的empty() :)
示例:
执行上面提到的 Captain 的另一种方法:
它的工作方式:
你想要的 $_REQUEST 数组。
它的值从 $_REQUEST 中剥离出来
任何散乱的空白(与
修剪())。
将其附加到 $address。
细绳。
如果您以前没有见过,那个
条件? expr_if_true : expr_if_false
语法称为三元运算符。在我看来,稍微干净一点。运行的 if/else 语句更少。
isset() returns true if the variable has been set - i.e., that is has been declared and has a non-NULL value. Apart from that bit about NULL, isset() says nothing about the contents of that variable.
To test if a variable is truly empty (an empty string, NULL, FALSE, 0, an empty array, etc), use the aptly named empty() :)
Example:
Another way of doing what Captain mentioned above:
The way this works:
the $_REQUEST array you want.
its value out of $_REQUEST, strip
any straggling whitespace (with
trim()).
append it to $address.
string.
If you haven't seen it before, that
condition ? expr_if_true : expr_if_false
syntax is called the ternary operator.A tiny bit cleaner, IMO. Fewer if/else statements running around.