用ajax获取不需要的字符串

时间:2016-09-15 09:31:18

标签: javascript php jquery ajax

我正在做一个php项目,我有使用ajax验证文本框的功能,并且ajax工作正常,但它只给$ return_value == - 4

   "<br />
<font size='1'><table class='xdebug-error xe-deprecated' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Deprecated: Function split() is deprecated in C:\wamp64\www\chargenetlive\require\functions.php on line <i>549</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.1225</td><td bgcolor='#eeeeec' align='right'>246464</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\chargenetlive\controller\jqueryphp.php' bgcolor='#eeeeec'>...\jqueryphp.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>7.1837</td><td bgcolor='#eeeeec' align='right'>497560</td><td bgcolor='#eeeeec'>CheckChargeNetID( ??? )</td><td title='C:\wamp64\www\chargenetlive\controller\jqueryphp.php' bgcolor='#eeeeec'>...\jqueryphp.php<b>:</b>12</td></tr>
</table></font>
The ID you entered is invalid..0003 0000 0055 Please follow this fromat xxxx xxxx xxxx and third 4 digit between 0 to 10"

只有我需要这个文字“您输入的ID无效...... 0003 0000 0055请遵循此fromat xxxx xxxx xxxx和0到10之间的第三个4位”

这是我在html页面中使用的ajax代码

$('#useridtxt').on('keyup', function(){
                // ajax request server
                $.ajax({ url: 'controller/jqueryphp.php', type: 'POST', dataType: 'text', data: {nfcid: document.getElementById("useridtxt").value},
                     success: function(data, textStatus, jqXHR)
                    {

                        if(data.length <=1000){
                         $('#id-validation').text(data);
                        }else{
                          $('#id-validation').text('');
                         }
                        //data - response from server
                    }
                });
            });

在php页面中我有以下代码

if(isset($_POST["id"])){
    if(!empty($_POST["id"])){
        $id =$_POST["id"];         

        $return_value=CheckID($id);
        if($return_value==-1){
            echo 'The NFC ID you entered is invalid..'.$id.' Please follow this fromat xxxx xxxx xxxx ';
        }elseif ($return_value==-2) {
            echo 'The NFC ID you entered is invalid..'.$id.' Please follow this fromat xxxx xxxx xxxx ';
        }elseif ($return_value==-3) {
            echo 'The NFC ID you entered is invalid..'.$id.' first 4 digit between 0 to 9';
        }elseif ($return_value==-4) {
            echo 'The NFC ID you entered is invalid..'.$id.' Please follow this fromat xxxx xxxx xxxx and third 4 digit between 0 to 10'; 
        }else {
            $CustomerNFC=getCustomerNFCdigit(getNFCIDdigits($return_value));
            if ($CustomerNFC !=''){
                $CustomerUserAll =getUserNamebyNFCID($CustomerNFC,$db);
                if(sizeof($CustomerUserAll) >= 1){
                    echo '';
                }else {
                    echo 'The NFC ID you entered is invalid..'.$id;
                }
            }else {
                echo 'The NFC ID you entered is invalid..'.$id;
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您可以执行此操作以从HTML中删除文本。

$('#useridtxt').on('keyup', function(){
                // ajax request server
      $.ajax({ url: 'controller/jqueryphp.php', type: 'POST', dataType: 'text', data: {nfcid: document.getElementById("useridtxt").value},
             success: function(data, textStatus, jqXHR)
                {

                    if(data.length <=1000){
                       var temEl = document.createElement("div")
                       temEl.innerHTML(data)
                       var text = temEl.childNodes[2]
                      $('#id-validation').text(text);
                     }else{
                       $('#id-validation').text('');
                     }
                   //data - response from server
                 }
               });
            });

最好的选择是在将HTML标记发送到客户端之前将其剥离。 您可以在PHP中使用strip_tags()执行此操作。