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.
Related
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']);
In my database I have a table named posts. In it, the columns are:
Postid - 1
PostBody - my first post
Likers - 1,2,3,4,5,
Now what am trying to do is, in the likers field, am trying to pull the individual number without pulling all the data on the field. So if I want to retrieve likers No.3, I want the output to be 3 when I echo it out.
I have tried FIND_IN_SET(), and strstr(). All I get is the whole array, 1-5, being echoed. And because of that, I cannot match it with other variables(I.e. $var = 3;) which have the matching number.
Is there a way of doing this?
Thanx
If you really want to do it this way (read my comment first), what you can do is retrieve the comma-seperated data, explode it into a php array, and then use the array_search built-in function:
$likers_attribute_data = '1,2,3,4,5,';
$likers_array = explode(',', $likers_attribute_data);
// check if user 3 is a liker:
if (array_search('3', $likers_array) {
// he has :)
} else {
// he hasn't :(
}
Again, this is a very sloppy and inefficient way of doing business. Please just go with the solution in my comment!
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.
I'm using a wordpress plugin which redirects to a random post. It allows me to redirect to random posts based on tags, so the url might look like
example.com/?random&random_tag_id=100
If I wanted to find random posts which are tagged with tag ids 100 and 101, I would just do
example.com/?random&random_tag_id=100&random_tag_id=101
But I want to find random posts which are from EITHER ids 100 or 101. I know & is used for 'this + this', but would it be possible to make a 'this OR this' request via the URL?
Thanks for any help!
You can do something like this:
example.com/?random&random_tag_id=100,101,102,103
You'll then have to do something like this when you process those variables:
<?php
$random_tag_ids = explode(',', $_GET['random_tag_id']);
// $random_tag_ids now contains an array of your random tag ids
?>
You can pass arrays to query like this:
example.com/?random&random_tag_id[]=100&random_tag_id[]=101
You'll get $_GET['random_tag_id'] == array(100, 101)
The problem with doing it that way is you'll overwrite the value in PHP. You need bracket notation to pass it correctly as an array
example.com/?random&random_tag_id[]=100&random_tag_id[]=101
When you get to your PHP then you'll have
echo $_GET['random_tag_id'][0]; //Outputs 100
You could then pass into your query an imploded list
You're misunderstanding how GET parameters work.
In a Query String (?key=a&otherkey=b), the & is just a delimiter between the different key/var pairs. So the PHP equivalent of that string is this:
$_GET['key'] = 'a';
$_GET['otherkey'] = 'b';
If you use the same key twice in a query string, the latter will overwrite the former. So for the string ?key=a&key=b, $_GET['key'] will be equal to b.
As others have said, you can pass data via a comma separated list, (i.e. ?key=1,2,3,4), or via an array, (i.e. ?key[]=a&key[]=b).
Basically, I have working solution for this, but I'm wondering if it could (should?) be done better in some other way.
I have table I'm creating in PHP with values from MYSQL. Each item in table has multiple values. In each line there is single link and clicking on this link fires up jQuery function. In each link there is also VALUE attribute with values from multiple MYSQL fields and joined with &&:
PHP code is:
foreach ($this->_data as $l)
{
?>
...
<td>Link</td>
...
<?php
}
And jQuery function to fire up when clickin' on link is:
$(".clickMe").click(function() {
myData = $(this).attr('value').split('&&');
});
Script splits string in VALUE attribute on && and creates an array myData with values:
myData[0] => value passed from $l->_data1 in PHP
myData[1] => value passed from $l->_data2 in PHP
Is this the right way to do it?
It's fine, as long as you'll never have && in your data. You could use json_encode() in PHP and then decode this into an array in JavaScript. That would be a more standard solution.
I would recommend against using && which looks like a boolean AND. Instead I would probably use something like a pipe to separate them val1|val2.
I think you're better off passing the whole joined string in to PHP and splitting it out there. It saves you work on both ends having to put the two resultant values into the proper post or get variables to send to PHP.
Then on the PHP side, it's a little easier to validate the one value's format before splitting it, as you can use a single regex like:
// Validate both values at once: 1 or more digits, a pipe, and one or more digits
if (preg_match('/^(\d+)\|(\d+)$/', $_POST['jqueryinput'])) {
// explode() and use in PHP...
list($val1, $val2) = explode("|", $_POST['jqueryinput']);
}