如何修改传递给php

时间:2017-09-25 01:20:28

标签: javascript angularjs

我可以在下面的angularjs函数中访问表单变量(例如s1 = Joe Smith)。

但我需要修改Indata变量,用s1中的内容替换a_searchvalue1,并附上引号。

原单:

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

所以这是读取(注意a_searchvalue1被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="webcastpoa@gmail.com">
    <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>

如果用户在输入字段

中放置双引号,我该如何处理

2 个答案:

答案 0 :(得分:1)

由于您的where_clause密钥是将JSON数组作为字符串,因此您可以使用字符串连接: -

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

这将用a1替换a_searchvalue1的值,并在其周围加上引号。 看看这是否有帮助。

答案 1 :(得分:0)

  • var Indata = {what_to_do:&#34; angular_users5&#34;,where_clause:&#39; [{&#34; sqlvalue1&#34;:s1,&#34; sqlvalue2&#34;:& #34; b_earchvalue2&#34;}]&#39; }

  • 使用encodeURIComponent()