How to get values of dynamically created input text fields? - php

Sorry for grammar mistakes. I have multiple dynamically created text fields (more than 100). how can i get the values of these fields. i don't know the number of fields. Please help me.
Example
<input type="text" name="id_1" />
<input type="text" name="id_2" />
<input type="text" name="id_3" />
................
<input type="text" name="id_100" />

Use arrays:
<input type="text" name="id[1]" />
<input type="text" name="id[2]" />
<input type="text" name="id[3]" />
Then:
foreach($_POST['id'] as $key => $value) {
echo "text $key = $value";
}

If you mean you want to get their values in PHP easily, just name them like "name[]".
<form method="post" action="yourscript.php">
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="submit" value="Submit" />
</form>
This way you'll be able to get the values in yourscript.php. They'll be in $_POST['input']. Just loop over it using foreach to get the values:
foreach($_POST['input'] as $value) {
// do what you want with the $value
}
Having index numbers in the names like this:
<input type="text" name="input[1]" />
<input type="text" name="input[2]" />
is redundant.

In HTML forms, using jQuery:
$(function() {
$("input[name^='id']").each(function() {
var name = $(this).attr('name');
var inputValue = this.value;
console.log(name + " : "+ this.value);
});
});

Hoa about adding a hidden field to pass the number of fields to the processing script?
<input type='hidden' name='num_text_fields' valus='100>
The do a for loop to get the values
$num_text_fields = $_POST['num_text_fields'];
for ($i=1;$i<=$num_text_fields;$i++)
{
$text_field_name = "id_" . $i;
$value = $_POST[$text_field_name];
// do something with $valus
}

I think the best way to do it is to add a class element to your input.
Then you can be easily able to find them regardless of how many you have

Related

Multiple search inputs to output as one search in wordpress

I'm trying to create a search box that would have 4 inputs but output as one search. Here is my current code:
<form method="get" id="searchform" action="http://sitename.com/">
<input type="text" name="s" />
<input type="text" name="a" />
<input type="text" name="b" />
<input type="text" name="c" />
<div class="clear_space"></div>
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />
</form>
But that outputs like this in wordpress: /?s=milk&a=eggs&b=cheese&c=garlic&submit=Search
But I need it to output like this: /?s=milk+egg+cheese+garlic&submit=Search
So somehow those additional inputs need to add their text to the first ?s= with just a +... Any help is massively appreciated.
Are you kidding??
The + that you are seeking is simply a space. Have one text field, and when the form is submitted the spaces in between the words will be converted into +.
Wow, you have a lot of problems, but before we get deep into them let me tell you that your "output" is in fact a get parameter, or a URL. Now you should NEVER use the same id for different tags (!!!). If you think about a valid reason to do that, then think again.
Change your form as follows:
<form method="get" id="searchform" action="http://sitename.com/">
<input type="text" class="field" placeholder="Search" />
<input type="text" class="field" placeholder="Search" />
<input type="text" class="field" placeholder="Search" />
<input type="text" class="field" placeholder="Search" />
<input type="hidden" name="s" id="s" />
<div class="clear_space"></div>
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />
</form>
and then implement a form submit event to calculate your value and assign to your input field. Cheers.
EDIT:
Use the following js code to run before you submit your form:
//...
var value = "";
$(".field").each(function(){
value += ((value !== "") ? (" ") : ("")) + $(this).val();
});
$("#s").val(value);
//...

Processing array in php

I got this 2 array in a form to be process. However, i only manage to get the output from only one of the array. Sample as below :
<inputs id="location" type="text" name="data[]" value=""/>
<input id="shipval" type="text" name="data[][id]" value=""/>
And in the PHP part is below :
foreach ($_POST ["data"] as $id => $subs) {
foreach ($subs as $key=>$sub) {
$subcategory = $sub;
if($subs['id']=="$subcategory"){
echo $sql = " insert into x(kodLebuhraya,kodSeksyen) values ('".$subs['id']."','".$sub."')";echo "<br>";
}else{
//echo "hi2";
echo $sql = " insert into x(kodLebuhraya,kodSeksyen) values ('".$subs['id']."','".$sub."')";echo "<br>";
}
}
}
It means one location for one shipval. i have multiple input field for location and shipval. Can you guys enlight me which one is wrong. Thanks in advanced.
So basically you need to pass location and shipval in pairs.
Try this structure in HTML:
<label>Set One</label>
<input class="location" type="text" name="data[location][]" value=""/>
<input class="shipval" type="text" name="data[shipval][]" value=""/>
<label>Set Two</label>
<input class="location" type="text" name="data[location][]" value=""/>
<input class="shipval" type="text" name="data[shipval][]" value=""/>
<label>Set Three</label>
<input class="location" type="text" name="data[location][]" value=""/>
<input class="shipval" type="text" name="data[shipval][]" value=""/>
And this code for PHP:
foreach ($_POST['data']['location'] as $key => $location) {
$shipVal = $_POST['data']['shipval'][$key];
//now you have a pair of $location and $shipVal
echo $location.' : '.$shipVal.'<hr>';
}
Avoid using named indexes after unnamed ones ex. <input name="array[][named]" /> you can lose order of fields if one of pair fields is empty.
You have <inputs id="location" instead of <input id="location"
Also... foreach ($subs as $key => $sub) { will throw an error for <inputs id="location" type="text" name="data[]" value=""/> because it is not multidimensional. So try changing that to <inputs id="location" type="text" name="data[][]" value=""/>
For multiple select, the name has to end in square brackets, so you'll need to change the name of your shipval inputs
Firstly you have writtten inputs instead of input.
Secondly, this line:
foreach ($subs as $key=>$sub) {
will treat each variable as an array, but the location isn't an array.
I did not see the need for a loop since you want to just access $_POST['data'][0] and $_POST['data'][1]['id']
I also noticed that your SQL is a duplicate so you can try You can try
$sql = "INSERT INTO x(`kodLebuhraya`,`kodSeksyen`) VALUES ('%s','%s')" ;
printf($sql,mysqli_real_escape_string($_POST['data'][0]),mysqli_real_escape_string($_POST['data'][1]['id']));
Output
INSERT INTO x(`kodLebuhraya`,`kodSeksyen`) VALUES ('A','B')
Form Used
<form method="POST">
A : <input id="location" type="text" name="data[]" value="" />
B : <input id="shipval" type="text" name="data[][id]" value="" />
<input id="shipval" type="submit" name="submit" value="Submit" />
</form>

Automatic check if input filled

There are 10 boxes in my website that I fill up based on my needs. From php code I prefer not to check them one by one like this. Instead of that I thought it would be a good idea to put check marks in each boxes and if I fill something in input field it should be checked so I can check however many checkboxes are filled and know how many boxes are filled.
if ($input1) {$total = "1";
if ($input2) {$total = "2";
}
}
Anybody knows how can I put automatically check into a checkbox when I start typing anything in it ? But it should be unchecked back if I delete what I wrote before sending it. Or if you guys have a better idea that would be nice also.
Thank you !
you could do this in JavaScript. Basically if the number of input text is the same of the checkboxes then you could use this function for example:
function change() {
var input_lengths = document.getElementsByName("textArray[]");
for(var i= 0; i < input_lengths.length;i++) {
if(document.getElementsByName("textArray[]").item(i).value != "") {
document.getElementsByName("checkArray[]").item(i).checked = true;
}
}
}
and your html could be:
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="AAA" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="button" value="test" onclick="javascript:change()" />
why don't you name your checkboxes as an array e.g.:
<input type="checkbox" name="boxes[1]" />
<input type="checkbox" name="boxes[2]" />
then you can loop through the array in php and check each one individually
This is how i understood your problem:
you have 10 text inputs
you want to know how many of them contain actual input when they are submitted
your idea was to assign a checkbox input to each text input so that the corresponding checkboxes are checked when an input has text and is unchecked when an input has no text
Something very quick and dirty. You might want to name you inputs with a square bracket, so that php interprets that parameter as an array. Then you can iterate over an array and if the values are set you can count.
<html>
<body>
<?php
if(isset($_POST['input'])) {
$count = 0;
foreach($_POST['input'] as $value) {
if($value) {
$count++;
}
}
echo $count;
}
?>
<form action="testinputs.php" method="post">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input type="submit"/>
</form>
</body>
</html>
If you want to do that on the client side, than I'd take something like jQuery and look how many inputs on my page contain any text inside them.

Add more (sets of) fields to form by jQuery

It is easy to add more fields to a html form by jQuery. Then, we can serialize the fields, if the have the same name but what if we have a set of fields? For example
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
Now I want to add a new set of fields by jQuery as
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
and so forth. I process the form with PHP to insert movies into mysql database with three columns of (name, director, year). In the above-mentioned example, it is hard to serialize the fields to create appropriate $_POST arrays. How should I serialize jquery-added sets of movies?
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
Nothing else. On the server you will get (in case of POST) array in $_POST['movie_name'], $_POST['movie_director'] and $_POST['movie_year'];. Elements with the same index are from the same set of inputs.
What kind of problem with serialization do you have?
<form>
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<hr />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<br />
<input type='button' id='serialize' value='Click me' />
</form>
and js code:
$('#serialize').click(function(){
alert($('form').serialize());
});
when you want to submit the data just write
$.post('script.php', $('form').serialize(), function() {alert('Saved');});
ps: if you are afraid to lose something, just compare count($_POST['movie_name']), count($_POST['movie_director']) and count($_POST['movie_year']).
or you can add indexes
<input type="text" name="movie_name[0]" />
<input type="text" name="movie_director[0]" />
<input type="text" name="movie_year[0]" />
<input type="text" name="movie_name[1]" />
<input type="text" name="movie_director[1]" />
<input type="text" name="movie_year[1]" />
Based on useful discussion with Cheery, I came to conclusion that the best and safest way is to use
<input type="text" name="movie_name[i]" />
<input type="text" name="movie_director[i]" />
<input type="text" name="movie_year[i]" />
where we define each i with jQuery to serialize the fields SAFELY. This way, we can be sure that serialized arrays are parallel and well matched, without misplacing.
You can do something like this:
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
// OTHER:
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
And do this to all... In Jquery, you create a function that create field as needed... I'm not the best in JQuery so I can't help you for this but the way I told you worked fine for me with PHP...

HTML form with associated inputs

I've got a system where I allow a user to select multiple checkboxes from n amount of checkboxes, but also want two more inputs associated with each checkbox. This is for a message, a date and a time. When I post the data to be processed by a PHP script, I'd like to be able to access each of the sets of checkbox and two other inputs so I can see what date and time a user has filled in for each of the messages they've selected. I'm having trouble coming up with a method to associate the two other inputs with each checkbox.
Any ideas how to do this?
You can use arrays in your html inputs like so...
<input type="text" name="messages[1][message]" value="herp" />
<input type="text" name="messages[1][date]" value="24th April" />
<input type="text" name="messages[1][time]" value="13:00" />
<input type="text" name="messages[2][message]" value="derp" />
<input type="text" name="messages[2][date]" value="26th April" />
<input type="text" name="messages[2][time]" value="18:00" />
<?php
$messages = $_REQUEST['messages'];
foreach ($messages as $messageId => $value){
echo $value['message'];
echo $value['date'];
echo $value['time'];
}
Your HTML:
<input type="checkbox" name="car" />
<input type="text" name="msg_car" value="Car message" />
<input type="text" name="date_car" value="Car date" />
<input type="checkbox" name="bike" />
<input type="text" name="msg_bike" value="Bike message" />
<input type="text" name="date_bike" value="Bike date" />
<input type="checkbox" name="train" />
<input type="text" name="msg_train" value="Train message" />
<input type="text" name="date_train" value="Train date" />
<input type="checkbox" name="plane" />
<input type="text" name="msg_plane" value="Plane message" />
<input type="text" name="date_plane" value="Plane date" />
Your PHP script:
$array = array("car", "bike", "train", "plane");
for ($i = 0; $i < count($array); $i++) {
if (isset($_POST[$array[$i]])) {
//Checkbox was checked, get values
$msg = "";
$date = "";
$msg_id = "msg_" . $array[$i];
$date_id = "date_" . $array[$i];
if (isset($_POST[$msg_id]))
$msg = $_POST[$msg_id];
if (isset($_POST[$date_id]))
$date = $_POST[$date_id];
}
}
I think something like this should work. I didn't test it.. So forgive me if this example still contains some minor errors.

Categories