First of all, I can't change the name of my input.
I have tried naming the inputs with an array like this:
<select name="info[]" class="select-block" id="2">
But then the css doesn't work.
So, how can I get post from 2 input with the same name with php
<select name="info" class="select-block" id="2">
<select name="info" class="select-block" id="1">
Just use a class or ID or something else for CSS and set up your input names using array notation. I would worry about how your form functions first. Make that work correctly and then worry about CSS, not the other way around.
In CSS if you have select[name=info] then replace it with .select-block and use info[] for name
Quick'n'dirty way, just to show that it is possible.
$qs = file_get_contents('php://input');
$_POST = array();
foreach(explode('&', $qs) as $varval){
list($var, $val) = explode('=', $varval, 2);
if (isset($_POST[$var])) {
if (!is_array($_POST[$var])) {
$_POST[$var] = array($_POST[$var]);
}
$_POST[$var][] = $val;
} else {
$_POST[$var] = $val;
}
}
This need some polishing like urldecoding. Of course it also comes with limitations: when form is sent with enctype multipart (for uploading files) php://input is not available. Also this does not care about arrays, so if you have some form element with name like info[], it wouldn't be parsed correctly. It also can be corrected but personally I would change my HTML/CSS
Related
On front-end i have a hidden dynamic input which has a dynamic atribute name as well
the html echo '<input type="hidden" name="like_c_'.$pc.'" value="like" />';
the php variable is concatenated inside attritute
in html would be like
<input type="hidden" name="like_c_abcde123.'" value="like" />
name="like_c_'.$pc.'"
just to make it easier to understand the code above, html is in bold and php in pre code format
on my php backend I need to get this:
$string = htmlentities($_POST['like_c']);//concatenate variable here
the question is how to concatenate to get the exact $_POST name
like this: name="like_c_'.$pc.'"
my code is sent via jquery serialize also is dinamic generated, with db value on frontend, that's why i need the concatenation on the backend
I don't know if this would be the right way
$string = htmlentities($_POST['like_c_'.$var]);
The problem here is that I think it would not work since like_c_would be treated as name="like_c_"
I think this might works for you.
foreach($_POST as $key => $value) {
if (strpos($key, 'like_c_') === 0) {
$key = htmlentities($_POST[$key]);
}
}
I need to get the name and value from many selects with POST, like this:
foreach ($_POST as $key => $value) {
// do stuff
}
The problem is I have other input types in this same form, like a checkbox, for example. I want to get in this foreach only the selects (I can't call by name cuz' each select have one name)
I guess if all your parameters are in an array yes you can, for example, in your HTML you have something like :
<input type="text" name="data['first_name']" value="something"/>
<input type="text" name="data['last_name']" value="somethingElse"/>
in your PHP code, you can use a foreach like so :
foreach( $_POST['data'] as $data )
{
var_dump($data);
}
The input element types are not sent to the server with your POST data. Only the names and values are.
The only way to differentiate would be to send additional data, such as a list, or by naming the inputs differently.
Really though, you shouldn't be POSTing data that you don't care about anyway. Save bandwidth, hide the elements that go nowhere with JavaScript on POST.
Only way to do that server side is to know exactly which inputs are selects and iterate through these.
$selects = array('select1', 'select2', ...);
foreach ($selects as $select) {
$value = $_POST[$select];
// do stuff
}
If you know the name of the select inside form you can do this
Your HTML:
<form>
<select name='selectName1'>
<option value='xx'>xx</option>
<option value='xx'>xx</option>
</select>
<select name='selectName2'>
<option value='xx'>xx</option>
<option value='xx'>xx</option>
</select>
</form>
Your PHP code
$arraySelect = array('selectName1', 'selectName2', ...);
foreach($_POST as $key=>$value)
if(in_array($key, $arraySelect))
$$key = $value; //this will create a variable with the name you use, in this case $selectName1, $selectName2. And the option the user selected will be assigned.
I'm trying to make a BASIC roulette script.
Is there anyway to get the submitted results of a form using PHP? In fact I know theres a way, but i can't find out how to do it.
So say if my form had several fields I want the result to loop through and show me which fields were filled and the numbers in each.
UPDATE: And say the form has about 40 fields, would I have to name each one in the loop? Any easier way?
$_GET or $_POST depending on the form method.
if(isset($_REQUEST['formInputName'])){
echo $_REQUEST['formInputName'];
}
$_REQUEST looks for GET, POST, and COOKIE.
You can also use $_GET to get a variable from the url (asdf.php?var=2).
If your form looks like this:
<form method="post" action="result.php">
<input type="text" name="foo">
</form>
In result.php you can use the global variable $_POST and loop through it if you want:
foreach($_POST as $name => $value) {
echo $name . ' = ' . $value;
}
If your form has 40 fields, you still need to name them all, but you can automate the process of naming and retrieving them with a loop. For example, if you wanted to create a sum with the value of all the fields, you could name them number1, number2, etc and do:
$sum = 0;
for($i = 1; $i <= 40; $i++)
$sum += $_POST['number' . $i];
You need to define the name of each field using name="something" in the input element, and than in the PHP you're getting it using $_POST['something'] in case you sent the form as method="post" or $_GET['something'] in case of get method
You can see what's sent using var_dump() or print_r(), just write something like that:
echo '<pre>';
print_r($_POST);
Or you can go all over the array using foreach statement:
<?php
foreach($_POST AS $key=>$val)
{
echo $key.': '.$val."<br />\n";
}
?>
How would I go about parsing incoming form data where the name changes based on section of site like:
<input type="radio" name="Motorola section" value="Ask a question">
where 'Motorola section may be that, or Verizon section, or Blackberry section, etc.
I do not have any control over changing the existing forms unfortunately, so must find a way to work with what is there.
Basically, I need to be able to grab both the name="" data as well as its coresponding value="" data to be able to populate the email that gets sent properly.
Well, you don't receive a HTML form, but just field names and values in $_POST. So you have to look what to make out of that.
Get the known and fixed fields from $_POST and unset() those you've got [to simplify]. Then iterate over the rest. If " section" is the only constant, then watch out for that:
foreach ($_POST as $key=>$value) {
if (stristr($key, "section")) {
$section = $value;
$section_name = $key;
}
}
If there are multiple sections (you didn't say), then build an section=>value array instead.
<form action="formpage.php" method="post">
<input type="radio" name="Motorola_section" value="Ask a question">
</form>
$motorola = $_POST['Motorola_section'];
if ($motorola =='Ask a question')
{
form submit code if motorola is selected
}
Well, first off, you shouldn't have spaces in the name field (even though it should work with them).
Assuming it's a form, you can get the value through the $_POST (for the POST method) and $_GET (for the GET method) variables.
<?php
if ( isset( $_POST['Motorola section'] ) ) // checks if it's set
{
$motoSec = $_POST['Motorola section']; // grab the variable
echo $motoSec; // display it
}
?>
You can also check the variables using print_r( $_GET ) or print_r( $_POST ).
i want to find the name attribute with in a tag in a form using php4 . Does any one help me how it find..
eg
<input type="text" name="txt_name" value="" >
I want to know name of all the fields
You need a library/code which implements an HTML DOM, look at these SO questions for more information.
When you submit a form, a url is send to the server in the form "name=value&name1=value1" etc. Here the "name" stands for the attribute "name" of an html element. So If you are using the $_GET or a $_POST then you can get the form item names in an array as follows
$names=array();
foreach($_GET as $key => $value)
{
$names[] = $key;
}
print_r($names);
You can use simple html dom as mentioned by Ignacio Vazquez-Abrams
Add it in your code like this
require_once('Simple_html_dom.php');
To use it I initialized it using this line
$html = new Simple_html_dom();
Load a page or link using the line
$html->load_file('http://www.yourste.com/yourpage.php');
to parse and find this element
$e = $html->find("input", 0);
the echo the value of attribute name
echo $value = $e->name