Im using Semantic UI for design and some functionalities and I failed to make multiple value posting in dropdown select - it adds blocks into the select input, but when submitting, posts only numeric string - one of the input values (instead of array). Seems to post the highest number from selection.
Can anyone tell me, how to make it working? (loading previous values works perfect);
<select class="ui dropdown" id="subjs" name="subj" multiple>
<?php
$selected = $cats->getsubj();
$subs = $cats->getsubj(true);
if (isset($subs) && count($subs) > 0)
for ($i = 0; $i < count($subs); $i++) {
$opt = (in_array($subs[$i], $selected)) ? ' selected="selected"' : '';
?>
<option class="subjs" value="<?php echo $subs[$i]['ID']; ?>" <?php echo $opt; ?>><?php echo $subs[$i]['title']; ?></option>
<?php
}
?>
</select>
JS:
<script>
$('.ui.dropdown').dropdown({
maxSelections: 2
});
</script>
PHP:
var_dump($_POST);
the script is listing of categories from database into select as options. Value for each option is it's ID (integer). One article may be liked with more than one category
All advices apreciated... Thanx
Well for all, who make stupid mistakes, simple add '[]' to the multiple select name parameter to post array.
<select name="x[]"></select>
Related
I have a submit button in my form like this :
<?php echo $this->Form->end(__('Submit')); ?>
and I want to embed variables that are part of the input form.
For example, I want to embed a variable that is input here $this->Form->input('user_id');.
Does anyone know how to do this? I currently have this onsubmit as part of the options array, but it throws errors when I try to embed variables.
<?php echo $this->Form->create('Shift', array(
'onsubmit' => 'return confirm("Creating shift for '$id' on ");'
)); ?>
Now making an edit to this question. So the Javascript solution worked fine. Issue now is that I want the alert box to display the user's name, which is what is displayed in the dropbox. However, the HTML tags contain the user's ids. Here is the HTML
<div class="input select required">
<label for="my_user_id">User</label>
<select name="data[Shift][user_id]" id="my_user_id" required="required">
<option value="1">john doe</option>
<option value="2">john johnson</option>
Is there a way to access the value between the <option> tags using Javascript?
You need to use double quotes:
<?php
echo $this->Form->create('Shift', array(
'onsubmit' => "return confirm(\"Creating shift for '$id' on \");"
));
?>
Another option is to use $this->Form->postLink() and set $confirmMessage parameter if it is a simple state shift form.
You can use JQuery
echo $this->Form->create('Shift', array('id' => 'myform'));
echo $this->Form->input('user_id', array('id' => 'my_user_id', 'type'=>'text'));
After the form:
<?php echo $this->Html->scriptBlock('
$(document).ready(function() {
$("#myform").submit(function(event) {
alert("Creating shift for " + $("#my_user_id").val());
});
});
');
?>
I couldn't find a solution on here, thought I wasn't sure what to search for. I have a select box that is populated from my database using PDO. However I was unsure on how to set the selected value to a team that has already been set from the DB.
Update The code only shows the team that matches the variable, however I need to show all teams in the drop down.
<option <?php if($row4['team_id'] == $team_id) { ?>
selected="selected" > <?php }
echo $row4['team_name'];?>
</option>
<?php } ?>
OLD code
$stmt4 = $conn ->prepare('SELECT team_id, team_name FROM team_tbl ORDER BY team_name');
$stmt4->execute();
while($row4 = $stmt4->fetch(PDO::FETCH_ASSOC)) {
echo '<option <? if ($row4['team_id'] == $team_id){?> selected="selected"<? } ?>>'.$row4['team_name'].'</option>';
}
?>
<option <?php if($row4['team_id'] == $team_id) { ?> selected="selected" <?php } ?>></option>
Add the above code to the option field.
I would like to create a requirement that if nothing is selected from a drop down field in my contact form that a message will come up saying "Please choose", and the form will not be able to be submitted unless something is chosen. I have gotten requirements to work on all of my text input forms, but cannot figure out how to create one for the drop down field.
The drop down HTML looks like this:
<div class='container'>
<label for='destemail' > Which department are you trying to reach?*</br> You must select a department.</label></br>
<select name="destemail" id="destemail">
<?php foreach ($emailAddresses as $name => $email) { ?>
<option value="<?php echo htmlspecialchars($name); ?>"><?php echo htmlspecialchars($name) ; ?></option>
<?php } ?></select>
<span id='contactus_destemail_errorloc' class='error'></span>
</div>
I got the other form requirements to work like so:
The HTML -
<div class='container'>
<label for='name' >Your Full Name*: </label><br/>
<input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
<span id='contactus_name_errorloc' class='error'></span>
</div>
The Javascript -
<script type='text/javascript'>
<![CDATA[
var frmvalidator = new Validator("contactus");
frmvalidator.EnableOnPageErrorDisplay();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("name","req","Please provide your name");
</script>
The PHP -
//name validations
if(empty($_POST['name']))
{
$this->add_error("Please provide your name");
$ret = false;
}
I tried the exact same coding for the drop down but with the different id names where appropriate, and it didn't work. Why doesn't this same method work for the drop down?
Help much appreciated!
I can't see what the Validator() code is doing, but you can just check to see whether the select field is empty using Javascript or jQuery.
jQuery way:
if( !$('#destemail').val() ) {
alert('Empty');
}
The problem may lie in that your select box actually does have a value, which is whatever the first value printed out in it is. The Validation function may be checking for any value, and since the select does have one, it returns as valid.
You could set up a default value to show first, something like "Please select a Department", and then do the jquery/javascript check for that text. If it exists, then you know an option has not been selected.
I am using a captcha in a form. When the user submits an invalid answer to the captcha, the page reloads repopulating the fields with the user data. My question is, is there a better (or easier) way to check which dropdown value was selected.
This is what I have now that works:
php
<?php
$state = $sf_params->get('state');
?>
html
<span>State </span>
<select size="1" name="state">
<option value="" selected="selected"></option>
<option value="AL" <?php if ($state == "AL") { echo "selected = 'selected'"; } ?>>Alabama</option>
<option value="AK" <?php if ($state == "AK") { echo "selected = 'selected'"; } ?>>Alaska</option>
<option value="AZ" <?php if ($state == "AZ") { echo "selected = 'selected'"; } ?>>Arizona</option>
<option value="AR" <?php if ($state == "AR") { echo "selected = 'selected'"; } ?>>Arkansas</option>
Is there another solution to getting the GET variable, then parsing the options, looking for the matched value?
Well, it's not really a good practice to build a form on your own. Usually you should use widget from sfForm class, etc ..
But, if you really want to build this kind of stuff, you should bring back a good helper from sf1.0 that helps you building form tag. In your case, the function options_for_select and select_tag might be useful:
options_for_select:
Returns a formatted set of <option> tags based on optional $options array variable.
select_tag:
Returns a <select> tag, optionally comprised of <option> tags.
options_for_select took as second parameter, the selected value you want.
So just, create a file called lib/helper/FormHelper.php and put these two functions inside. Don't forget to load the helper in your view :
<?php use_helper('Form') ?>
And use options_for_select & select_tag to build your select.
First move the values of the select to an array in the Controller like
$states = array('AL' => 'Alabama', ... );
Then you can use the Helper j0k mentioned or go by feet:
By assigning the vars in the controller.
$this->states = $states;
In the Template:
<?php foreach ($states as $key => $value): ?>
<option value="<?php echo $key ?>" <?php if ($state == $value) { echo "selected = 'selected'"; } ?>><?php echo $value ?></option>
<?php endforeach; ?>
But I also recommend to use the Helper.
My problem is that I am making a dynamic generated form using codeigniter. I need my dynamic results to be made into links. These 'links' are contained within a form and when clicked, should pass an item_id. The displayed link however should portray the item_name. I have both the id and name of every product within a foreach, gathered from a query. The reason I am doing it like this and not having them as a radio/drop-down, is because I'm attempting to make this touchscreen friendly. I need to have them list out as links. I am trying to stay away from javascript on this if I can. Just wondering if anyone had an idea of what I could do here. As I asked in my title, what I am looking for is a way to make form inputs as if they where select/option but listed as links. I also tried them as submit tags, but then I cannot display the name and have the value as the id.
EDIT:
I should also add that $key is the id of each product and $info is the name.
foreach($products as $key => $info):
?>
<div class="field_row clearfix">
<?php
/* NEED THE FORM ELEMENTS HERE */
?>
</div>
<?php
endforeach;
You could use jquery and js metadata to achieve this.
N.B. untested code of the top of my head.
<?php
foreach ($products as $key => $info)
{
?>
<div class="product-link {item_id: <?php echo $key; ?>}">
<?php echo $info; ?>
</div>
<?php
}
?>
<script type=text/javascript">
/*<![CDATA[*/
$(document).ready(function() {
// listen event on drop down
$(".product-link").click(function () {
var item_id = $(this).metadata().item_id;
// do what ever you want here, maybe a redirect
window.location.href = "/products/items/"+item_id;
});
});
/*]]>*/
</script>