OverRide15
OverRide15

Reputation: 29

How to modify an angular variable string that is passed to a php

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

Upvotes: 1

Views: 49

Answers (2)

Siddhesh Phatak
Siddhesh Phatak

Reputation: 198

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.

Upvotes: 1

Nitin Ayir
Nitin Ayir

Reputation: 54

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

  • use encodeURIComponent()

Upvotes: 0

Related Questions