amrnavy
amrnavy

Reputation: 35

jquery and json array

I stop 2 days and looking for the answer,
This is what my data looks like:

The field rows have this data:

rows :
[
   [
       "1",
       "test1",
       "pw85798",
       "GM",
       "38475786",
       "yr",
       "m",
       "20-11-1972",
       "100",
       "[email protected]"
   ],
   [
       "11",
       "test2",
       "pw78423786",
       "media",
       "",
       "yrer",
       "m",
       "11-05-1990",
       "1",
       "[email protected]"
   ],

I have 2 textboxes: usetext and pwstext
I want my loop to take the 2 values on the texts and look inside the array if usrtext = "test1" and pswtext = "pw85798". Then <div> will appear. If not, alert "error"
I need this code to go on.

I tried this:

db.openDoc(id, {
    success:function (result) {
        // Update revision input field

        $("#revision").val(result.rows[0,1]);

It returns all the first indexes:

1, test1, pw85798, GM, , 38475786, yr, m, ...

Upvotes: 0

Views: 88

Answers (2)

amrnavy
amrnavy

Reputation: 35

this is my page

<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
    <script src="/_utils/script/jquery.js"></script>
    <script src="/_utils/script/jquery.couch.js"></script>
<body>
<div id="databases"></div>
<form>
  <p>
  <input name="usrtext" type="text" id="usrtext"/>
  <br>
  <input  name="pswtext" type="text" id="pswtext"/>
  </p>
  <input id="test" type="text" name="test"/>
  <p><br/>
  <br/>
  <input type="submit" value="Submit" id="login"/>
  </p>
</form>
</body>
  <script>
var db = $.couch.db("aabc");
var id = "iams";
    //$('#login').submit(function()  {
    db.openDoc(id, {
    success:function (result) {
          result.rows.forEach(function(item)    {
          if($("#usrtext").val() == item[1] && $("#pswtext").val() == item[2])    
           {
            alert(1);
            $("#test").val(result._id);
            }
       else {
            alert(2);
            $("#test").val(result._rev);
        // do something else
            }
           });

        }
    //})
 });
  </script>
</html>

my json data

rows:[
  ["1","test1","12345","GM","7847427","alix","m","20-05-1975","100","[email protected]"],
  ["11","test2","54321","media","","tom","m","","1","[email protected]"]
  ]

when page loading alert(2) fire many times as my rows count if i had 5 its appear 5 times and val(result._rev); is work but if i inter the right value like test1 and 12345 ,alert (2) is appear not alert(1)

Upvotes: 0

cr0ss
cr0ss

Reputation: 877

I don't recommend and you should NOT use this code.

But I think this works as you want it to work, though it's really unadvised. Use it at your own risk.

I'm iterating over your json data and matching the user name and password to the content of the textboxes. I'm really not comfortable providing this lol.

'q' is a javascript object containing your json data.

q.forEach(function(item)    {
    if($("#user").val() == item[1] && $("#pass").val() == item[2])    {
        alert(1);
        // do something
    } else {
        alert(2);
        // do something else
    }
});

JSFiddle

Upvotes: 1

Related Questions