I have created many textboxes and I want to put all of the values into array only if they have themselves filled. I need them to work as how checkbox works in HTML (only the checked ones will then put into array). I use PHP language here. How to do that?
This is my simple HTML textbox:
<input name="array[]">
Would really appreciate for any help you give to me. Much thanks!
Firstly, I'd recommend you change your name to something more readable:
<input name="name[]">
Next, you want to get your data, I am assuming your form is using POST. We're going to store the form data into a $names array variable.
$names = $_POST['name'];
Next, we're going to create a new array variable which will store input values that have data.
$namesWithData = [];
We're now going to loop through the $names array. This loop will add any fields with data to the $namesWithData array.
foreach($names as $name) {
if(!empty($name) {
array_push($namesWithData, $name);
}
}
The $namesWithData array has the data ready to use.
Related
I have in mysql table 98 fields, and I am trying to make all inputs $_POST and control it if they are numeric etc.
Is there any easy way to type that or I need make all inputs like this one:
$age = $db->real_escape_string(trim($_POST['age']));
Thanks
You can use array_map.
$_POST = array_map(function($item){
return $db->real_escape_string(trim($item));
},$_POST)
please provide perfect scenario what you want to do?
if u try to escape all $_POST field than simplay loop $_POST and store back it's value in $_POST appropriate key than you got filter value.
eg.
foreach($_POST as &$key => $post){
$key = $db->real_escape_string(trim($post));
}
So this has been bugging me for quite a while...
I've been trying to add $_POST of an array to another array and it doesn't seem to work...
That's the input form :
<input name="camp[]" type="text" value="">
$NrCampuri = array();
$NrCampuri = $_POST['camp'];
That's the current form that I have.
What I aim to do is to assign the $NrCampuri[0] the post of the camp[0].
I tried a lot of things and nothing seems to work. I have multiple values in camp but I want to assign them one by one.
simple use array_merge function merges one or more arrays into one array
array_merge($NrCampuri ,$_POST['camp']);
As per your comment you need to use array_push for pushing specific values into new array .
array_push($NrCampuri,$_POST['camp'][0]);
array_push($NrCampuri,$_POST['camp'][2]);
try this
$output = array_combine($NrCampuri, $_POST['camp']);
I´m still new in php and I´m having trouble in sending a form vars through POST.
I use a jquery to dynamically add new fields with the same name plus a auto increment ID in a form.
In the _POST I need to have a loop to get those vars, but how am I going to get the vars count for the loop?
I have something like this:
$ID1_name = $_POST["ID1_name"];
$ID2_name = $_POST["ID2_name"];
$ID3_name = $_POST["ID3_name"];
$IDX_name = $_POST["IDX_name"];
$ID1_place = $_POST["ID1_place"];
$ID2_place = $_POST["ID2_place"];
$ID3_place = $_POST["ID3_place"];
$IDX_place = $_POST["IDX_place"];
There can be unlimited variables with same name with and AI, and I have around 10 vars like that.
How can I count by the var partial name?
or
Is there a better way to get those _POST? I´m using a For loop.
I was using the same name and counting in the array. It worked, but I have 3 fields with radio and 2 with checkbox, and in the array all the vars of the same name merge. So the array for food have entries from all food checkbox from the field. Does that make sense?
So in one $ID1_place = string I might have $ID1_check = array
Thanks for the help. I´know there is a easy way, but I think I´m searching the wrong way since I found nothing.
Thanks a lot.
regards
There's two ways I can think for this. Either you can pass through a hidden element with the number of elements with your name, or you can use the preg_grep function to match all POST vars matching a regex pattern:
// Search keys:
$ID_place_keys = preg_grep('/ID[0-9]+_place/', array_keys($_POST));
// Search values:
$ID_places = preg_grep('/ID[0-9]+_place/', $_POST);
// Get array of values matching keys:
$ID_places = array_intersect_key($_POST, array_flip(preg_grep('/ID[0-9]+_place/', array_keys($_POST))));
// Gets a bit messy with all the array functions...
And here's a URL to a simple working example:
http://ideone.com/a9AhL6
Have you considered instead of sending a lot of post variables, to send only two; where all the fields are json encoded?
Example: names: ["ID1", "ID2", "ID3"] and places: ["ID1", "ID2", "ID3"]. To get the AJAX parameters like this you can stringify the arrays you have using JSON.stringify(). Ref: JSON.stringify()
Then on the back-end just do $names = json_decode($_POST["names"]) and $places = json_decode($_POST["places"])
This way you have an array with the id-s of the names and places. You can count them, loop over them, do whatever you need to.
I've been searching for a while now to accomplish my question, so I decided to ask it here.
My problem.
I've filled 2 arrays with content from a database like this:
$query = "SELECT table_1, table_2 FROM questions";
// Execute query or trow an error
$results = mysqli_query($conn, $query) or die(mysql_error());
$resulttablet1= array();
$resulttablet2= array();
while($row = mysqli_fetch_array($results))
{
// Add the right questions to the array
$resulttablet1[] = $row['table_1'];
$resulttablet2[] = $row['table_2'];
}
So I've now got 2 arrays, each filled with the content of one table. This is all working fine. Now I want to put those two arrays into one array so it acts like one big array.
Something like this:
$newarray = array();
$newarray[$resulttablet1, $resulttablet2];
or
$newarray = array($resulttablet1,
$resulttablet2);
Then I want to echo $newarray and show all the elements of the other two arrays.
I know I can echo both arrays separately, but this is not possible for the goal I'm trying to accomplish.
Thanks in advance.
Edit
I realise my question isn't clear enough, I'll try to explain it a bit better.
On top of my question I want to show the the content of both arrays one by one on button click. That's what I'm doing at the moment like this:
// I retrieve a value from a javascript file, where I add 1 to a variable each time a button is clicked then I send this value to the server using jQuery Ajax
$value = (int)$_POST["question_nmbr"];
// I use $value to echo the right element out of the array.
echo "<li>$resulttablet1[$value]</li>";
Everytime the button is clicked ajax loads the php file and the value is increased so the next question is loaded.
I want to do the same thing but now with an array which has multiple arrays inside of it.
array_merge doesn't do the trick I think, cause print_r($result); gives me all the content of the array.
I hope my question is a little bit more clear now.
I found the answer, thanks to someone who removed his answer already.
array_merge did the trick after all, I don't know what I did wrong the first time but now I can echo it just fine.
Here is how it works:
// I retrieve a value from a javascript file, where I add 1 to a variable each time a button is clicked then I send this value to the server using jQuery Ajax
$value = (int)$_POST["question_nmbr"];
$newarray = array_merge($resulttablet1, $resulttablet2);
echo "<li>$newarray[$value]</li>";
Hope someone will find this useful, if not ask for more info :)
i'm not sure how to explain it correctly, but i would like to use $_POST values like this $_POST[0] and not like this $_POST['form_field_name'].
Let's say i have 3 form fields so if i'd like to get data from post like this:
echo $_POST[0];
echo $_POST[1];
echo $_POST[2];
I hope you guys understand what i wanna do here.
Try it like this:
$values = array_values($_POST);
No idea why you would do such a thing though.
I would not recommend ever to refer to your $_POST values with indexes, as it is generally a bad idea.
You can access them by indexes if you do this:
$items = array_values($_POST);
$foo = $items[0];
$bar = $items[1]
You can also run through your values with a foreach loop, like this (which is better, but still bad!)
foreach($_POST as $item)
{
// do your thing here
}
$_POSTs variables depends on the name attribute of the form element as you can read in this link.
On the other hand the attribute name of the form elements according to W3C always must begin with a letter.
But I think you can prepare the $_POST variable before all your code (at the begining of your php script) with:
$arrPostVariables = array_values($_POST);
And then call them in the way as you want, but I think you will previously must detect the order of the array in order to avoid errors by not having the text of each variable.
This should work:
<input name="0" value="val0" />
<input name="1" value="val1" />