Php toggle with a form: logic error? - php

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>
';

Related

Add class to label with class if radio is checked

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.

checkbox array not working

<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$counter'>
for some reason, when I run the form with this name for this input, it won't run the PHP script, it won't even run the beginning of the script.
Is there some reason this isn't working? am I doing it wrong? I thought this is the actual way this would work.
[Edit]
this is the actual full script in the form:
foreach($producten as $row)
{
echo("
<label class='mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect' for='checkbox-" . $counter ."'>
<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$counter'>
<span class='mdl-checkbox__label'>" . $row['productcode'] . ' ' . $row['categorie'] . ' ' . $row['merk'] . ' ' . $row['type'] . ' ' . $row['cpu'] . ' ' . $row['ram'] . ' ' . $row['os'] . ' ' . $row['hdd'] . ' ' ."</span>
</label>
");
$counter++;
}
.
I have checked with below code as related your code. It's working with Checkbox array. I hope it's will help you. All the best.
<form action="#" method="POST">
<?php
$producten = array('1a','2b','3c','4d');
$counter = 1;
foreach($producten as $row)
{
echo("
<input type='checkbox' id='checkbox-" . $counter ."' class='mdl-checkbox__input' name='product[]' value='$row'> <label class='mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect' for='checkbox-" . $counter ."'> $row </label><br>
");
$counter++;
}
?>
<input type="submit" name="submit" value="Submit">
</form>
<?php echo "<pre>"; print_r($_REQUEST); ?>
Output:
Array
(
[product] => Array
(
[0] => 1a
1 => 2b
[2] => 3c
[3] => 4d
)
)

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

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