且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP isset()函数与多个参数

更新时间:2023-02-23 21:58:16

参数(S),以 isset()函数必须是一个变量引用而不是前pression(在你的情况下,串联);但你可以将多个条件放在一起是这样的:

 如果(使用isset($ _ POST ['SEARCH_TERM'],$ _ POST ['交code'])){
}
 

这将返回仅在所有的参数 isset()函数设置并且不包含

注意使用isset($ VAR)使用isset($ VAR)==真有同样的效果,因此后者是有些多余。

更新

你的前pression用途的第二部分空()是这样的:

 空($ _ POST ['SEARCH_TERM'。$ _ POST ['交code'])==假
 

这是错误的,与上述相同的原因。其实,你并不需要空()在这里,因为到那个时候,你就已经检查变量是否设置,这样你就可以快捷的完成EX pression像这样:

 使用isset($ _ POST ['SEARCH_TERM'],$ _ POST ['交code'])及和放大器;
    $ _ POST ['SEARCH_TERM']&功放;&安培;
    $ _ POST ['交code']
 

或使用等效EX pression:

 空($ _ POST ['SEARCH_TERM'])及!&安培; !空($ _ POST ['交code'])
 

最后的想法

您应该考虑使用过滤器功能来管理输入:

  $数据= filter_input_array(INPUT_POST,阵列(
    SEARCH_TERM'=>阵列(
        '过滤器'=> FILTER_UNSAFE_RAW,
        '标志'=> FILTER_NULL_ON_FAILURE,
    ),
    后code'=>阵列(
        '过滤器'=> FILTER_UNSAFE_RAW,
        '标志'=> FILTER_NULL_ON_FAILURE,
    ),
));

如果($的数据===空|| in_array(空,$的数据,真实的)){
    //有些领域缺少或它们的值没有通过过滤器
    死(你做了一件顽童);
}

// $数据['SEARCH_TERM']和$数据['交code']包含所需的字段
 

顺便说一句,你可以自定义过滤器来检查提交的值的各个部分。

I'm trying to work with AJAX autocompletes and I am having a few problems with getting the two languages to work in synergy.

When I replace all the issets with only 1 $_POST the snippet below will work, however by adding another $_POST I get an error on line 5.

<?php 

require_once '../Configuration.php';
if (isset($_POST['search_term'] . $_POST['postcode']) == true && empty ($_POST['search_term'] . $_POST['postcode']) == false) {
$search_term = mysql_real_escape_string($_POST['search_term'] . $_POST['postcode']);
$query = mysql_query("SELECT `customer_name`,`postcode` FROM `Customers` WHERE `customer_name` LIKE '$search_term%' ");
while(($row = mysql_fetch_assoc($query)) !== false) {
    //loop
    echo '<li>',$row['customer_name'] . $row['postcode'] '</li>';
}
}


?>

Any advice on why it is throwing this error would be much appreciated. Thanks.

I understand I should be using mysqli, I am just trying to get the logic first :)

Js:

Primary.js:

$(document).ready(function() {
$('.autosuggest').keyup(function() {

    var search_term = $(this).attr('value');
    var postcode = $_GET['postcode'];
    //alert(search_term); takes what is typed in the input and alerts it
    $.post('ajax/search.php', {search_term:search_term, postcode:postcode},     function (data) {
        $('.result').html(data);
        $('.result li').click(function() {
            var result_value = $(this).text();
            $('.autosuggest').attr('value', result_value);
            $('.result').html('');

        });
    });
});
});

The parameter(s) to isset() must be a variable reference and not an expression (in your case a concatenation); but you can group multiple conditions together like this:

if (isset($_POST['search_term'], $_POST['postcode'])) {
}

This will return true only if all arguments to isset() are set and do not contain null.

Note that isset($var) and isset($var) == true have the same effect, so the latter is somewhat redundant.

Update

The second part of your expression uses empty() like this:

empty ($_POST['search_term'] . $_POST['postcode']) == false

This is wrong for the same reasons as above. In fact, you don't need empty() here, because by that time you would have already checked whether the variables are set, so you can shortcut the complete expression like so:

isset($_POST['search_term'], $_POST['postcode']) && 
    $_POST['search_term'] && 
    $_POST['postcode']

Or using an equivalent expression:

!empty($_POST['search_term']) && !empty($_POST['postcode'])

Final thoughts

You should consider using filter functions to manage the inputs:

$data = filter_input_array(INPUT_POST, array(
    'search_term' => array(
        'filter' => FILTER_UNSAFE_RAW,
        'flags' => FILTER_NULL_ON_FAILURE,
    ),
    'postcode' => array(
        'filter' => FILTER_UNSAFE_RAW,
        'flags' => FILTER_NULL_ON_FAILURE,
    ),
));

if ($data === null || in_array(null, $data, true)) {
    // some fields are missing or their values didn't pass the filter
    die("You did something naughty");
}

// $data['search_term'] and $data['postcode'] contains the fields you want

Btw, you can customize your filters to check for various parts of the submitted values.