Circumventing PHP's max_var_lim on POST method - javascript

I am a newbie in the PHP language and have been struggling with this particular problem.
I am trying to workaround the max_var_lim for retrieving data via POST in PHP. I am currently collecting data as follows, using a selectbox to retrieve data and putting them in an array to store in POST. Below is the code I have in my blade.php file:
<form action={{route('account.confirm')}} method="POST">
<select class="select-cats" name = "categories[]" id="optcatlist" multiple="multiple">
#foreach ($categories as $cat)
<option value="{{$cat->id}}" #if($user->company->categories->contains($cat->id)) selected="selected" #endif>
{{$cat->name}}
</option>
#endforeach
</select>
</form>
The problem is, there are a ton of options (>1000) so in the case where all are selected, I am only able to retrieve the first 1000. I am unable to change the php.ini file, so that solution is a no-go.
My question is, how do I access $_POST["categories"] on this page and implode this into a single string so I can pass it on using POST without exceeding the max_var_limit?
A detailed answer would be much appreciated as I just started using PHP and don't know left to right.
Thanks!

Related

Can anyone suggest some ideas for what I want to do here

I am quite well versed in developing in PHP, HTML5 and CSS3 however when it comes to Javascript and Jquery I'm totally lost.
What I'm looking at doing is on my site I want to place a custom page that allows the users to select certain options which will be specified by me.
pretty much along the lines of:-
Question1?
Select one of 2 options.
option 1 moves along to question 2
however where option 2 then moves to a different question.
what I would like to do instead is to use Javascript instead so it could be question 1 select answer, then question 2 automatically populates underneath. and then depending on that answer and so on, until the end is reached.
I will include a little snippet below so it helps visualise the end goal that I am looking for:-
<?php
$q1 = $_POST['q1'] ?? '';
$q2 = $_POST['q2'] ?? '';
?>
<?php
if(isset($_POST['q1] {
echo 'q2';
} else {
echo 'q20';
}
?>
The HTML would then be:-
<form name="script_path.ext" method="POST">
<span>Question 1?</span></br>
<select id="q1" name="q1">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<input type="submit" name="submit"
value="Next">
obviously it would be better to use Jquery to do this as the amount of variables are going to grow as questions get added and could become an infinite loop of if_statements.

Dynamically update Javascript variables using Jinja?

I have a web form created that requires template creation by the user. Calling all previous entries that are templates isn't an issue, but passing the information to the form when called seems to be tricky. Basically I want the user to be able to select a template from the dropdown (See the screenshots and stuff below), then based on their selection, update the variables in the script to autofill form data. Right now, it only selects the most recent entry. Is this possible just using python/flask/javascript or am I missing something? Any help would be appreciated! Thanks!
Template Dropdown
<label for="template_select">Select Template</label>
<select class="form-control" name="template_select" id='template_select' onchange='changeTemplate()'>
<option value=''></option>
{% for template_info in template_search %}
<option value='{{template_info.client_name}}'>{{template_info.client_name}}</option>
{% endfor %}
</select>
Javascript to change values
{% for template_info in template_search %}
<script>
function changeTemplate(){
var template = document.getElementById('template_select').value;
document.getElementById('client_name').value='{{template_info.client_name}}';
document.getElementById('client_name').innerHTML='{{template_info.client_name}}';
}
</script>
{% endfor %}
Python Passing in the Query
template_search = newmatter_model.NewClientMatter.query.filter_by(
creator=verifier, is_template='on').all()
Your mistake is to create Javascript code in a loop. You don't need to do this.
What you want to do is think of the data sent to the browser as independent. Make it work first without Flask and Jinja2. Create a static page that works and does what you want.
The following code would work with static data:
function changeTemplate(){
var template = document.getElementById('template_select').value;
document.getElementById('client_name').innerHTML = template;
}
<label for="template_select">Select Template</label>
<select class="form-control" name="template_select" id="template_select" onchange="changeTemplate()">
<option value=""></option>
<option value="Client 1">Client 1</option>
<option value="Client 2">Client 2</option>
<option value="Client 3">Client 3</option>
</select>
<div id="client_name"><i>No client set</i></div>
That's HTML for a select box, a separate <div> element, and Javascript code to copy the selected option value into the <div>. Note that the Javascript code doesn't know anything about what data is involved; no client names are stored in the code. All that the small function does is to copy the value from the currently selected option, to somewhere else.
Once that works on its own you can start thinking about how you are going to insert the values from your application. In the above code, all that needs replacing is the dropdown options, because the Javascript code can then access everything it needs from the <select> element value.
So the Javascript code doesn't need to be generated at all, you only generate the <option> elements, as you already did in your question.
You rarely need to generate dynamic Javascript code, and it would be much better for your app if you don't. Static Javascript code can be served by a CDN and cached in the browser, removing the need for your app to keep serving that again and again for all clients. Try to minimise that whenever you can.
Instead, generate just the data that the code needs to operate on.
You can put that information in HTML tags. In the above example, your data is put in the repeated <option> tags. Or
you could add data attributes to your HTML, which are accessible both to Javascript code and to CSS. Or
use AJAX to load data asynchronously; e.g. when you pick an option in the <select> box, use Javascript and AJAX to call a separate endpoint on your Flask server that serves more data as JSON or ready-made HTML, then have the Javascript code update your webpage based on that. Or
generate JSON data and put it directly into your HTML page. Just a <script>some_variable_name = {{datastructure|tojson|safe}};<script> section is enough; then access that some_variable_name from your static Javascript code to do interesting things on the page. JSON is a (almost entirely a) subset of Javascript, and the way the tojson filter works is guaranteed to produce a Javascript-compatible data structure for you. The browser will load it just like any other embedded Javascript code.

Getting the selected string in the dropdown list in Angular

To begin with, I am an absolute beginner in front-end development, thus please excuse me if the question is too elementary.
The project I am working on, a drop-down list has been defined as:
<div ng-controller="FacetController">
<div class="spnlocationdrop-container" ng-hide="isservicelocal">
<select class="spnlocationdrop" ng-model="$parent.locationsearch" ng-options="location for location in locations | sortArray ">
<option value="">All Cities</option>
</select>
</div>
</div>
The list contains cities, out-of which the user has to select one. The value has to be stored, and sent to the backend via an AJAX call.
How do I get the selected value? Until now, I've been doing that using the document.getElementByID().value() function, but since the above list contains no ID, how do I get the value?
ng-model will have the value of the option selected.
Here's a simple working example: Plunker
In my example, data.singleSelect has the value you need so I'm able to output that to the view using {{ data.singleSelect }} though if I wanted to access it in my controller I would do var input = $scope.data.singleSelect and then pass that input variable to the backend via an AJAX call.

select2 set preloaded tags from html option

I have the following html, the <option> tags are generated by me (by php, more precisely symfony3)
<select
name="simple_event[tagsList][]"
id="js-tags-list"
class="form-control"
multiple
data-tags="true"
data-placeholder="add-category-separated-by-semicolon"
>
<option selected>html</option>
<option selected>select2</option>
<option selected>jquery</option>
</select>
I use select2 with the followig snippets
$("#js-tags-list").select2({tokenSeparators: [";"]});
I thought it would have put the 3 tags as preloaded data, but it actually only put them as preloaded "suggestions" (which I thought would have been the case if I had not put the selected attribute)
Is there a way to do this (i.e preload data) without needing to generate my javascript with my php (which seems really dirty to me) ?
If you load the values from a database, Symfony already provides something that preloads data. http://symfony.com/doc/current/reference/forms/types/entity.html

using a variable from drop down list

I'm attempting to create a dynamic drop down list, but after searching for hours I'm still completely stumped as to how to do it. So I am experimenting with variables in a list. This is the code I've started with.
<body>
<form method = "post">
<select id = "State" onchange = "a = this.options[selectedIndex].value;">
<option value = ""> Select a state</option>
<option value = "S3"> Pennsylvania</option>
<option value = "S4"> California</option>
<option value = "I4"> Texas</option>
<option value = "I5"> New York</option>
</select>
</form>
</body>
my question is what can I do with the variable 'a' so that I could create a dynamic drop down list based off it? One thing I had hoped to do was use an if-statement to display a second list. I wanted to remain on the same page too.
Note: I apologize if this seems like a basic question, but I've been scouring websites for answers and all I've figured out was that I can't send javascript variables to php, otherwise I'd have gotten this done forever ago
try this way
<select id="appamt" name="appamt">
<? while($amt=mysql_fetch_array($amount)){ ?>
<option value="<?=$amt['amount']?>"><?=$amt['amount']?></option>
<? } ?></select>
When your page detects the onchange event, what you need to do is either have all the combinations of options you want readily available but hidden on the page e.g. $('#sub-option1).hide(); and when the value is selected use $('#sub-option1).show();
Secondly you can have a ajax request that returns a JSON value of key=>value pairs, then use these key value pairs to dynamically build another dropdown using jquery .each() function.
Loop through your json values and add them to a previously hidden selector and then display it.
Hope this helps but if not email me and I will skype you through it.
Jim C

Categories

Resources