0

PHP:

$ports = $Db->query('SELECT port FROM servers');

Javascript:

$("#port").on('keyup', function(){
    var port = $("#port").val();
    var portlist = <?php print(json_encode($ports)); ?>;

    if(jQuery.inArray(port, portlist[port])!==-1)
    {
        $("#result").removeClass("label-success");
        $("#result").removeClass("label-warning");
        $("#result").addClass("label label-danger");
        $("#result").html("Port belegt!");
    }
    else
    {
        $("#result").removeClass("label-danger");
        $("#result").removeClass("label-warning");
        $("#result").addClass("label label-success");
        $("#result").html("Port Frei!");
    }



})

Why is this not working?

The array contains 1234 and 7777. So if I type 7777 into the html input field (#port) it should do the first action (if true)

But it always goes into the else action.

2
  • 1
    Change if(jQuery.inArray(port, portlist[port])!==-1) to if(jQuery.inArray(port, portlist)!==-1). Otherwise you'd need to check if portlist[port] exists and that's not inArray. Commented Jan 30, 2014 at 13:49
  • Hmm okay. But then the variable portlist only contains [object Object] Commented Jan 30, 2014 at 14:17

2 Answers 2

1

you need to cast integers in array to string by quoting and remove [port] here is a working example of your code http://jsfiddle.net/4X68y/

 var portlist = ["1234","777"];

    if(jQuery.inArray(port, portlist)!==-1)
Sign up to request clarification or add additional context in comments.

7 Comments

So the values from php are strings, right? Without the mysql query (var portlist = ["1234","777"];), it works..
well thats my guess. check the source code if php outputs [1234,7777] or ["1234","7777"]. if its the first, you can always typecast integer values with (string)$intval
Thats the output from php (print_r): Array ( [0] => Array ( [port] => 7777 ) [1] => Array ( [port] => 1234 ) )
ports seem to be integer. just iterate the array and parse them to string. foreach($ports as $key=>$port) { $ports[$key] = (string)$port; }
then try this: foreach($ports as $port) { $portArr[] = $port['port']; }; $portJson = '["' . implode('","',$portArr) . '"]';
|
0

jQuery inArray accepts array as second parameter - not specific key.

Try this:

$("#port").on('keyup', function(){
var port = $(this).val();
var portlist = <?php print(json_encode($ports)); ?>;

if(jQuery.inArray(port, portlist)!==-1)
{
    $("#result").removeClass("label-success");
    $("#result").removeClass("label-warning");
    $("#result").addClass("label label-danger");
    $("#result").html("Port belegt!");
}
else
{
    $("#result").removeClass("label-danger");
    $("#result").removeClass("label-warning");
    $("#result").addClass("label label-success");
    $("#result").html("Port Frei!");
}

})

--EDIT--

The following works for me (if the portlist is array not json object. If it isnt maybe it's a good idea to just loop the results and create the array old fashioned way like below)

PHP:

<?php
$q = $db->query("SELECT port FROM ports");
$ports = array();
while($$row = $result->fetch_assoc()){ 
            $ports[] = $row['port'];
        } 
?>

JS CODE:

$("#port").on('keyup', function(){
var port = parseInt($(this).val());
var portlist = <?php print(json_encode($ports)); ?>;

if(jQuery.inArray(port, portlist)===-1)
{
    $("#result").removeClass("label-success");
    $("#result").removeClass("label-warning");
    $("#result").addClass("label label-danger");
    $("#result").html("Port belegt!");
}
else
{
    $("#result").removeClass("label-danger");
    $("#result").removeClass("label-warning");
    $("#result").addClass("label label-success");
    $("#result").html("Port Frei!");
}
})

6 Comments

but without the specified key, I only get [object Object] :S
Could you please past what have you got in portlist variable after printing json_encode($ports)?
Sure. I get [{"port":7777},{"port":1234}]
As i thought - u're getting it's parsed as json object that's why you're getting back object - pretty obvious right? Look my edit for answer :) Best regards.
I'm getting confused :S. Your example does not use the php array, right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.