render individual option from radio Element in Zend Forms - php

I have this element inside my Zend form
$genderOptions = array( 'male'=>'male', 'Female'=>'Female');
$gender= new Zend_Form_Element_Radio('gender');
$gender->setDecorators(array('ViewHelper'))
->setAttrib('name', 'gender')
->setAttrib('class', 'required error pull-right')
->setAttrib('id', 'gender')
->setRequired(false)
->setMultiOptions($genderOptions);
And I want to retrieve the inputs individually in the viewscript (phtml file). I've tried
as
<div>
<span>Male</span>
echo $this->myForm->gender['male'];
</div>
<div>
<span>Female</span>
echo $this->myForm->gender['female'];
</div>
how can I do that using Zend Form?
Thanks

For form elements extending Zend_Form_Element_Multi you can use the getMultiOption($option) to fetch a single option.
View.phtml
<div>
<span>Male</span>
<?php echo $this->myForm->gender->getMultiOption('male'); ?>
</div>
<div>
<span>Female</span>
<?php echo $this->myForm->gender->getMultiOption('female'); ?>
</div>
Alternatively, you may want to check that the option is available before you attempt to use it (or you will get NULL)
<?php
$gender = $this->myForm->gender;
$option = $gender->getMultiOptions(); // returns assoc array
if (isset($option['male']))
printf('<div><span>Male</span>%s</div>', $option['male']);
if (isset($option['female']))
printf('<div><span>Female</span>%s</div>', $option['female']);
?>
Edit
Having re-read your question I can see you're looking for the individual radio elements, rather than the value.
This may be harder to achieve as the Zend_Form_Element_Radio class actually represents all the radio options; Where the view helper Zend_View_Helper_FormRadio loops over each 'option' (i.e. male, female) and returns the complete HTML string with each <input type="radio"/> already included.
Shockingly the Zend_View_Helper_FormRadio helper actually has all of its HTML generation code within one method; This makes it very hard to override it without duplication.
Personally, I would :
Create a new helper MyNamespace_View_Helper_CustomFormRadio extending Zend_View_Helper_FormElement
Copy the entire contents of the Zend_View_Helper_FormRadio (FormRadio()) into your new helper
Modify section where the each radio input is created.
For example
$radio = '<div><span'
. $this->_htmlAttribs($label_attribs) . '>'
. (('prepend' == $labelPlacement) ? $opt_label : '')
. '<input type="' . $this->_inputType . '"'
. ' name="' . $name . '"'
. ' id="' . $optId . '"'
. ' value="' . $this->view->escape($opt_value) . '"'
. $checked
. $disabled
. $this->_htmlAttribs($attribs)
. $this->getClosingBracket()
. (('append' == $labelPlacement) ? $opt_label : '')
. '</span></div>';
Then you can use it within your view with $this->customFormRadio()

Related

jQuery Modification of Ajax Populated Form Fields

I have a form, and when a certain select box changes, it uses Ajax to populate some checkboxes. Works great.
However, when I check these checkboxes, I'd like to use jQuery to modify another part of the form - this does not seem to be working... the jQuery does not fire at all.
Is this something to do with when the jQuery loads vs the ajax request and its just impossible? Or is there likely an error somewhere in my code?
The Ajax.php page (which works just as expected)
... some other code ...
$fields = "";
$i = 0;
foreach($cols as $c){
$fields .= '<tr><td width="50%"><input class="fieldCheckBox" data-num="' . $i . '" type="checkbox" name="data[' . $_GET['t'] . '][fields][' . $i . '][field]" value="' . $c . '"> ' . $c . '</td><td width="50%"><input type="text" name="data[' . $_GET['t'] . '][fields][' . $i . '][fieldAs]" class="form-control" style="width: 150px;"></td></tr>';
$i++;
}
echo $fields;
And then my jQuery request (which isn't firing)
... document.ready function...
$(".fieldCheckBox").change(function(){
alert("were in!");
});
This is your problem:
$(".fieldCheckBox").change(function(){
The value of the checkbox is not actually changing so this will never execute.
Instead you can check for the click event for example:
$(".fieldCheckBox").click(function(){
Edit: If the checkboxes don't exist on page-load, when your javascript is executed, you need event delegation to make sure the events get registered.
A simple example:
$('body').on('click', '.fieldCheckBox', function(){

Equivalent of ASP.NET WebForms ViewState in PHP?

ASP.NET WebForms is outdated, but in one aspect it's far better than PHP: The ViewState. When you submit the form, controls stay. In PHP, you have to do everything all by yourself, rendering HTML.
Quickly, I come up with code that looks like this:
print('<input type="text" name="txtDate" value="' . (isset($_POST['btnSave']) ? $_POST['txtDate'] : Common::FormatDate($row['Date'])) . '" class="datepicker ' . ($dateValid ? '' : 'invalid') . '" />');
So I decided to at least create basic functions for rendering controls:
class Controls
{
public static function TextBox($name, $id, $value, $class)
{
return '<input type="text" name="' . $name . '" id="' . $id . '" value="' . htmlentities($value) . '" class="' . $class . '" />';
}
}
This makes things more comfortable, but it's just not the same as having a ViewState.
Not to mention that you have to duplicate code for both edit and create with different settings:
Create: value = whatever is in $_POST
Edit: value = If form is submitted, take whatever is in $_POST, otherwise take the value from the database/model. (ternary operator here)
This get's even harder when you show/hide popups!
All in all this is a very hard and uncomfortable way and I have absolutely no idea where to start improving it. What are your suggestions?

$_POST doesn't work for PHP post. ISSET reports that no such value was forwared via post method?

I have a form for a comment section. Here every comment has unique IDs.
But however, the comments aren't forwarded to the action PHP form.
Code for comments:
echo '<form action="interact.php" method="post">';
$new_refreshed_ID = 'uni_story_ID_' . $row['ID'] . '_comment_ID_' . $cache_ready_new_comment_ID;
echo '<textarea name="' . $new_refreshed_ID . 'rows="12" cols="70"></textarea>';
echo '<button type="submit">Submit</button>';
echo '</form>';
$_SESSION['assoc'] = $row['ID'];
$_SESSION['cache_comment_details'] = $new_refreshed_ID;
My code for receiving the request:
interact.php:
<?php
session_start();
$assoc = $_SESSION['assoc'];
$get_comment = $_SESSION['cache_comment_details'];
if(isset($_POST[$get_comment])) {
echo "yea!";
} else {
echo "no!";
die();
}
I get no in the interact.php which means that no data was forwarded.
How can this be?
btw comment id's are in this manner (for example):
uni_story_ID_4_comment_ID_17
I did check $new_refreshed_ID. It is showing all the values properly as desired. I did start the sessions in the both PHP files.
echo '<textarea name="' . $new_refreshed_ID . 'rows="12" cols="70"></textarea>';
You need to close the name of textarea like this:
echo '<textarea name="' . $new_refreshed_ID . '" rows="12" cols="70"></textarea>';
Edit:
Even Better to not use php when not needed (to avoid those) you can maybe do something like this:
<textarea name="<?php print $new_refreshed_ID;?>" rows="12" cols="70"></textarea>
Please note that this is an Example, i'm only printing what is really needed with php, otherwise i'll stay with html :)

call values in select box without using zend form

I am new to zend framework. I want to call option values in select box dynamically in view file without using zend form. Please help
MY OLD CODE
$txtCategory = new Zend_Form_Element_Select('category');
$txtCategory->setLabel('Category')
->setRequired(true);
$table = new Application_Model_DbTable_Category();
foreach ($table->getcategory() as $c) {
$txtCategory->addMultiOption($c->ExpenseCategoryID, $c->Category);
}
My categories are in select box.
You can simply echo the element on your view if you don't want to use zend_form
In controller
$table = new Application_Model_DbTable_Category();
$this->view->categories = $table->getcategory();
$this->view->selected = "X"; // currently selected value
In view
<form>
<select name="select2" size="3" multiple="multiple" tabindex="1">
<?php $selected = $this->selected;
foreach($this->categories as $c) {
echo "<option value=\"" . $c->ExpenseCategoryID . "\"" . ($c->ExpenseCategoryID == $selected ? " selected=\"selected\">" : ">") . $c->Category . "</option>";
}?>
</select>
//rest of the element
</form>
You're going to need to learn how to use AJAX and javascript to talk to your PHP server. It's going to be a bit harder than using the Zend form like you are now.
Some links with tutorials:
Tutorialspoint on PHP and AJAX
Tutorials point AJAX series

How to update radio group status in a metabox?

I have implemented radio-group option in WordPress metabox. I got the desire radio group in meta box with respective label, but I failed to update my checked status on selecting radio or saving the post where I am using it. I think there is something I need to put.
<div class="my radio group">
<h2>my radio group </h2>
<?php
$cars = array('BMW', 'FERRARI', 'PORSCHE', 'BENTALI', 'MRX', 'CHEVROLET');
foreach ($cars as $car) {
echo '<input name="my-best-car" type="radio" onchange="javascript:document.post.submit()"';
$option = 'id=" ' .$car . '"';
$option = '<value="' . $car . '"';
if ($car == $my_favorite_car) $option .= "checked";
$option .= '>';
$option .= '<label for=" '.$car .' ">' . $car .' ';
$option .= '</label>';
echo $option;
}
?>
</div>
WordPress meta box save function is also added. My other option type like text, select & checkbox updating properly.
While I trying to update my RADIO-GROUP meta values using:
update_post_meta($post_id, 'my-best-car', $_POST['my-best-car'], true);
Ok I found your mistake in foreach loop you have one < extra before the value so the line would look like this:
$option = 'value="' . $car . '"';
Also your js is not submitting the form so just add js function in head like this for example:
<script>
function submitOnClick(formName){
document.forms[formName].submit();
}
</script>
And in the form instead of:
onchange="javascript:document.post.submit()"
put
onclick="submitOnClick(\'myForm\')"
I tried it and it works you just need to rename your form name accordingly

Categories