Currently I'm working on a large form with dynamic number of input fields, it can get extremely large (above 3k variables or more) and it causes problems with 'max_input_vars' option.
I'm looking for a workaround for this issue (I'm not interested in increasing the value of 'max_input_vars' option), so currently I'm trying to join all form fields values into single field (using jQuery serialize() method) and then recreate the variables on the server side.
This is what I have so far:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Workaround for max_input_vars</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"]==="POST" && !empty($_POST["data"])) {
$vars = explode("&", $_POST["data"]);
$data = array();
foreach($vars as $var) {
parse_str($var, $variable);
assign_var($_POST, $variable);
}
echo "<pre>";
var_dump($_POST);
echo "</pre>";
}
function assign_var(&$target, $var) {
$key = key($var);
if(is_array($var[$key]))
assign_var($target[$key], $var[$key]);
else {
if($key==0)
$target[] = $var[$key];
else
$target[$key] = $var[$key];
}
}
?>
<form id="myForm" method="POST">
<input type="text" name="var_1" value="<?php echo htmlentities("double quote: \"hello\"");?>"/><br/>
<input type="text" name="var_2" value="<?php echo htmlentities("single quote: 'hello'");?>"/><br/>
<input type="text" name="var_3" value="<?php echo htmlentities("text with & ampersant");?>"/><br/>
<input type="text" name="var_4" value="<?php echo htmlentities("animals");?>"/><br/>
<input type="text" name="matrix[]" value="1"/><br/>
<input type="text" name="matrix[]" value="2"/><br/>
<input type="text" name="matrix[]" value="3"/><br/>
<input type="text" name="matrix[]" value="4"/><br/>
<input type="text" name="matrix[]" value="5"/><br/>
<input type="text" name="matrix[]" value="6"/><br/>
<input type="text" name="matrix[]" value="7"/><br/>
<input type="text" name="var_5" value="abc"/><br/>
<input type="text" name="var_6" value="bcd"/><br/>
<input type="text" name="var_7" value="cde"/><br/>
<input type="text" name="var_8" value="def"/><br/>
<input type="text" name="multi_matrix[colors][]" value="red"/><br/>
<input type="text" name="multi_matrix[colors][]" value="blue"/><br/>
<input type="text" name="multi_matrix[colors][]" value="green"/><br/>
<input type="text" name="multi_matrix[weight][]" value="75"/><br/>
<input type="text" name="multi_matrix[weight][]" value="83"/><br/>
<input type="text" name="multi_matrix[weight][]" value="62"/><br/>
<input type="text" name="multi_matrix[height][]" value="170"/><br/>
<input type="submit" value="Send">
</form>
<style>
input {
margin: 5px 0;
}
</style>
<script type="text/javascript">
(function(){
$(function(){
$("#myForm").submit(function(event) {
var $this = $(this);
var data = $this.serialize();
$this.find("input, textarea, select, button").remove();
$this.append("<input type='hidden' class='data' name='data'/>");
$this.find("input.data").val(data);
});
});
})(jQuery);
</script>
</body>
</html>
As you can see, I'm intercepting the submit event and replace all form fields with single 'data' field. On PHP side I'm using explode() and pars_str() methods to recreate the variable, lastly I want to put the variable into $_POST array, so I created simple recursive function that can handle simple values and multidimensional associative arrays.
The result is a $_POST array filled with form values, as if the form was submitted normally.
What do you think about above method ? Maybe there's a much simpler solution to the problem that I hadn't noticed earlier ? If not, are there any options to improve the above code ? Are there any negative side effects of the above solution ?
Thanks in advance !
Related
Is it possible?
My question is that :
I have a form having fields like this:
<input type="hidden" id="chargetotal_reload_uk" name="chargetotal" value="12.01" />
<input type="text" id="name" name="name" value="xyz" />
and another form:
<input type="hidden" id="chargetotal_reload_uk" name="chargetotal" value="12.01" />
<input type="text" id="name" name="name" value="xyz" />
In both cases:
print_r($_POST);
give me:
array(
'chargetotal'=>'12.01',
'name'=>'xyz',
);
Is it possible to identify hidden field in $_POST ie chargetotal was the hidden field in the form?
As stated above, you should probably already know the names of your hidden fields, but technically, you could pass the field type in with the name, then explode on the $_POST keys in your script.
HTML
<input type="hidden" id="chargetotal_reload_uk" name="charge total::hidden" value="12.01" />
<input type="text" id="name" name="name" value="xyz" />
PHP
<?php
$vars = array();
foreach($_POST as $key => $val){
if(sibstr_count($key, '::') > 0){
$key = explode('::'. $key);
$vars[$key[0]] = array('fieldType' => $key[1]. 'value' => $val);
} else {
$vars[$key] = $val;
// or $vars[$key] = array('fieldType' => ''. 'value' => $val); if you need to keep the same format
}
}
As per your comment on usage, you can use PHP sessions for this.
You would need to set your value in $_SESSION['value'] = "12.01"; for it to work.
<?php
session_start();
$_SESSION['value'] = "12.01";
?>
<?php
if (isset($_SESSION['value']) || !empty($_SESSION['value'])) {
echo "The value is: " . $_SESSION['value'];
}
else {
echo "<div id='session_name'>No value set.</div>";
}
?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var thevalue = "<?php echo $_SESSION['value']; ?>";
// alert (thevalue);
});
</script>
<br><br>
<input type="hidden" id="chargetotal_reload_uk" name="chargetotal" value="<?php echo $_SESSION['value']; ?>" />
<div id="session_name">The value is: <?php echo $_SESSION['value']; ?></div>
HTML source/output:
The value is: 12.01
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var thevalue = "12.01";
// alert (thevalue);
});
</script>
<br><br>
<input type="hidden" id="chargetotal_reload_uk" name="chargetotal" value="12.01" />
<div id="session_name">The value is: 12.01</div>
I am trying to get user input of 10 numbers in an html file that will be stored into an array on a php file. Although when I try to print the array out all I am getting is the word "Array", any idea as to why?
HTML File:
<!DOCTYPE HTML>
<html>
<head>
<meta charset= "utf-8">
</head>
<body>
Please enter 10 numbers that will be stored in an array:
<form action="practice3.php" method="post">
<input type="text" name="number1[]" placeholder="First number"><br />
<input type="text" name="number2[]" placeholder="Second number"><br />
<input type="text" name="number3[]" placeholder="Third number"><br />
<input type="text" name="number4[]" placeholder="Fourth number"><br />
<input type="text" name="number5[]" placeholder="Fifth number"><br />
<input type="text" name="number6[]" placeholder="Sixth number"><br />
<input type="text" name="number7[]" placeholder="Seventh number"><br />
<input type="text" name="number8[]" placeholder="Eighth number"><br />
<input type="text" name="number9[]" placeholder="Ninth number"><br />
<input type="text" name="number10[]" placeholder="Tenth number"><br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
PHP File:
<?php
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$number3 = $_POST['number3'];
$number4 = $_POST['number4'];
$number5 = $_POST['number5'];
$number6 = $_POST['number6'];
$number7 = $_POST['number7'];
$number8 = $_POST['number8'];
$number9 = $_POST['number9'];
$number10 = $_POST['number10'];
$myArray = Array($number1, $number2, $number3, $number4, $number5, $number6, $number7, $number8, $number9, $number10);
echo $myArray;
?>
Just use print_r :
echo '<pre>';
print_r ($myArray);
echo '</pre>';
It will display information about your variable in a way that's readable by humans. can be used with arrays and objects.
Just to elaborate, echo expects a string and doesn't work for Arrays. In general when debugging people tend to use var_dump () which works on Objects, Arrays, Strings, etc.
You don't need the [] in your input names, number1, number2, ... will suffice. Then just use print_r to print the array.
print_r will work but if you want control over formating you will need to iterate over the array
foreach ($myArray as $value) {
echo "Value: $value<br />\n";
}
I am learning PHP now, so pardon my silly question which I am not able to resolve.
I have created a simple web form where in I display the values entered by a user.
function submitform()
{
document.forms["myForm"].submit();
} // in head of html
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" onclick="submitForm()" />
</form>
and res.php contains:
foreach($_POST as $field => $value)
{
echo "$field = $value";
}
When I click on the submit button, I just get a blank page without any values from the form. Can anyone please let me know what am I missing?
There's no need for the javascript. This should do:
<form action ="res.php" id="myForm" method="post" >
Name: <input type="text" name="name" size="25" maxlength="50" /> <br> </br>
Password:<input type="password" name="password" size="25" maxlength="50" />
Description: <textarea name="editor1"> </textarea>
<input type="submit" value="Submit" />
</form>
Let's start with fixing errors:
JavaScript is case-sensitive. I see that your function name is submitform and the form's onclick calls submitForm.
The javascript is not really necessary from what you've shown us, I would try this on a single php page and see if it works:
Create a test.php file for test purpose:
<?php
if($_POST){
foreach($_POST as $key=>$value){
echo "$key: $value<br />";
}
}else{
<form action="test.php" method="post">
<input type="text" value="1" name="name1" />
<input type="text" value="2" name="name2" />
<input type="submit" value="submit" name="Submit" />
</form>
}
?>
If it does work, slowly work your way into your current form setup to see what is breaking it. If it doesn't work, there's something larger at play.
There are 2 things you should do now.
Remove the JavaScript function to submit the form. It's not required (or necessary). The default behavior of a submit button is to... well... submit. You don't need to help it with JavaScript.
Enable error display by using error_reporting(E_ALL).
After you do both things, you should be able to debug and assess the problem much more easily.
Put your php code inside php tags!
<?php
foreach($_POST as $field => $value)
{
echo $field ." = ." $value.'<br />';
}
?>
If you do
<?php
print_r($_POST);
?>
what do you get?
If this still doesn't work, does your server parse php?
Create the file test.php and access it directly http://localhost/test.php or whatever your URL is
<?php
echo 'Hello';
?>
if this doesn't work..it's a whole diferent problem
You can submit an HTML form using PHP with fsubmit library.
Example:
require_once 'fsubmit.php';
$html = "<form action ='res.php' method='post'><input type='text' name='name'/></form>";
$form = new Fsubmit();
$form->url = 'http://submit_url_here.com';
$form->html = $html;
$form->params = ['name'=>'kokainom'];
$response = $form->submit();
echo $response['content'];
I have two files, (file1 and file2). file1 includes file2 in a PHP include statement. File1 also contains a form and prints out all $_POST variables. File2 uses a Javascript button to dynamically change the value in an input field. The problem is that $_POST is empty after submit is pressed. Why is that and how do I fix it?
File1:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php include 'file2.php'; ?>
<input type="submit" /></form>
<?php foreach ($_POST as $key => $val) { echo $key . " belongs to " . $val; } ?>
File2
<script type="text/javascript">
var button = {
counter : 0,
count : function() {
text = document.getElementById("text");
this.counter++;
text.setAttribute("value", this.counter);
}
};
</script>
<button type="button" onclick="button.count()">CLICK ME!</button>
<input id="text" type="text" value="0" />
You forgot to set name attribute:
<input id="text" name="text" type="text" value="0" />
text.setAttribute("value", this.counter);
This is better off as:
text.value = this.counter;
Also you need a name attribute on your element:
<input id="text" name="text" type="text" value="0" />
you didnt set the name attribute for the field "text".
Corrected code:
<input id="text" name="text" type="text" value="0" >
I am creating a dynamic form where the user will have the ability to add a set of inputs to the form. The html looks like this:
<form>
<input id="title1" class="title" name="title1" type="text" value="">
<input id="productionCompany1" name="productionCompany1" type="text" value="">
<input id="year1" name="year1" type="text" value="">
<input id="role1" name="role1" type="text" value="">
<div id="newCredit"> </div>
add another credit
</form>
When the user clicks the link with the id of "addCredit" the following jQuery script is called:
$(document).ready(function() {
var $ac = $('#addCredit');
$ac.click(function() {
/* the following two lines are where the problem lies? */
var $credInt = $(this).prev(".title");
$.get("addCredit.php", {num: $credInt},
function(data){
$('#newCredit').append(data);});
return false;
});
});
The jQuery function queries a php file called "addCredit.php", which looks like this:
<?php
$int = $_GET["num"];
$int = substr($int, -1);
$int++;
?>
<input id="title<?php echo $int;?>" class="title" name="title<?php echo $int;?>" type="text" value="">
<input id="productionCompany<?php echo $int;?>" name="productionCompany<?php echo $int;?>" type="text" value="">
<input id="year<?php echo $int;?>" name="year<?php echo $int;?>" type="text" value="">
<input id="role<?php echo $int;?>" name="role<?php echo $int;?>" type="text" value="">
My problem is getting the javascript variable $credInt set properly so that it can be sent to the addCredit.php page and update the form fields accordingly. I also need to be sure that every time the form is appended, the next value sent is the incremented value.
Any thoughts on how I might accomplish this? Thank you for your help.
This is the wrong way of doing it; PHP can handle array syntax in a variable name. This makes it much easier to handle. It is also unnecessary to call the server to clone the form. You should name your fields like this:
<form>
<div id="originalCredit">
<input name="title[]" type="text" value="">
<input name="productionCompany[]" type="text" value="">
<input name="year[]" type="text" value="">
<input name="role[]" type="text" value="">
</div>
add another credit
</form>
And then your Javascript can be like this:
$(function() {
$('#addCredit').click(function() {
var newCredit = $('#originalCredit').clone(); // create new set
newCredit.find('input').val(''); // empty input fields
$(this).before(newCredit); // append at the end
return false;
});
});
When the form is finally sent to the server, because the variables are in the format of name[] PHP will recognize they are an array and then you can do this:
<? foreach($_POST['title'] as $k => $v) { ?>
Title: <?=$_POST['title'][$k]?><br>
Company: <?=$_POST['productionCompany'][$k]?><br>
Year: <?=$_POST['year'][$k]?><br>
Role: <?=$_POST['role'][$k]?><br>
<? } ?>
Obviously this is just displaying it as an example, but you can then save/update/whatever with it.