PHP - Get the input's label content? - php

I'm making order page which contain a list of items and each of them has input to type in the amount you want to order.
The markup is like this:
<label for="item1">Fried Rice</label>
<input type="number" id="item1" name="item1">
...
//I have like 10 more items below
So basically, how do I get the string Fried Rice in PHP?
I'm thinking of hard-coding the input's name to "fried rice", but it will be troublesome if someday the item name is changed.
Any solution? Thanks. I'm new to PHP by the way.

I would do it like this
<label for="item1">Fried Rice</label>
<input type="number" id="item1" name="item1[Fried Rice]">
--EDIT--
if you have a mapping of your indices (textual name) with the number then just put the index number
<label for="item1">Fried Rice</label>
<input type="number" id="item1" name="item1[id goes here]">
--EDIT-- with sample output
Array
(
[item1] => Array
(
[Fried Rice] => 1
)
)

you can take as hiddne value for item like
<label for="item1">Fried Rice</label>
<input type="hidden" id="item1_desc" name="item1_desc" value="Fried Rice">
<input type="number" id="item1" name="item1">
...
please take as you need more for 10
in post you will get [item1_desc] for your label value
let me know if i can help you more.

Try something like
<?php $name='Fried Rice' ?>
<label for="item1"><?php echo $name ?></label>
<input type="number" id="item1" name="<?php echo $name ?>">
This way, if the name changes, you only need to change it in one place.

Related

HTML Number Field Checking

In HTML number field I want that a user enters numbers between 10 to 20 and then 40 to 50... How to implement this inside an HTML text field or a number field.
As in HTML 5 you can do this
<form action="">
<input type="number" min="10" max="20"/>
<input type="submit"/>
</form>
If you are getting value from database/ want to integrate with php values, you can do something like following.
<input type="number" min="<?php echo $minVal ?>" max="<?php echo $maxVal ?>"/>
REMEMBER: when you are doing validation you has to do the validation for front end end back end. And min and max attribute are not only limited to number type, you can use it for date type also refer this
if you dont want to use javascript or jquery you can use min and max attributes
<input type="number" min="10" max="20">
<input type="number" min="40" max="50">
I don't suppose it is possible using html alone. You will have to use JavaScript validation because the condition here is complex with regard to html.
You can try with patter in html5 :
<input type="text" name="range" pattern="^(?:\d[10-20]|\d[40-50])$" >
Check it simple and short using javascript :
function allownums(a)
{
value1 = a.value;
if( (10<value1==value1<20) || (40<value1==value1<50))
alert("vaild")
else
alert("invaild")
}
<input type='text' id='numonly' onkeyup='allownums(this)'>
if you want to using html only you can use two number field:
<input type="number" min="10" max="20">
<input type="number" min="40" max="50">

dynamically selecting all variables with the same prefix in php

I have a form and there is a button to append another set of input boxes if you wish to add more information. Everytime it adds a new set of boxes all the input boxes get a unqiue number added on for that set of input boxes.
Example:
If you have three sets of input boxes it would look like this:
name, age, gender, dob
name1, age1, gender1, dob1
name2, age2, gender2, dob2
However, when I send this information over to my php file I extract the information from the array so each one is a variable. So, name would be $name and name1 would be $name1 and so on. But my question is how can I sanitize and validate all the names at once and all the ages at once etc..
The reason I am asking is because I have googled this alot and I can't find an answer on how to do this.
Try to create sets as given in sample below:
For first set:
<input type="text" name="name[]" id="name1" />
<input type="text" name="gender[]" id="gender1" />
<input type="text" name="age[]" id="age1" />
<input type="text" name="dob[]" id="dob1" />
For second set:
<input type="text" name="name[]" id="name2" />
<input type="text" name="gender[]" id="gender2" />
<input type="text" name="age[]" id="age2" />
<input type="text" name="dob[]" id="dob2" />
and set all the further sets accordingly.
Now, to get posted data you can use
<?php
echo "<pre>".print_r($_POST, true)."</pre>";
?>
You may use something like this for each entity:
<input type="text" name="age[]" id="age1" />
Here, id should be in incremental order with JavaScript or jQuery and name should be same which will give you an array for all the attributes in $_POST or $_REQUEST
Print $_REQUEST and you will come to know how exactly you can get all the data.
You are already getting the array for the name, age, etc.
To sanitize use array_map() function in php. It will sanitize the array.
EXAMPLE
$result = array_map("stripslashes", $result);
Here $result is an array

How to store a php variable with spaces into a html variable?

I have a php array element that would like to submit as a POST value but it is only accepting the first word due to the spacings. Below is my source code :
<input type="number" class="form-control" placeholder="Please enter the amount to order" size="5" name='.$row["fname"].' />
When the page is printed it shows as :
<input type="number" class="form-control" placeholder="Please enter the amount to order" size="5" name=Mussel in Chill Sauce />
As you can see, only the word "Mussel" was stored into the name variable of the html form. How do i store the full text "Mussel in chilli sauce" which is derived from my $row["fname"]?
Appreciate your help thanks
You should wrap them in quotes properly:
echo '
<input
type="number"
class="form-control"
placeholder="Please enter the amount to order"
size="5"
name="'.$row["fname"].'" />
';
Sidenote: Thats not a good name value, are you sure thats the value you want? You should consider putting other name value instead as this would look like inside the $_POST variable when its submitted:
Array
(
[Mussel_in_Chill_Sauce] => 1 // going to access this thru $_POST['Mussel_in_Chill_Sauce'] ?
)
Try this code name="'.$row["fname"].'"
Put quotes around attribute values with spaces in them.
For that matter, put quotes around all attribute values. They are sometimes optional and never forbidden.
I think that way to do will help you :).
name="'.$row["fname"].'"
Change your code to this to have name inside double-quotes
<input type="number" class="form-control" placeholder="Please enter the amount to order" size="5" `name="'.$row[fname']." />`

How do you manipulate form values based off local input?

I have a form that has a series of check boxes and some line items have a text input next to them that defines quantity of the item.
<input type="checkbox" name="measure[][input]" value="<?=$item->id?>">
<input class="item_mult" type="text" name="measure[][input]" />
What is the best way to capture the integer from the input field and have it correspond to the check box so I can use it to calculate the total later on?
<input type="checkbox" name="measure[<?php echo $item->id; ?>][checked]" value="<?php echo $item->id; ?>">
<input class="item_mult" type="text" name="measure[<?php echo $item->id; ?>][input]" />
That should give the values $measure[1]['checked'] (only present if checked) and $measure[1]['input']
I corrected your short tags - they're a bad idea, as they are disabled in php by default so can cause issues if you move servers
You can give your array a name/id to associate them, just add it into the name attribute:
<input type="checkbox" name="measure[1][my_checkbox]" value="<?=$item->id?>">
<input class="item_mult" type="text" name="measure[1][my_text]" />

Is it possible to group $_POST variables?

I have a huge form (for an internal CMS) that is comprised by several sections, some of them are optional some of them are compulsory. All is under an humungous form (it has to be like this, no ajax, no other ways :-( )
Since in a Dilbertesque way everything get changed every second I was wondering if there is any simple way of grouping $_POST data, I mean sending POST like this:
$_POST['form1']['datax']
or to retrieve data from server side easily, and by easily I mean withouth having to expressily declare:
$array1 = array($_POST['datax'],$_POST['datay'],...);
$array2 = array($_POST['dataalpha'],$_POST['dataomega'],...);
since there are around 60 fields.
I hope I was able to explain this well and as always thank you very much..
If you give your input elements array-like names, they arrive in the PHP $_POST (or $_GET) array as an array:
<input type="text" name="foo[]" value="a"/>
<input type="text" name="foo[]" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo[bar]" value="d" />
<input type="text" name="foo[baz][]" value="e" />
<input type="text" name="foo[baz][]" value="f" />
Goes to:
print_r($_POST)
foo => array (
0 => a
1 => b
2 => c
bar => d
baz => array(
0 => e
1 => f
)
)
If you name your inputs properly, you can do that. Example:
<input type="text" name="textInput[]" />
<input type="text" name="textInput[]" />
That will populate an array in $_POST named textInput. That is:
$_POST['textInput'][0] == "whatever the first was set to be"
$_POST['textInput'][1] == "whatever the second was set to be"
Using square brackets after the input name will cause it to be grouped in PHP:
<input name="foo[]" type="text" value="1" />
<input name="foo[]" type="text" value="2" />
You can also make an associative array:
<input name="foo[bar]" type="text" />
I think multi-dimensional arrays would also work, but I'm not sure if I've actually tried it.
Edit: Here's the same thing answered in the PHP FAQ.
you can use your form fields like this:
<input type="text" name="form1['datax']"/>

Categories