How do you manipulate form values based off local input? - php

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]" />

Related

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

Store values from dynamically created inputs to a PHP variable

I have a page that allows the user to add and remove text fields to a form using JavaScript.
Text fields are named field1, field2, field3, etc. and depends on how many fields the user has added
I'm trying to store all the values from my text fields into one Php variable;
I understand that i need to store them into an array first and then use implode(), but how can i specify how many inputs there are within my Php code?
Usually the best way to approach this is to use array-named input, as shown in the following example in the PHP docs:
<form action="" method="post">
Nombre: <input type="text" name="personal[nombre]" /><br />
Email: <input type="text" name="personal[email]" /><br />
Cerveza: <br />
<select multiple name="cerveza[]">
<option value="warthog">Warthog</option>
<option value="guinness">Guinness</option>
<option value="stuttgarter">Stuttgarter Schwabenbräu</option>
</select><br />
<input type="submit" value="submit me!" />
</form>
You could use the very same name for each of the user added fields, as in:
<input type="text" id="field1" name="fields[]" />
<input type="text" id="field2" name="fields[]" />
And then just use implode as required:
$imploded_fields = implode(', ', $_POST['fields']);
There are many options:
You can use cookies. Use PHP $_COOKIE to get it. For help.
You can use html hidden input fields - <input type="hidden" value=""> and can store actual number of fields in it.
But, Diego Agulló is better one
You can create a hidden field and initialize it with 1 if on option is already open(field1). And increase the the value of counter while increasing the value of fields and vise verse. On submit you will find the total fields added.
Thanks

Dynamically adding the values of a checkbox that has been inserted dynamically?

How do we dynamically display the sum of all the values of check boxes that have been checked in php.
Its basically like a checkout in a shopping cart.Each item that is to be checked, has a value and the final amount(in the same page at the bottom) should be the sum of rates of all the items that have been checked(without refreshing).
I may need to use AJAX. Can anyone give a simple sample code please
For the checkboxes, you need to make them into an array, say for instance:
<input type="checkbox" name="items[]" id="items[]" value="25" /><br />
<input type="checkbox" name="items[]" id="items[]" value="40" /><br />
<input type="checkbox" name="items[]" id="items[]" value="12" /><br />
... <!-- as many as you want -->
<input type="checkbox" name="items[]" id="items[]" value="20" /><br />
On the PHP side you could then handle it like so...(NOTE: It will come into PHP as an array)
$items = $_POST["items"];
//it's an array -- feel free to do a var_dump($items) to see its content
//to sum you could even do an $total_amount = array_sum($items);
//but i would advise cleaning up the values first
And yes, you can achieve the same if you submit the form using AJAX (e.g. via jQuery)
Happy coding :)

Joomla Extra User Field

I'm working with Joomla User Form. I need to create some extra fields. I googled, and got this tutorial
I followed but that wasn't enough for me because I have to handle with checkbox and radio button. The following form code didn't save submission data (I mean checked/uncheked).
<input class="inputbox" type="checkbox" name="hasweb" id="hasweb" size="40" value="<?php echo $this->user->get('hasweb');?>" />
But the following is fine when I put any data there.
<input class="inputbox" type="text" name="hasweb" id="hasweb" size="40" value="<?php echo $this->user->get('hasweb');?>" />
I'm novice in Joomla. Please help me.
There is a difference between the value and the checked state with HTML checkbox elements. So if you want to have the checkbox represent the actual choice you have to do something like this:
<input class="inputbox" type="checkbox" name="hasweb" id="hasweb" size="40" value="reallyhasweb" <?php echo $this->user->get('hasweb') == "reallyhasweb" ? 'checked="checked"' : ''; ?>" />
This should place checked="checked" in the HTML when the user has selected the checkbox and will make the checkbox selected.

Unchecked checkboxes in an array

I'm building a form with the possibility to add more group of fields, to process them i read out the array in a for loop
the script:
<?php
foreach ($_POST as $key => $value) {
$$key = $value;
}
$count = count($name);
for ($i=0; $i<$count; $i++){
?>
<strong><?php echo $name[$i]; ?></strong> (<?php echo $check[$i]; ?>)<br /><?php echo $select[$i]; ?><br /><br />
<?php
}
?>
<form method="post">
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<button>Add another group</button>
<input type="submit" />
</form>
If all checkboxes are checked there is no problem but if only the last one is checked it counts only one checkbox in the array, name[0] is then combined with check[0] but check[0] is really check[2]. English is not my native language so i don't know the right words.
Actually, there is a decent workaround for this as proposed by Sam in this answer on Stack Overflow:
Post the checkboxes that are unchecked
It worked for me, and I suspect you and I had a similar problem (mine being that I had/have upwards of 300 input fields in similar(ish) groups and didn't want to write validation rules for every one of those individual fields, just rules targetted at each family of input types e.g. the email addresses, or the postcodes. In brief, the technique is that you place a hidden input field, with the same name, before your checkbox field. Setting the value of the hidden field (type='hidden') to '0' will ensure that at least one key/value appears in your POST array, with the '0' being superceded by a later '1' only if the box is checked. I needed the '0' value to allow people to 'unset' an option they had previously 'set', for example that they were willing to show their contact data. This technique allows me to present the user with much the same form for an update as they would get at at first registration. Thanks to Sam!
That's normal PHP behaviour, when a checkbox is not checked it does not includes it in $_POST variable ...
Yes. That's how it is. There is no workaround for this. Using field[] identifiers is only applicable for unstructured input fields. If you depend on the ordering and relation, then unset fields will prevent this from working.
You have no other option but to set explicit indexes. You should bite into the sour apple and do so for name[0], check[1] and select[2]. Use a PHP loop to simplify it:
foreach (range(0,2) as $i)
echo <<< END
<div class="group">
<input type="text" name="name[$i]" /><br />
<input type="checkbox" name="check[$i]" value="true" /><br />
<select name="select[$i]"><option>1</option><option>2</option><option>3</option></select>
</div>
END
I was having the same exact problem with some parts of a form that can be add on user's wish. I came to this really simple workaround using basic javascript:
Add a hidden type input just after your checkbox, assign its value to the state of the checkbox and it's this hidden input that will be reported in your $_POST, will be true or false.
<input type="checkbox" onchange="this.nextSibling.value = this.checked">
<input type="hidden" name="state[]" value="false">
Just change value to true if your checkbox is checked="checked" by default.

Categories