views:

357

answers:

2

Hi,

  I m new to Jquery..How to retrieve the values from my controller and compare them with some String inside JQuery..Please suggest me..

   $(".select"+increment).change(function () 
            {

          if("here i need the value from my controller " =='String')
               {
       $("<label id=labelstr"+stringinc+"  >"+labelname+"</label>").appendTo(".menu li");
       $("<input id=inputstr"+stringinc+" type= 'text' ></input>").appendTo(".menu li");
              }
         }
+1  A: 

[edit] : This answer is if by Controller you mean the Controller data passed in the ViewData object of a ASP.NET MVC project. [/edit]

The script will have to be in the aspx/ascx (not in a separate JS file).

    <script language="Javascript">

       $(".select"+increment).change(function () 
            {

          if("<%=ViewData["YourData"] %>" =='String')
               {
       $("<label id=labelstr"+stringinc+"  >"+labelname+"</label>").appendTo(".menu li");
       $("<input id=inputstr"+stringinc+" type= 'text' ></input>").appendTo(".menu li");
              }
         }
    </script>
Olivier PAYEN
No i want to get the php value returned from my controller..I m using Cakephp framework..In my controller i want to fetch values and comparing here in JQuery...
Aruna
A: 

Your question is very confusing but I will try my best to help. I'm going to assume you want to retrieve this information via AJAX. I'll also assume your CakePHP controller spits out the following when called (you can find out by going to: http://yoursite/Forms/views):

This is some random string

In order to use this as I've presumed, you will need to do this:

<script language="javascript">
$(function() {
    $(".select"+increment).change(function () {
        $.get('/Forms/views',{},function(data) {
            if(data == 'This is some random string') {
                // I have no idea where you are getting the
                // 'stringinc' and 'labelname' variables from.
                $("<label id=labelstr"+stringinc+"  >"+labelname+"</label>").appendTo(".menu li");
                $("<input id=inputstr"+stringinc+" type= 'text' ></input>").appendTo(".menu li");
            }
        });
    });
});
</script>

Now, if you are trying to do it the way Oliver is suggesting (except just in CakePHP), you would need to do this:

<script language="javascript">
$(function() {
    $(".select"+increment).change(function () {
        // I'm not familiar with Cake, you might need to use
        // some sort of template syntax. Either way, whatever
        // method you need to use to get the value into you view
        // let's just assume its called '$value_from_controller'.
        var data = <?= $value_from_controller; ?>;
        if(data == 'This is some random string') {
            // I have no idea where you are getting the
            // 'stringinc' and 'labelname' variables from.
            $("<label id=labelstr"+stringinc+"  >"+labelname+"</label>").appendTo(".menu li");
            $("<input id=inputstr"+stringinc+" type= 'text' ></input>").appendTo(".menu li");
        }
    });
});
</script>

I hope that helps in some way.

KyleFarris