仅当填写 _POST 时才将内容发送到变量

发布于 2024-10-30 10:16:04 字数 1298 浏览 0 评论 0原文

帮助!我在将一些条件语句放入表单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2024-11-06 10:16:04

对于这种“参数过滤”问题,我喜欢使用 PHP 数组函数。

例如:

<?php
$aMessage = array();

$aMessage[] = $_REQUEST["address1"] ;
$aMessage[] = $_REQUEST["address2"] ;
$aMessage[] = $_REQUEST["city"]     ;
$aMessage[] = $_REQUEST["postcode"] ;

$aMessage = array_filter($aMessage);

$message = (implode(', ', $aMessage));

echo $message;
?>

它的作用:

  1. 用不同的值填充数组
    参数。
  2. 删除空值
    使用 array_filter 函数
  3. 打印与 a 连接的值
    逗号(implode 函数)。

使用这种方法,即使有很多参数,代码仍然易于阅读。

For this kind of "parameters filtering" problematic i like to use PHP array functions.

For example:

<?php
$aMessage = array();

$aMessage[] = $_REQUEST["address1"] ;
$aMessage[] = $_REQUEST["address2"] ;
$aMessage[] = $_REQUEST["city"]     ;
$aMessage[] = $_REQUEST["postcode"] ;

$aMessage = array_filter($aMessage);

$message = (implode(', ', $aMessage));

echo $message;
?>

What it does:

  1. fill the array with the differents
    parameters.
  2. delete the empty values
    with the array_filter function
  3. print the values concatenated with a
    comma (implode function).

With this method the code remains easy to read even if you have a lot of parameters.

遗忘曾经 2024-11-06 10:16:04

在导入到数据库之前定义一个新的 php 变量

$address=$_REQUEST["address1"];

if (trim($_REQUEST["address2"]!=''))
$address.=','.$_REQUEST["address2"]

define a new php variable before importing to db

$address=$_REQUEST["address1"];

if (trim($_REQUEST["address2"]!=''))
$address.=','.$_REQUEST["address2"]
帝王念 2024-11-06 10:16:04

如果变量已被设置,则 isset() 返回 true - 即已声明并且具有非 NULL 值。除了 NULL 之外,isset() 没有提及该变量的内容

要测试变量是否真正为空(空字符串、NULL、FALSE、0、空数组等),请使用恰当命名的empty() :)

示例:

$a = NULL;
$b = '';
// $c we won't declare
echo isset($a); // False - $a has a NULL value
echo isset($b); // True - $b is set to a non-NULL Value. empty() will return True too.
echo isset($c); // False - $c is not set.

执行上面提到的 Captain 的另一种方法:

$address = "";
foreach( array( "address1", "address2", "address3" ) as $param )
{
    $param = trim($_REQUEST[$param]);
    $address .= !empty( $param ) ? ', '.$param : "";
}

它的工作方式:

  1. 定义元素的名称
    你想要的 $_REQUEST 数组。
  2. 你循环遍历每个元素,拉
    它的值从 $_REQUEST 中剥离出来
    任何散乱的空白(与
    修剪())。
  3. 如果参数不为空,
    将其附加到 $address。
  4. 如果为空,则追加一个空
    细绳。

如果您以前没有见过,那个条件? 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:

$a = NULL;
$b = '';
// $c we won't declare
echo isset($a); // False - $a has a NULL value
echo isset($b); // True - $b is set to a non-NULL Value. empty() will return True too.
echo isset($c); // False - $c is not set.

Another way of doing what Captain mentioned above:

$address = "";
foreach( array( "address1", "address2", "address3" ) as $param )
{
    $param = trim($_REQUEST[$param]);
    $address .= !empty( $param ) ? ', '.$param : "";
}

The way this works:

  1. Define the names of the elements in
    the $_REQUEST array you want.
  2. You loop through each element, pull
    its value out of $_REQUEST, strip
    any straggling whitespace (with
    trim()).
  3. If the parameter is not empty,
    append it to $address.
  4. If it is empty, you append an empty
    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文