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
Related
I am trying to build a form where the user selects an item based upon an array of values from a database table. Here is the code that I am using to filter out the list:
$arrlength=count($results);
$listSubscriptions = '<form>';
for($x=0; $x<$arrlength; $x++)
{
$itemList = $results[$x]->item_name;
$listSubscriptions .= '<input type="radio" name="'.$itemList.'" value="'.$itemList.'"> '.$itemList.'<br>';
}
$listSubscriptions .= '</form>';
return $listSubscriptions;
When I print the results of the form I am able to get the radio buttons to display as I would like them to, but the problem is that the selection is not being limited to one radio button selection. Any ideas why?
The radio buttons must all share a single name attribute to belong to the same group:
$listSubscriptions .= '<input type="radio" name="subscription" value="' . $itemList . '"> ' . $itemList . '<br>';
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()
I need to run this sql query, which give me a list of Id and Dates
I want to click each result and take with me the Id value to the next form
I wrote this query above but i see in the debager that the hidden ID get his value but not pass to the next form
I think i have a problem with the submit() .
where should I put him ?
function ShowAllCarts($user_email) {
$connB = new ProductDAO();
$connB->Connect();
$pro_query = "SELECT * FROM Cart WHERE `Email`='$user_email';";
$db_result = $connB->ExecSQL($pro_query);
$html_result = '<div data-role="content"> <ul data-role="listview" data-theme="b"> ';
$html_result .= '<form action="PreviouscartProduct.php" method="POST"/>';
while($row_array = $db_result->fetch_array(MYSQLI_ASSOC))
{
$Id= $row_array['Id'];
$Date= $row_array['Date'];
//$html_result //
$html_result .="<li><a href='PreviouscartProduct.php'>Cart number: $Id from Date: $Date><input type='hidden' name='Id' value'<?=$Id?>'</input></a></li>'";
$html_result .= '<a onclick="this.form.submit();" </a>;
}
$html_result .= '</form>';
$html_result .= ' </ul> </div>';
$connB->Disconnect();
return $html_result;
}
//display all carts
$func_result = ShowAllCarts($Email);
You need to use a checkbox element:
$html_result .="<li>"
."<checkbox name='cartItem[$Id]' value='$Date'>"
. "Cart number: $Id from Date: $Date"
. "</li>'"
;
Then, in PreviouscartProduct.php, you'd itera over cartItem:
$cartItems = $_POST[ 'cartItem' ];
foreach( $cartItems as $id => $date ) {
... do something ...
}
In case you'd like to take exactly one item, why not use this:
$html_result .="<li>"
. "<a href='PreviouscartProduct.php?cartID=$Id&date=$Date'>"
. "Cart number: $Id from Date: $Date"
. "</a>"
. "</li>'"
;
There's a bunch of HTML syntax errors in this, check the output in a validator
For a start, your opening a tag doesn't close after this.form.submit();, it should read
$html_result .= '<a onclick="this.form.submit();">Anchor Text Here</a>';
Edit :
the anchor element needs to reference the form. Give the form element a name attribute and use something like
onclick="document.nameattributehere.submit();return false"
on the link.
End edit.
Also, in the line above, you're already using the PHP parser when you get to the value attribute of your input, so there is no need for the
<?= and ?>
Finally, in the same tag, you don't need a closing input tag
</input>
Just close the opening tag with
/>
That's just glancing, run the validator for other errors and I'm sure the problem will be clearer.
I have the following dropdown box
<form name="myform" method="POST" ACTION="songs.php">
Select Category: <select id="sel" name="categories" onchange="document.myform.submit()">
And all the options follow after. When the user makes a selection, the category is brought up below using PHP and MYSQl based on the selection containing a list of songs etc.
However, the dropdown box always defaults back to the first value in the list of options. How do I make so that the dropdown box will set the selected option to the last submitted value? Thanks in advance!
You can't do that with JS as it has no direct access to POST request parameters. Rather let the server side language (which is in your case PHP) print the selected attribute on the <option> element whenever the submitted value matches the option value.
E.g.
foreach ($options as $value => $label) {
echo '<option value="' . $value . '"' . ($selected == $value ? ' selected' : '') . '>' . $label . '</option>';
}
On my website, user's are allowed to filter games by Genre. When a user chooses a genre from the select box and hits submit, the page uses GET to see what the filter was. Now the filter works fine, the problem is that the select box's selection goes to the default one (Which says "All".)
I want it so that after a user submits their filter request, the select box will keep that selection after the page reloads.
There's only one way I could think of to do this but it would require adding in PHP into every option there is. Are there any simpler ways to go about doing this with PHP or jQuery?
You don't want to do this with jQuery. There it is not for.
Just have the options in an array in PHP and loop over it. If the looped value matches the currently selected option, then just add the selected attribute.
E.g.
foreach ($options as $value => $label) {
echo '<option value="' . $value . '" ' . ($selected == $value ? ' selected' : '') . '>' . $label . '</option>';
}
Assuming you're doing a full form submit the selected option is only going to be available to the server-side code, once it gets back to the client to use jQuery you won't have that (unless you try to use cookies before the form submit, but bleh).
I'd use PHP in the option tag and echo selected="selected" if the option matches up with the selected option.
If you want to avoid a lot of duplicated code why not do something like this:
<select name="test">
<?php
$options = array(1 => 'Option 1', 2 => 'Option 2', 3 => 'Option 3');
foreach ($options as $key => $value) {
echo '<option value="' . $key . '"' . ($key == $_GET["test"] ? ' selected="selected"' : '') . '>' . $value . '</option>';
} ?>
</select>
you can do it without a loop just change the 6 to whatever value you want when you render the page
$("#$optionId[value=6]").attr('selected','selected');