htmlspecialchars() expects parameter 1 to be string array given - html

I have an input field in which I am passing an array from my controller as hidden input but its giving me this error of array given.
Code of my view is
<input type="hidden" name="DiseaseDiagnosed[]" value="{{$DiseaseDiagnosed}}">
Code of controller passing the value to view is
return view('/doctorPanel/diagnoseDisease', ['chart' => $chart, 'patient' => $patient, 'symptomsStated' => $symptomsStated, 'DiseaseDiagnosed' => $DiseaseDiagnosed]);
Kindly tell me why I am getting this error

<input type="hidden" name="DiseaseDiagnosed[]" value="{!! jsond_encode($DiseaseDiagnosed) !!}">
Actually, your input is DiseaseDiagnosed is an array which is returned to view.
So you have to use {{ json_decode($DiseaseDiagnosed) }}
You can also try
#foreach($DiseaseDiagnosed as $disease)
<input type="hidden" name="DiseaseDiagnosed[]" value="{{ $disease }}">
#endforeach

Blade Template engine is producing this error. you can't print array like this using {{ }}. When passing this value, you can encode it using 'DiseaseDiagnosed'=>json_encode($DiseaseDiagnosed]), then you can use that syntax. After that when you want to use this value, don't forget to decode it using json_decode()

In order to create an array with inputs you need to have 1 input for each value inside the array. You are appending an array on the value when it only accepts Strings so thats why it warns you that an array was given when a String was expected.
As #Adnan suggested you can solve this using:
#foreach($DiseaseDiagnosed as $disease)
<input type="hidden" name="DiseaseDiagnosed[]" value="{{ $disease }}">
#endforeach
Then in your controller you will recieve the array DiseaseDiagnosed with all the values you inserted, eg: You will recieve all the values within the same array->
dd($request->DiseaseDiagnosed);
// You will see this is an array with all the values

Related

How do I POST an array to the server?

I am trying to post an array of multiple geolocation addresses (generated by Google maps) to my server. The array displays correctly as an array while on the client side, however, the array arrives at the server as a string and not as an array. I am not entirely sure why this happens, also I can't figure out how to convert this string back into an array on the server.
Find below the code of the array while on the client side via the browser:
console.log("Geolocations: " ,selecedAddresses);
This codeabove yields an array of 3 items as illustrated below:
Geolocations: (3) ['Kileleshwa, Nairobi, Kenya', 'Ntinda, Kampala, Uganda', 'Uhuru Highway, Nairobi, Kenya']
When the same array is expanded for better readability:
0: "Kileleshwa, Nairobi, Kenya"
1: "Ntinda, Kampala, Uganda"
2: "Uhuru Highway, Nairobi, Kenya"
length: 3
For me to be able to POST the array to the server, I assign the array value to an HTML input field in a form.
document.getElementById('locationArray').value = selecedAddresses;
Find below the code to my HTML form where input field resides:
<form action="/advertiser" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<input type="text" name="locationArray" value="" hidden/>
</div>
<button type="submit" class="btn btn-secondary"> Add location </button>
</form>
When the user clicks on the Add location button, the array is submitted to the server.
And now for the server side code...
app.route('/Geolocations')
.post((req, res)=>{
let locationArray = req.body.locationArray;
console.log("locationArray: " +locationArray);
})
The code above yeilds:
locationArray: "Kileleshwa, Nairobi, Kenya, Ntinda, Kampala, Uganda, Uhuru, Highway, Nairobi, Kenya"
Instead of the desired out put:
['Kileleshwa, Nairobi, Kenya', 'Ntinda, Kampala, Uganda', 'Uhuru Highway, Nairobi, Kenya']
Looking forward to your help.

How to insert python variable inside the value of a form?

I am trying to make a timer that gets its time from a python variable. But whatever I am doing it's not working.
Here is my python piece of code.
#blogs.route('/individual_set/<int:set_id>')
def individual_set(set_id):
individual_page = Post.query.get_or_404(set_id)
return render_template('forms_page.html', individual_page=individual_page,time_limit = 25)
According to the above code, I am wanting to make the input value to time_limit = 25
Here is the HTML part
<input name = 'set_time' type="hidden" id="set-time" value='1'>
<div id="countdown">
<div id="tiles" class="color-full"></div>
</div>
Inserting {{time_limit}} does not work whereas plain value like value ='5' works
Doing {{ time_limit }} instead of {{time_limit}} worked.

Angularjs convert string to object inside ng-repeat

i've got a string saved in my db
{"first_name":"Alex","last_name":"Hoffman"}
I'm loading it as part of object into scope and then go through it with ng-repeat. The other values in scope are just strings
{"id":"38","fullname":"{\"first_name\":\"Alex\",\"last_name\":\"Hoffman\"}","email":"alex#mail","photo":"img.png"}
But I want to use ng-repeat inside ng-repeat to get first and last name separate
<div ng-repeat="customer in customers">
<div class="user-info" ng-repeat="name in customer.fullname">
{{ name.first_name }} {{ name.last_name }}
</div>
</div>
And I get nothing. I think, the problem ist, that full name value is a string. Is it possible to convert it to object?
First off... I have no idea why that portion would be stored as a string... but I'm here to save you.
When you first get the data (I'm assuming via $http.get request)... before you store it to $scope.customers... let's do this:
$http.get("Whereever/You/Get/Data.php").success(function(response){
//This is where you will run your for loop
for (var i = 0, len = response.length; i < len; i++){
response[i].fullname = JSON.parse(response[i].fullname)
//This will convert the strings into objects before Ng-Repeat Runs
//Now we will set customers = to response
$scope.customers = response
}
})
Now NG-Repeat was designed to loop through arrays and not objects so your nested NG-Repeat is not necessary... your html should look like this:
<div ng-repeat="customer in customers">
<div class="user-info">
{{ customer.fullname.first_name }} {{ customer.fullname.last_name }}
</div>
This should fix your issue :)
You'd have to convert the string value to an object (why it's a string, no idea)
.fullname = JSON.parse(data.fullname); //replace data.fullname with actual object
Then use the object ngRepeat syntax ((k, v) in obj):
<div class="user-info" ng-repeat="(nameType, name) in customer.fullname">
{{nameType}} : {{name}}
</div>
My advise is to use a filter like:
<div class="user-info"... ng-bind="customer | customerName">...
The filter would look like:
angular.module('myModule').filter('customerName', [function () {
'use strict';
return function (customer) {
// JSON.parse, compute name, foreach strings and return the final string
};
}
]);
I had same problem, but i solve this stuff through custom filter...
JSON :
.........
"FARE_CLASS": "[{\"TYPE\":\"Economy\",\"CL\":\"S \"},{\"TYPE\":\"Economy\",\"CL\":\"X \"}]",
.........
UI:
<div class="col-sm-4" ng-repeat="cls in f.FARE_CLASS | s2o">
<label>
<input type="radio" ng-click="selectedClass(cls.CL)" name="group-class" value={{cls.CL}}/><div>{{cls.CL}}</div>
</label>
</div>
CUSTOM FILTER::
app.filter("s2o",function () {
return function (cls) {
var j = JSON.parse(cls);
//console.log(j);
return j;
}
});

What is colon:message `:message` in form

I want to know more about how :message works. I see in the official docs how to use it but I don't understand where the value is set and how it becomes a unique variable for each input. I can't it find it in the API either.
<div class="form-group {{ $errors->has('first_name') ? 'has-error' : '' }}">
<label for="first_name">First Name</label>
<input type="text" class="form-control" id="first_name">
{{ $errors->first('first_name', '<p class="help-block">:message</p>') }}
</div>
where can I find more information about :message. what does the : signify and so on? I can't google it. So I hope someone can direct me to a reference.
What you pass in as second argument to first() is the format of the error message. As mentioned in the comments, the :message part gets replaced by the actual error message.
It all happens inside the MessageBag class
You call first(). Inside the function $this->get() gets called
$messages = is_null($key) ? $this->all($format) : $this->get($key, $format);
Inside get it will check if there's a message for the passed key and if positive $this->transform() will be called
if (array_key_exists($key, $this->messages))
{
return $this->transform($this->messages[$key], $format, $key);
}
Now it finally gets interesting. Within transform, The keywords :message and :key will be replaced with the actual message and the actual key ($format is the string you passed in at the beginning '<p class="help-block">:message</p>')
$replace = array(':message', ':key');
$message = str_replace($replace, array($message, $messageKey), $format);

Iterate with index using thymeleaf

I am new to thymeleaf and am converting all my jsp code to thymeleaf.I don't know how to convert this below code to thymeleaf.Do anyone know how to convert the below code to thymeleaf?
<logic:iterate id="id" property="idList" name="sampleForm" indexId="i">
<label for="id<%=i%>">
<bean:write name="id" property="id" />
</label>
</logic:iterate>
Please tell me how to initialize the index value in thymeleaf to be used in some values??
<label th:each="id,status : ${idList}" th:for="|id${status.index}|" th:text="${id.id}"></label>
th:each will iterate over the idList, assign each item to id and create a label for each item. The status of the item can be assigned by adding an extra name, separated by a comma (status in this example).
th:for will set the for attribute of the label. The pipes (|) are used for easy string concatenation.
th:text will set the inner text of the label to the ID.
You can also use it like this:
<label th:each="id : ${idList}" th:for="${'id' + idStat.index}" th:text="{id.id}">
This starts the index from 0
If you want to start the index from 1 use this
<label th:each="id : ${idList}" th:for="${'id' + idStat.count}" th:text="{id.id}">
Check out the Thymeleaf documentation