Laravel IF Multiple values in Cookie - php

I have a cookie in Laravel and a variable from my server.
I want to validate the result of my server's variable with the result of the cookie.
Normally I would use this but the detail is that my server's cookie has multiple values separated by commas and inside one of them is the value I need.
#if (Cookie::get('DeliveryToken') == '{{ $orderitemaddons32 }}')
{{ Cookie::get('DeliveryToken') }}
#else
No se encontro
#endif
How do I get it to detect the comma-separated value?
I give you an example.
My variable to compare
"{{ $orderitemaddons32 }}" value = DEL-43421
"DeliveryToken" value: delton,DEL-43421,hp-32
As you can see the exact value after the first comma I need the if to parse all the values after the comma.
Or if it is in the first one to stop there and give the result of the if

A simple inline method for doing this would be convert your comma separated string into an array using explode().
Then check that the value you expect is inside that array with in_array().
For example:
in_array($check_value, explode(',', $cookie))
If this is something you frequently do, you may want to create a service to put this logic and other cookie based manipulations in.

Related

How to pass input text with commas through laravel form?

I would like to pass string input with commas through laravel form text field. You can provide multiple numbers separated by commas, the application will handle imploding into array.
The issue is that laravel (or web browser) by default will change comma to '%2C'. I know it's a safety feature, but this is not too big concern in case of this application.
Is there a way to disable this?
Already tried to use {!! !!} instead of standard {{ }}
{{ Form::text('number', null) }}
Try using POST method in form tag instead of GET method.
It will not show field value in URL.

Symfony form.vars.data vs form.vars.value

A FormView object in Symfony contains several variables, that can be accessed via twig using the public vars property.
Two of those variables are value and data.
So, supposing that we have our form variable in twig, we can access them using form.vars.data and form.vars.value.
The documentation is clear about the meaning of those properties:
value: The value that will be used when rendering (commonly the value HTML attribute).
data: The normalized data of the type.
but when I use {{ dump(form.vars) }} and compare form.vars.value and form.vars.data they look like identical. Why? What's the correct meaning and proper usage of those two properties?
Take for example a DateType field.
Here, value would be something like the string 2016-06-10.
data on the other hand would be a corresponding DateTime-Object.
When using Text fields, you will not see any difference because in both cases there will be just a string.

Check if a string is empty or not using Ajax, HTML and PHP

I am populating a HTML table with data from ajax as follows:
...
<td>{{data.ordered}}</td>
<td>{{data.quantity}}</td>
<td >{{data.pending}}</td>
<td>{{data.unit_name}}</td>
<td>{{data.material_name}}</td>
<td>{{data.service_name}}</td>
<td>{{data.cost}}</td>
<td>{{data.net}}</td>
...
I need to check if a value is empty using PHP.
As example, I am assigning the value of {{data.material_number}} to a variable:
<?php $material = '{{data.material_number}}'; echo "Material=".$material;?>
The echo shows the value as it should be.
But I need to detect when {{data.material_number}} is empty or has no value, and using the function:
strlen($material)
it always shows the value 24 when the string is supposed to be empty, but also when the variable is not empty and has a specific value.
How can I check if the variable $material or {{data.material_number}} is empty?
As mentioned by David, you can't check a template string with php. It doesn't work that way.
You could use the template engine to check if it's blank by using a conditional statement on the page.
Using Twig for example:
{% if data.material_number is empty %}
<td>0</td>
{% else %}
<td>{{data.material_number}}<td>
{% endif %}
"empty" is kind of vague.
You could use empty($variable) which will return true if $variable is null, an empty string, an empty array, the number zero, or if the variable doesn't exist.
You could use isset($variable) which will return true if the variable has been set no matter what the value is or false if it doesn't exist.
If you want to check if it's simply an empty string just do if($variable === ""){ ... }
While that answers your actual question, strlen will also work just fine for your purposes. If it's returning 24 every time it's because your variable is 24 chars every time. period. it's not a bug in PHP, your variable just isn't what you think it is. There must be an issue with your code, but you haven't posted enough of it, so unless you do no one is going to be able to give you a solution.
You are looking for the function empty()
For more details, read the manual

passing jquery array to php - unexpected format and count() value and variable passing issue

I am passing a jquery array called 'selected' full of ids along with the opening of an ajax modal.
$('#dtDelete').on('click', function () {
$('#modal-ajax').load('/modals/m__delete.php?selected='+selected);
$('#modal-ajax').modal('show');
});
On the modal php page count($_GET['selected']); always returns 1 no matter what. I am trying to get an actual count of the number of values in the array. Turns out this is because the array is a string as noted below.
var_dump($_GET['selected']); returns something along the lines of string(69) "187419,187420,187413,187414,187415,187416,187417,187418,187421,187422" which is something I am not accustomed to (sort of new to jquery). I need to do processing in php using foreach on this array. Can I 'convert' this to a 'normal' php array so count() will work as expected and I can process it normally in php?
Lastly, this array may or may not be extremely large at times. The jquery function above opens an ajax modal (I am using the modal as a confirmation box for the user whether they really want to delete the entries in the selected array) and I know the $_GET method has limits to the amount of data it can pass. I can't do $_POST because this is a modal and I need to load it then show it... how else can I pass this data?
$_GET['selected']
returns the STRING after the attribute 'selected', and count(string) is 1 not matter what ( it's not a multi-dimension array to be greater than 1).
As for the comma separated string example you gave, you may use the following :
$selected = $_GET['selected'];
//test wether the string has contents
if(strlen($selected)!=0) {
$selected_array = explode(',',$selected); //this is the new array that you want
}
else {
//the string is empty
}
There are many string functions you may check at : http://www.php.net/manual/en/ref.strings.php

PHP: How to check if query string or POST vars contain same var twice

It may sound strange, but in my PHP application I need to check if the same variable name has been declared more than once in the query string or POST variables, and return an error value if this is the case. If my application doesn't return an error in this case, it fails a compliance check.
When accessing vars using $_GET, $_POST, etc, PHP only returns the last value given for each variable name. I can't find a way to tell if any variable appeared more than once.
I simply need to find out if the query string or the variables in the POST body contained the same variable name more than once, whatever the values.
Example
My application is supposed to return an error for this query string:
verb=ListIdentifiers&metadataPrefix=oai_dc&metadataPrefix=oai_dc
Note that "metadataPrefix" is defined twice.
My application should not return an error for this query string:
verb=ListIdentifiers&metadataPrefix=oai_dc
POST Requests
$input = file_get_contents('php://input');
(Or $HTTP_RAW_POST_DATA (docs))
GET Requests
$input = $_SERVER['QUERY_STRING'];
Processing
explode('&', $input) and maintain an array - $foundKeys - of keys (the part of each item from explode() before the = character). If you hit a key already defined in $foundKeys, throw the error.
For GET data, check out $_SERVER['QUERY_STRING']. But for POST data, you'll need to read the raw POST data from the php://input stream.
So something like this:
// GET data:
$raw = $_SERVER['QUERY_STRING'];
// Or for POST data:
$raw = file_get_contents("php://input");
if (substr_count('&'.$raw, '&metadataPrefix=') > 1)
die('Error');
print_r($raw); //post vars
PHP $_POST will always set only one value per variable unless the request variable name ends with [].
If you have no control over the variables that are sent, you may try using $_SERVER['RAW_HTTP_POST_DATA'] to get the original POST request data before parsed, then you can use the parse_str() function to parse that string.
Just be careful that PHP configuration may have disabled setting the RAW_HTTP_POST_DATA value. In that case, you cannot do anything to solve your problem.
Not completely foolproof but this might work
$occurrences = substr_count($_SERVER['QUERY_STRING'], 'metadataPrefix=');
If you expect multiple values named the variable with square brackets in the end. This way you get an array for that variable. If multiple values are set, the array will have multiple entries.
<input type="checkbox" name="my_var[]" value="a">
<input type="checkbox" name="my_var[]" value="b">
$_POST['my_var'] will be an array with either 'a' or 'b', both, or none depending on the checkboxes used.

Categories