PHP isset()函数与多个参数 [英] PHP isset() with multiple parameters

查看:2308
本文介绍了PHP isset()函数与多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用AJAX自动完成工作,我有几个问题,让这两种语言的协同工作。

当我代替所有的issets只有1 $ _ POST代码段将在下面的工作,但通过添加另一个$ _ POST我得到一个错误,在第5行。

 < PHP

require_once'../Configuration.php;
如果(使用isset($ _ POST ['SEARCH_TERM'] $ _ POST ['交code'])==真放大器;&安培;空($ _ POST ['SEARCH_TERM'] $ _ POST ['交code '])== FALSE){
$ SEARCH_TERM = mysql_real_escape_string($ _ POST ['SEARCH_TERM'] $ _ POST ['交code']);
$查询=请求mysql_query(选择`customer_name`,`后code`从`客户`其中`customer_name` LIKE'$ SEARCH_TERM%');
而(($行= mysql_fetch_assoc($查询))!= FALSE){
    //循环
    回声'<李>',$行['CUSTOMER_NAME']。 $行[后code']'< /李>';
}
}


?>
 

这是为什么它抛出这个错误将是非常美联社preciated任何意见。谢谢你。

我明白我应该使用mysqli的,我只是想先拿到的逻辑:)

记者:

Primary.js:

  $(文件)。就绪(函数(){
$('自动提示)。KEYUP(函数(){

    VAR SEARCH_TERM = $(本).attr('值');
    变种后code = $ _GET [后code'];
    //警报(SEARCH_TERM);需要什么类型的输入和警报它
    .post的$('AJAX / search.php中',{SEARCH_TERM:SEARCH_TERM,交code:交code},功能(数据){
        $('。结果')的HTML(数据)。
        $('。结果礼)。点击(函数(){
            VAR result_value = $(本)的.text();
            $('自动提示。)ATTR('值',result_value)。
            $('。结果)HTML('')。

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

解决方案

参数(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.

这篇关于PHP isset()函数与多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆