2

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

   $(".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");
              }
         }
4
  • You'll have to be more specific - what type of controller, is it housed on a server? What are you trying accomplish? You need a better introduction to your question as it is difficult to discern what you are trying to achieve. Commented May 20, 2009 at 12:17
  • No idea what this question is about. jQuery doesn't have anything called a "controller" Commented May 20, 2009 at 12:41
  • 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...My controller is Forms and action is views..In that action i m fetching all values from a table forms.. Commented May 20, 2009 at 12:58
  • You shoud edit your question and title and add the corresponding tags. Commented May 20, 2009 at 13:18

2 Answers 2

1

[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>
Sign up to request clarification or add additional context in comments.

1 Comment

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...
0

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.

Comments

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.