1

I am able to access the forms variables in my angularjs function per below (example s1 = Joe Smith).

But I need to modify the Indata variable to replace the a_searchvalue1 with what is in s1 with quote around it.

Orginal:

    var Indata = { what_to_do: "angular_users5",  where_clause: '[{"sqlvalue1":"a_earchvalue1","sqlvalue2":"b_earchvalue2"}]' }

so that is reads (note a_searchvalue1 is replaced with Joe Smith)

    var Indata = { what_to_do: "angular_users5",  where_clause: '[{"sqlvalue1":"Joe Smith","sqlvalue2":"b_earchvalue2"}]' }





<div id="myapp"  ng-controller="empcontroller">
    <input id="name1"  type="text" placeholder="Name"    required name="Name" value="Joe Smith">
    <input id="email1" type="text" placeholder="Email"   required name="Email" value="[email protected]">
    <p id="sample">demo1</p>
    <button ng-click="postData()">Submit</button><br>
    </div>

    <script>
    var app = angular.module('demoApp', []);

    app.controller('empcontroller', function($scope, $http)
                   {

                   $scope.postData = function ()
                   {
                   var s1 =  document.getElementById("name1").value;
                   alert(s1);

                   var Indata = { what_to_do: "angular_users5",  where_clause: '[{"sqlvalue1":"a_earchvalue1","sqlvalue2":"b_earchvalue2"}]' }

                   var req =
                   {
                   method: 'POST',url: 'angular_master.php',
                   headers: {'Content-Type':undefined},
                   params: Indata
                   }

                   $http(req).then(function (response)
                                   {
                                   $scope.names = response.data.records;
                                   document.getElementById("sample").innerHTML = "YOU CLICKED THE BUTTON";

                                   alert(angular.toJson(response.data.records));


                                   });

                   }

                   });


    </script>

also how can I handle if user put double quotes in input field

1
  • Use ng-model to bind object to input. You rarely ever use dom methods like getElementById in angular. Commented Sep 25, 2017 at 1:48

2 Answers 2

1

Since your where_clause key is a taking JSON array as a string you can use string concatenation like this:-

var Indata = {
  what_to_do: "angular_users5",
  where_clause: '[{"sqlvalue1":"'+s1+'",          
  "sqlvalue2":"b_earchvalue2"}]'
}

This will replace the value of a_searchvalue1 with s1 with quotes around it. See if this helps.

Sign up to request clarification or add additional context in comments.

Comments

0
  • var Indata = { what_to_do: "angular_users5", where_clause: '[{"sqlvalue1":s1,"sqlvalue2":"b_earchvalue2"}]' }

  • use encodeURIComponent()

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.