Add class to label with class if radio is checked - php

I want to add a class 'checked' to the <span class="label product-option"> when a radio button is checked and remove when a other radio is checked.
How can I extend my code for this?
$htmlValue = $_value->getOptionTypeId();
if ($arraySign) {
$checked = (is_array($configValue) && in_array($htmlValue, $configValue)) ? 'checked' : '';
} else {
$checked = $configValue == $htmlValue ? 'checked' : '';
}
$spanClass = "input-radio";
if ($checked) {
$spanClass = "input-radio checked";
}
$selectHtml .= '<li>' . '<span class="input-radio"><input type="' . $type . '" class="' . $class . ' ' . $require
. ' product-custom-option"'
. ($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"')
. ($this->helper('core')->currencyByStore($_value->getPrice(true), $store, false) ? '0' : ' checked="checked"')
. ' name="options[' . $_option->getId() . ']' . $arraySign . '" id="options_' . $_option->getId()
. '_' . $count . '" value="' . $htmlValue . '" ' . $checked . ' price="'
. $this->helper('core')->currencyByStore($_value->getPrice(true), $store, false) . '" /></span>'
. '<span class="label product-option' . $spanClass . '"><label for="options_' . $_option->getId() . '_' . $count . '"><span class="option-name">'
. $this->escapeHtml($_value->getTitle()) . '</span>' . $priceStr . '</label></span>';
if ($_option->getIsRequire()) {
$selectHtml .= '<script type="text/javascript">' . '$(\'options_' . $_option->getId() . '_'
. $count . '\').advaiceContainer = \'options-' . $_option->getId() . '-container\';'
. '$(\'options_' . $_option->getId() . '_' . $count
. '\').callbackFunction = \'validateOptionsCallback\';' . '</script>';
}
$selectHtml .= '</li>';

Your code shows that you already know how to tell if an element is checked or not, and how to set variables containing class names.
Just extend that logic.
$spanClass = "input-radio";
if ($checked) {
$spanClass = "input-radio checked";
}
...
'<span class="' . $spanClass . '">'

I didn't bother to check you php-code to find out how the html would look like exactly (maybe use Streams next time?), but you need to register an eventhandler like this:
(using jQuery as you seem to already be using it anyways)
$('.input-radio input[type=radio]').change(function() {
// $('.input-radio.checked').removeClass('checked'); // this works if you only have one .input-radio
/* if you want multiple (with different names) */
// note: template string are only available in ES6, if you use earlier versions, use the + to concatinate strings
$(`.input-radio.checked input[type=radio][name=${this.name}]`)
.parents('.input-radio.checked').removeClass('checked');
$(this).parents('.input-radio').addClass('checked');
});
/* This CSS is NOT needed and is only used for effect */
.wrapper span {
display: block;
/* To make them vertically aligned, usually makes no sense for span I know */
}
span.input-radio.checked {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<span class="input-radio checked">
<label>
Radio Button
<input type="radio" name="myRadio" checked/>
</label>
</span>
<span class="input-radio">
<label>
Radio Button
<input type="radio" name="myRadio"/>
</label>
</span>
<span class="input-radio">
<label>
Radio Button
<input type="radio" name="myRadio"/>
</label>
</span>
<span class="input-radio">
<label>
Radio Button
<input type="radio" name="myRadio"/>
</label>
</span>
<span class="input-radio">
<label>
Radio Button
<input type="radio" name="myRadio"/>
</label>
</span>
<span class="input-radio">
<label>
Radio Button
<input type="radio" name="myRadio"/>
</label>
</span>
</div>
As a side note, personally I prefer using data-attributes for things like this instead of classes as that more clearly separates design from function.

Related

Php toggle with a form: logic error?

I can't get my head around why this code doesn't keep toggling.
What should be changed?
print_r( $_POST );
$direction = isset( $_POST['direction'] ) ? $_POST['direction'] : 'DESC';
$opposite = $direction == 'DESC' ? 'ASC' : 'DESC';
echo '
<form method="POST" >
<input type="submit" value="' . $opposite . ' " name="direction">
</form>
';
The culprit is:
<input type="submit" value="' . $opposite . ' " name="direction">
^^
Notice how you've got an extra space at the end of the value, value="' . $opposite . ' "
Try changing it to:
echo '
<form method="POST" >
<input type="submit" value="' . $opposite . '" name="direction">
</form>
';

How to add own CSS to includeded form in PHP

I have proxy server powered on PHProxy 0.5b2 where is included mini url form. But there is an problem with CSS. Every visited page overwrite and overlap my own CSS.
I tried to add own style="" for every object but is not working (visited sites overwrite css).
My index.inc.php
if ($_flags['include_form'] && !isset($_GET['nf']))
{
$_url_form = '<div style="width:100%;margin:0;text-align:center;border-bottom:1px solid #725554;color:#000000;background-color:#F2FDF3;font-size:12px;font-weight:bold;font-family:Bitstream Vera Sans,arial,sans-serif;padding:4px;">'
. '<form method="post" action="' . $_script_url . '">'
. ' <label for="____' . $_config['url_var_name'] . '">Address:</label> <input id="____' . $_config['url_var_name'] . '" type="text" size="80" name="' . $_config['url_var_name'] . '" value="' . $_url . '" />'
. ' <input type="submit" name="go" value="Go" />'
. ' [go: up one dir, main page]'
. '<br /><hr />';
foreach ($_flags as $flag_name => $flag_value)
{
if (!$_frozen_flags[$flag_name])
{
$_url_form .= '<label><input type="checkbox" name="' . $_config['flags_var_name'] . '[' . $flag_name . ']"' . ($flag_value ? ' checked="checked"' : '') . ' /> ' . $_labels[$flag_name][0] . '</label> ';
}
}
$_url_form .= '</form></div>';
$_response_body = preg_replace('#\<\s*body(.*?)\>#si', "$0\n$_url_form" , $_response_body, 1);
}
Any help?
Thanks
Peter

Combine array and MySQL results

I have a quiz that uses an array to store the questions:
$array=array(
'1'=> array('1','What does everyone know when they see it?','quest1','Good publicity','Bad punctuation', 'Good business writing', 'Bad spelling','../images/example03.jpg'),
'2'=> array('2','What do people write instead of \'now\'?','quest2','Presently','At this moment in time', 'Currently', 'At the present time','../images/example03.jpg'),
);
I then display the questions using this loop:
foreach($array as $quiz)
{
echo
'<div class="question">
<div class="questionList">
<h4>' . $quiz[0] . '. <strong>' . $quiz[1] . '</strong></h4>
<ul>
<li><input type="radio" name="' . $quiz[2] . '" value="1"> '. $quiz[3] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="2"> '. $quiz[4] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="3"> '. $quiz[5] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="4"> '. $quiz[6] . '</li>
</ul>
</div>
<img src="'. $quiz[7] . '" class="questionImg01" />
<div class="clr"></div>
</div>
';
}
}
If someone doesn't answer all the questions the first time, I store the values for each question in MySQL or "0" if there was no answer. I would like to check the radio buttons that the user has already answered, but can't work out how to combine the quiz array and the MySQL results.
The table structure for the results is:
quiz_id | username | quest1 | quest2 | quest3 | quest4 | quest5 | quest6 | quest7 | quest8 | quest9 | quest10 | complete
Where quiz_id is the reference number for that quiz and complete is a flag to record if the user has answered all the questions. I'm retrieving the results like this:
$results = mysql_query("SELECT * FROM quiz_results_baseline WHERE username = ('$username')");
Is there a way I can combine these two?
I also have the problem where I can't make this query include a WHERE statement - I would like to store all the quiz results in one table and then refer to each user's quiz results using the quiz_id:
mysql_query("INSERT INTO quiz_results_baseline (quest1,quest2,quest3,quest4,quest5,quest6,quest7,quest8,quest9,quest10,username,quiz_id,complete) VALUES $queryData ON DUPLICATE KEY UPDATE quest1=VALUES(quest1),quest2=VALUES(quest2),quest3=VALUES(quest3),quest4=VALUES(quest4),quest5=VALUES(quest5),quest6=VALUES(quest6),quest7=VALUES(quest7),quest8=VALUES(quest8),quest9=VALUES(quest9),quest10=VALUES(quest10),complete=VALUES(complete)");
Any help would be appreciated!
In order to get your half completed quiz results to render along with the quiz, try the following code.
$questions=array(
'1'=> array('1','What does everyone know when they see it?','quest1','Good publicity','Bad punctuation', 'Good business writing', 'Bad spelling','../images/example03.jpg'),
'2'=> array('2','What do people write instead of \'now\'?','quest2','Presently','At this moment in time', 'Currently', 'At the present time','../images/example03.jpg'),
);
// do your query for the row of answers
// $answers = mysql_fetch_assoc(...);
// for testing
$answers = array(
'quest1' => '1',
'quest2' => '4');
for($i = 1; $i <= count($questions); $i++)
{
$quiz = $questions[$i];
$answer = $answers[$quiz[2]];
echo
'<div class="question">
<div class="questionList">
<h4>' . $quiz[0] . '. <strong>' . $quiz[1] . '</strong></h4>
<ul>
<li><input type="radio" name="' . $quiz[2] . '" value="1" ' . (($answer == '1') ? 'checked' : '') . '> '. $quiz[3] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="2" ' . (($answer == '2') ? 'checked' : '') . '> '. $quiz[4] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="3" ' . (($answer == '3') ? 'checked' : '') . '> '. $quiz[5] . '</li>
<li><input type="radio" name="' . $quiz[2] . '" value="4" ' . (($answer == '4') ? 'checked' : '') . '> '. $quiz[6] . '</li>
</ul>
</div>
<img src="'. $quiz[7] . '" class="questionImg01" />
<div class="clr"></div>
</div>
';
}
You will have to do your query to get the $answers array, but I put in an example so you can see how to use the result.
I ran the code and the results looks like this:
Hope that helps!

How can I create a "go to a page" dropdown list in PHP?

I am trying to extend a component for Joomla called Jquarks. This component allows you to create paginated quizzes, and answer the questions or correct your answers by going back and forth through the questions. I think this handled by some Javascript code.
This is the piece of code I believe makes this functionality work, but I don't know how to start to modifying it. I want to have a dropdown list containing all the page numbers, to allow the user go to a navigate to a specific page.
// adding the back next link if multi-pages
if ($nbrPage > 1)
{
if( $pNum == (int)($qNum / $nbrQuestionPage) || $qNum == $totalNbrQuestions )
{
if ($pNum == 1)
{
echo '<span class="jquarks_qprog"><a id="jquarksPage_' . $pNum . 'next" href="#">' . JText::_('NEXT') . '</a>'
. '<p>' . JText::_("PAGE") . ' ' . $pNum . ' / ' . $nbrPage .'</p>' .'</span></div>' ;
}
elseif ( $pNum == $nbrPage )
{
echo '<span class="jquarks_qprog"><a id="jquarksPage_' . $pNum . 'back" href="#">' . JText::_('BACK') . '</a>'
. '<p>' . JText::_("PAGE") . ' ' . $pNum . ' / ' . $nbrPage .'</p>' .'</span></div>' ;
}
else
{
echo '<span class="jquarks_qprog"><a id="jquarksPage_' . $pNum . 'back" href="#">' . JText::_('BACK') . '</a>
|
<a id="jquarksPage_' . $pNum . 'next" href="#">' . JText::_('NEXT') . '</a>'
. '<p>' . JText::_("PAGE") . ' ' . $pNum . ' / ' . $nbrPage .'</p>'
. '</span></div>' ;
}
$pNum ++ ;
}
}
$qNum++ ;
}
if ($nbrPage == 1 ) :
echo "</div>" ;
endif ;
?>
</div>
<div style="clear: both;"></div>
<div>
<p>
<?php if ($nbrPage > 1) : ?>
<input type="submit" value="<?php echo JText::_('SUBMIT_ANSWERS_CHECK_PAGES') ; ?>" id="send" name="send" />
<?php else : ?>
<input type="submit" value="<?php echo JText::_('SUBMIT_ANSWERS') ; ?>" id="send" name="send" />
<?php endif ; ?>
</p>
<p>
Powered by JQuarks
</p>
</div>
<?php
$attribs = array('type' => 'text/css');
$document->addHeadLink(JRoute::_("components/com_jquarks/assets/stylesheets/SyntaxHighlighter.css"), "stylesheet", "rel", $attribs) ;
?>
Here's an example of javascript on an html drop down:
<select onchange="window.location = this.value">
<option value="#">Select a page</option>
<option value="/foo.html">Foo Bar</option>
<option value="/foo2.html">Foo Bar2</option>
<option value="/foo3.html">Foo Bar3</option>
<option value="/foo4.html">Foo Bar4</option>
</select>
I haven't tested this in all browsers but it works in chrome. Usually I use jQuery so the would be:
<select onchange="window.location = $(this).val()">
I'll leave the converting this to PHP as your homework assignment ;) Cheers

How can I adjust the position of a label for a zend form Radio element?

with this piece of code
$feOnline = New Zend_Form_Element_Radio('online');
$feOnline->setValue($article->online)
->addMultiOptions(array(0=>'offline', 1=>'online'))
->setLabel('Online');
this html is generated
<dd id="online-element">
<label for="online-0">
<input type="radio" checked="checked" value="0" id="online-0" name="online">offline
</label><br>
<label for="online-1"><input type="radio" value="1" id="online-1" name="online">online
</label>
</dd>
However I don't want the input-tag inside the label-tag. No need for the "" either...
What decorators must I add to get this output?
<dd id="online-element">
<input type="radio" checked="checked" value="0" id="online-0" name="online"><label for="online-0">offline</label>
<input type="radio" value="1" id="online-1" name="online"><label for="online-1">online</label>
</dd>
If you are using default Zend_View_Helper_FormRadio you can't change the way radio is rendered.
The code is as follows (line 159)
// Wrap the radios in labels
$radio = '<label'
. $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
. (('prepend' == $labelPlacement) ? $opt_label : '')
. '<input type="' . $this->_inputType . '"'
. ' name="' . $name . '"'
. ' id="' . $optId . '"'
. ' value="' . $this->view->escape($opt_value) . '"'
. $checked
. $disabled
. $this->_htmlAttribs($attribs)
. $endTag
. (('append' == $labelPlacement) ? $opt_label : '')
. '</label>';
No configuration is in place to change the logic.
Think of the reason why you REALLY need to change the way it is rendered, try using CSS to style the output for example.
If you conclude, you need to change rendering, create your own view helper and use it instead of the default one.

Categories