In Laravel, we can set the value for a form field element with this:
{{Form::text('name', 'predefined name')}}
The above work well, even in the edit mode the predefined name will always appear in the text field. However, for checkbox and radio button, it does not work.
{{Form::checkbox('country', 'UK',true)}}
{{Form::radio('gender', 'male',true)}}
{{Form::radio('gender', 'female',false)}}
No matter what I set for the checkbox & radio button, it will be ignored and use the data from database. This become an huge trouble for array of checkbox.
{{Form::checkbox('country[]', 'UK',true)}}
{{Form::checkbox('country[]', 'SG',false)}}
{{Form::checkbox('country[]', 'US',false)}}
How can I solve it? Thank you.
If you are referring to the visibility of the value of a checkbox or radio-array:
Usually you will add text to a checkbox or radio-element via a label.
{{ Label::for('nameOfInput', 'Value') }}
Related
I have one really simple question. I have a form with three groups of dynamically generated check boxes. Let's say one of these groups of check boxes needs to have all check boxes as default set.
This is not a problem, the problem comes when I submit the form and I need to have the state of the checkbox.
Here is the HTML code I generate:
#for($i=0;$i<24;++$i)
{!! Form::checkbox('check_hour[]', $i, $check_hour[$i], ['class' => 'check_hour']) !!} {{($i+1)}}h
#endfor
and here is the code in the Controller:
for($i=0;$i<24;++$i) $check_hour[$i] = (isset(Input::get('check_hour')[$i])) ? true : false;
In this situation I have no problem to save the checkbox state, but I need all check boxes to bet set as default.
The form used to add a new item into the database and edit existing items is the same form. A "Mode" is passed into the form to tell it if were adding something new or to load the existing item for editing. So....
<input type="checkbox" name="fflreq" id="fflreq" value="<?=$row['FFLr']?>" <?php if ($row['FFLr']=="Yes") {echo 'checked';} ?>>
When a new item is being added, $row['FFLr'] doesn't exist so of course the value is BLANK or NULL or i guess 0 if i don't initially check the checkbox- The form processor coverts this into a "No" and inserts it into the database.
Now here is my problem - When I come back to a item and the form is in edit mode, the VALUE in this checkbox is now "No" - when I am clicking the checkbox to change its status, I see the checkbox become 'checked' but the value is not changing. in other words the click/check status is not setting the value of $_POST['fflreq'] to YES or 1.
I thought, that checking or unchecking a form checkbox replaces whatever is currently in the value='' attribute with a 1 or 0 to represent yes/no on/off or whatever. Why would the value pulled in from the database not change on form submission?
You need to do it in this way:
<input type="checkbox" name="fflreq" id="fflreq" value="Yes" <?php if ($row['FFLr']=="Yes") {echo 'checked';} ?>>
and when submit the form if the above checkbox is checked then you recieved the $_POST["fflreq"] in the form submit page and if it is not checked you recieve nothing in $_POST
so in the submit page you can do this:
$fflreq = "No"
if(isset($_POST["fflreq"]) && $_POST["fflreq"] == "Yes")
{
$fflreq = $_POST["fflreq"];
}
//then you can simply do anything with the $fflreq such as inserting it into database etc.
I hope this can be of some help.
That's not how it works. If you have "checked" the check box then it (along with it's value) will be sent with the post/get (i.e. submission) of the form. If you haven't checked it, then it won't be set...
If the checkbox is active, the browser sends the key/value pair defined in the input tag. However, if the checkbox is not active, nothing at all is sent for this checkbox.
There are two options to deal with this:
The clean option is to be aware of this on the server side, and assume that the checkbox was not active whenever no value comes through.
A more dirty variant is having a <input type="hidden"> tag just before the checkbox, using the same name, but the value you need to see when the checkbox is inactive. This way, when the checkbox is active, you'll still get the desired value from the checkbox, because it will overwrite the hidden value. However, if the checkbox is inactive, you'll get the value from the hidden field.
Not really, the check/unchecked status is read out by looking if the HTML name attribute value is present in the $_POST param.
You can check this with:
<?
if (!empty($_POST['fflreq'])){ /*checked*/ }
else{ /*unchecked*/ }
?>
The value of the HTML attribute value always stays whatever it is in your HTML. So no user interaction (except JS) can change that.
Working with PHP empty() function lets you bypass all the "Yes" "1" string int casting issues.
Further I would use ternary notation for these kind of things:
<input type="checkbox" name="fflreq" id="fflreq"
value="<?=$row['FFLr']?>" <?=(!empty($row['FFLr'])?'checked':'')?>>
I have a form with some fields of type text and 2 fields (name and second name) that have a value by default, and these 2 fields are of type="hidden".
Now, I would like to add a link on the form "Fill an order for another person". If the user click on this link the same form must be reloaded but with a little difference: the fields name and second name must be of type text to allow the user enter a name/second name.
I can reload the form, but the fields name and second name remain hidden, because I don't know how define a condition that say to change the types if the user clicks on the link. Could you explain how can I do this?
The fields that are filled by default:
if ($field_label=="Name")
{
return sprintf("<div class='ginput_container'>$name<input name='input_%d' id='$field_id' type='hidden' value='$name' class='ginput_container' $max_length $tabindex $html5_attributes %s/></div>", $id, $field_id, $html_input_type, esc_attr($value), esc_attr($class), $disabled_text);
}
else if ($field_label=="Second name")
{
return sprintf("<div class='ginput_container'>$secondname<input name='input_%d' id='$field_id' type='hidden' value='$secondname' class='ginput_container' $max_length $tabindex $html5_attributes %s/></div>", $id, $field_id, $html_input_type, esc_attr($value), esc_attr($class), $disabled_text);
}
add another input field and call it something like "form_submitted". set the value of this to 1.
you will be able to check for this in your php script once you've submitted the form. if this value is set use text if not use hidden
EDIT: If you want to only display one additional form you can create it on page load and use javascript to make it visible once a user clicks on the link
if you want to be able to add multiple forms, use javascript to build the new ones. each click on the link can add another form. the only thing you need to make sure is that you update the name of the form elements.
You do not need to reload the form. You can just use jQuery to remove the existing hidden field, and create a new textbox in it's place, with the same value as the hidden one.
For example: http://jsfiddle.net/FT2B3/1/
I have an inline frame within a form. The inline frame actually contains the form element which is generated programatically (a radio which holds a value). How can I retrieve that value hold by the radio from the page which contains that inline frame. Any idea? thanks for reading
What MvanGeest is suggesting is for you to use javascript to transfer values of the radio buttons to a hidden field in your main page form
so for each radio button you would have onclick="valueSet(this.value)"
and in the function valueSet (that you define in the iframe) you would set the value of the hidden form field
function valueSet(radioValue){
window.parent.document.forms["nameOfYourForm"].elements["nameOfHiddenElement"].value = radioValue;
}
and in the main window, in the FORM you have
<input type="hidden" name="nameOfHiddenElement" value="" />
and you can set the default value for it as well
Don't forget to give your form a name attribute and use that name in the function where it references forms["nameOfYourForm"]
Does that make sense for your project? Or am I totally off base here?
This site explains about cross-frame access in JavaScript: http://www.west-wind.com/Weblog/posts/589454.aspx. Be aware that the same-origin policy is enforced; in other words, you cannot access a frame which contains a page loaded from another domain.
Given that when using an html radio box, to send values to a php script, only the selected values is sent to the php script, still holds true.
Do I still need to check that the user has selected something, when I am only interested in the selected value being sent to the php script, if the user does not select anything we do nothing or I guess I could prompt the user to select something.
If so, whats the best way to handle this with regards to radio boxes?
<!--Using the radio box, a single selected value is sent to the php script for processing-->
<td width="100px" height="30px">
<input type = "radio"
name = "selection"
value = "Lays" />Lays Chips 0.99c<br />
<input type = "radio"
name = "selection"
value = "Ruffles" />Ruffles $1.85<br />
The user will be able to click the "submit" button even without selecting an option from your radiobox.
If it's ok to send the form without a selection and you just want to do something different (or ignore it) when nothing is selected, you can do this:
<?php
if (isset(($_POST['selection'])) {
//Use it.. and make sure to validate user input.
} else {
//nothing was selected. Do something or just ignore?
}
?>
OTOH, if you want to prevent submiting without a selection, you will need to use some JavaScript to do it.
Even if the radiobox is not set, you can still post the form back to the server. Unless, of course, you have Javascript that prevents one from clicking on the submit form if the radiobox is not set.
So, you still need to check whether the radiobox is set or not, before working on it.