[英]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).我可以访问下面的 angularjs 函数中的表单变量(例如 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.但是我需要修改 Indata 变量以将 a_searchvalue1 替换为 s1 中的内容,并在其周围加上引号。
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)所以这是读取(注意 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>
also how can I handle if user put double quotes in input field如果用户在输入字段中放置双引号,我该如何处理
Since your where_clause
key is a taking JSON array as a string you can use string concatenation like this:-由于您的
where_clause
键是将 JSON 数组作为字符串,您可以像这样使用字符串连接:-
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.这将用带有引号的 s1 替换 a_searchvalue1 的值。 See if this helps.
看看这是否有帮助。
var Indata = { what_to_do: "angular_users5", where_clause: '[{"sqlvalue1":s1,"sqlvalue2":"b_earchvalue2"}]' } var Indata = { what_to_do: "angular_users5", where_clause: '[{"sqlvalue1":s1,"sqlvalue2":"b_earchvalue2"}]' }
use encodeURIComponent()使用 encodeURIComponent()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.