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.
Related
Im using Codeigniter - I am displaying radio buttons like so :
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime"> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime"> Part Time
</fieldset>
Which works fine, they display.
Here is the code in the model.
'time'=>$this->input->post('time'),
which also works fine , it saves the choice to the database. But how do I get the radio button to populate when the page loads with the choice from the database?
You can use the form helper, which has a function called "set_radio", the very last function on this page;
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
<input type="radio" name="time" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?> />
<input type="radio" name="time" value="parttime" <?php echo set_radio('time', 'parttime'); ?> />
I hope this helps.
Just as #Craig said, using set_radio is what you will need.
php
<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />
Note
The value of this radio button is "2". Whatever the value is, needs to be the second parameter in set_radio.
Your code would look something like this:
HTML
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" id="radfull" value="fulltime" <?php echo set_radio('time', 'fulltime'); ?>> Full Time<br/>
<input type="radio" name="time" id="radpart" value="parttime" <?php echo set_radio('time', 'parttime'); ?>> Part Time
</fieldset>
If you want either option checked by default (when the page loads) that is where you would add the third parameter TRUE to the corresponding radio.
ALSO NOTE
In your controller, you will need to load the form_validation library for any of this to work.
php
$this->load->library('form_validation');
Hope this helps!
EDIT
My apologies, I misread the question. What I had above is...before the form is officially/successfully submitted. In your case, the form has already been (successfully) submitted and you want to...edit the form (for lack of better words) - so you need a way of populating the inputs with whatever the user chose.
What I have done in the past - in my view - is just have a ternary operation right on the element.
For example, let's say I am returning a user, and there is a "time" property.
HTML
<fieldset>
<legend>Part Time / Full Time:</legend><br>
<input type="radio" name="time" value="fulltime" <? echo($user['time'] === 'fulltime') ? selected="selected" : ''; ?> />
<input type="radio" name="time" value="parttime" <? echo($user['time'] === 'parttime') ? selected="selected" : ''; ?> />
</fieldset>
Maybe that's closer to what you are looking for?
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 :)
I have a form such as the one below:
<input type="checkbox" name="type" value="wash"><label>Wash</label><br>
<input type="checkbox" name="type" value="no wash"><label>No Wash</label><br>
<label>Other (Specify)</label><br>
<input name="type"><br>
If you notice for all three i am using "type" as the input name. The point being that the user will be given two options, if none of the two options apply to them they should enter a value in other. Now in the database i have the field type, so if they selected the first two and entered a value in the field or if they only wrote a value in the field i still want it to to be part of the type field. So how can i make it so that if they select the input field it should also insert in "type".
Do you mean something like this?
HTML:
<input type="checkbox" name="type[]" value="wash"/><label>Wash</label>
<input type="checkbox" name="type[]" value="no_wash"/><label>No wash</label>
Other type:
<input type="text" name="other_type"/>
PHP:
if (!empty($_REQUEST['other_type']))
$_REQUEST['type'][] = $_REQUEST['other_type'];
var_dump($_REQUEST['type']);
First of all you should better use radio buttons instead of checkboxes.
Then you could do the following
<input type="radio" name="type" value="wash"/><label>Wash</label>
<input type="radio" name="type" value="no_wash"/><label>No wash</label>
<input type="radio" name="type" value="other"/><label>Other</label>
<input type="text" name="other_type"/>
Your PHP would then look like this:
if ($_REQUEST["type"] == "wash"){
echo "Wash me please";
}else if ($_REQUEST["type"] == "no_wash"){
echo "no wash";
}else if ($_REQUEST["type"] == "other"){
echo "you want to ".$_REQUEST["other_type"];
}
If you use JS you could even disable the textbox unless the user selects the third option.
Edit: If I got your comment right it would be the easiest like this:
<input type="checkbox" name="wash_me"/><label>Wash your car?</label>
<input type="text" name="other"/><label>What else can we do for you?</label>
PHP
if (isset($_REQUEST["wash_me"]){
echo "wash my car please";
}
if (strlen($_REQUEST["other"]) != 0){
echo "and do the following: ".$_REQUEST["other"];
}
Im trying to create a form using PHP and I cant seem to find a tutorial on what I need so thought Id ask on here.
I have a multiple checkbox option on my page...
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service">Static guarding<br>
<input type="checkbox" value="Mobile Patrols" name="service">Mobile Patrols<br>
<input type="checkbox" value="Alarm response escorting" name="service">Alarm response escorting<br>
<input type="checkbox" value="Alarm response/ Keyholding" name="service">Alarm response/ Keyholding<br>
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
</li>
I'm not sure however how to collect all checkbox values using POST method?
if i use
$service = $_POST['service'];
I only get 'other' returned
Name the fields like service[] instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:
Check if a certain value was selected:
if (in_array("Other", $_POST['service'])) { /* Other was selected */}
Get a single newline-separated string with all selected options:
echo implode("\n", $_POST['service']);
Loop through all selected checkboxes:
foreach ($_POST['service'] as $service) {
echo "You selected: $service <br>";
}
Currently it's just catching your last hidden input. Why do you have that hidden input there at all? If you want to gather information if the "Other" box is checked, then you have to hide the
<input type="text" name="other" style="display:none;"/>
and you can show it with javascript when the "Other" box is checked. Something like that.
Just make the name attribute service[]
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service[]">Static guarding<br />
<input type="checkbox" value="Mobile Patrols" name="service[]">Mobile Patrols<br />
<input type="checkbox" value="Alarm response escorting" name="service[]">Alarm response escorting<br />
<input type="checkbox" value="Alarm response/ Keyholding" name="service[]">Alarm response/ Keyholding<br />
<input type="checkbox" value="Other" name="service[]">Other</span>
</li>
Then in your PHP you can access it like so
$service = $_POST['service'];
echo $service[0]; // Output will be the value of the first selected checkbox
echo $service[1]; // Output will be the value of the second selected checkbox
print_r($service); //Output will be an array of values of the selected checkboxes
etc...
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
You've got a hidden input field with the same name as the checkbox. "later" fields with the same name as an earlier one will overwrite the previous field's values. This means that your form, as posted above, will ALWAYS submit service=Other.
Given the phrasing of your question in the html, it sounds more like you'd want a radio button, which allows only ONE of a group of same-name fields to be selected. Checkboxes are an "AND" situation, radio buttons correspond to "OR"
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]" />