Wordpress widget form function runs twice - php

I'm having a problem with my custom Wordpress Widget I'm making. For some weird reason the form function gets ran twice, and thus, makes two Redactor textareas, but it only happends when I add the widget to a sidebar. Not when I click the save button after it has been added.
How can I do so my form function only gets ran once, or at least make the JavaScript run once.
Full widget code
http://pastebin.com/BHDJSxWX
My form function
function form($instance)
{
$instance = wp_parse_args((array) $instance, array( 'box1' => '', 'box2' => '', 'box3' => '', 'box4' => '' ));
$box1 = $instance['box1'];
$box2 = $instance['box2'];
$box3 = $instance['box3'];
$box4 = $instance['box4'];
$boxqty = $instance['boxqty'];
$uniqueID = rand();
echo "Unique ID: ".$uniqueID;
?>
<p>
<label for="<?php echo $this->get_field_id('boxqty'); ?>">Number of blocks: (1-4)
<input class="widefat" id="<?php echo $this->get_field_id('boxqty'); ?>" name="<?php echo $this->get_field_name('boxqty'); ?>" type="text" value="<?php echo attribute_escape($boxqty); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('box1'); ?>">Box 1:
<textarea class="widefat redactor_content_<?php echo $uniqueID; ?>" id="box1" name="<?php echo $this->get_field_name('box1'); ?>" style="width: 600px;"><?php echo $box1; ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('box2'); ?>">Box 2:
<textarea class="widefat redactor_content_<?php echo $uniqueID; ?>" id="<?php echo $this->get_field_id('box2'); ?>" name="<?php echo $this->get_field_name('box2'); ?>" style="width: 600px;"><?php echo $box2; ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('box3'); ?>">Box 3:
<textarea class="widefat redactor_content_<?php echo $uniqueID; ?>" id="<?php echo $this->get_field_id('box3'); ?>" name="<?php echo $this->get_field_name('box3'); ?>" style="width: 600px;"><?php echo $box3; ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('box4'); ?>">Box 4:
<textarea class="widefat redactor_content_<?php echo $uniqueID; ?>" id="<?php echo $this->get_field_id('box4'); ?>" name="<?php echo $this->get_field_name('box4'); ?>" style="width: 600px;"><?php echo $box4; ?></textarea>
</label>
</p>
<!--Activate Redactor-->
<script type="text/javascript">
jQuery('.redactor_content_<?php echo $uniqueID; ?>').redactor({
fixed: true
});
console.log('Unique ID: <?php echo $uniqueID; ?>');
</script>
<?php
}

I tested some things and what seems to be happening is that the whole widget loads both on the left, where the widget form content is hidden, and on the right in the 'active' widgets sections. Untested, but you should be able to target the 'active' section only with:
jQuery('.widgets-holder-wrap .redactor_content_<?php echo $uniqueID; ?>').redactor({
fixed: true
});
If I had the complete widget I might be able to spot something else.

Related

get value of radio button using jQuery and php

I want to get the value clicked in radio button.
This is my code:
<ul class="collapsible popout" data-collapsible="accordion" id="clicks">
<?php
foreach ($preguntas['preguntas'] as $row)
{
$opciones = $pregunta->opciones($row[0]);
?>
<li>
<div class="collapsible-header"><i class="material-icons">question_answer</i><?php echo utf8_encode($row[2]); ?></div>
<div class="collapsible-body">
<?php foreach ($opciones as $opcion){ ?>
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
<?php } ?>
</div>
<?php } ?>
</li>
</ul>
I need to get the value of this input id="opcion_..."
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
The problem is name and id is changing, it's not the same
Any idea?
Thank you.
JQuery selector for part of attribute $("[attribute^='value']") like this:
$("[id^='opcion_']").click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label><input name="pregunta_xxx" type="radio" id="opcion_123" value="123">radio for 123</label>
<label><input name="pregunta_xxx" type="radio" id="opcion_456" value="456">radio for 456</label>

codeigniter - get the value of checked checkboxes in the form in other way

Is there other way how to get the value of all checked checkboxes in my form and display it on the other view?
here's my input view
<div class="col-md-6">
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
</div>
my model
public function view_sen($sen_id){
$sen_id[] = $this->input->post('sen_id');
foreach ($sen_id as $id) {
$this->db->select('*')->from('party_candidates')->where('id', $id);
$query[] = $this->db->get()->result_array();
}
return $query;
}
my controller
public function balot_form(){
$sen_id[] = $this->input->post('sen_id');
$view_sen_votes = $this->vote->view_sen($sen_id);
$data = array("view_sen_votes" => $view_sen_votes);
$this->load->view("admin_dashboard/votation_page/balot_form_page", $data);
}
display results view
<div class="col-md-6">
<?php foreach($view_sen_votes as $sen => $id) { ?>
<div class="row">
<div class="form-group">
<label>Senators</label>
<input type="text" class="form-control" value="<?php echo $sen[$id]; ?>" name="sen_id" />
</div>
</div>
<?php } ?>
</div>
how to get the value and display it on my result view? thanks, regards..
Try like below to check key value.
print_r( $view_sen_votes );

Show Custom Customer Fields in Checkout

I've succesfully managed to add custom fields to the customer. However I need these fields to show up in onepage checkout.
I've overridden Mage_Customer_Block_Widget_Name and created my own customer/widget/name.phtml, added the attributes in the sql/xxx_setup/installer-x.y.z.php (added them to adminhtml_customer, customer_account_edit, checkout_register and customer_account_create) and they work fine in the admin site, however they just wont work on the checkout form. The field shows up, but it has the wrong value and no label.
I'm clueless why does it work in the customer registration form but doesn't in the checkout.
The installer code to add the attribute is:
$attributes = array(
'lastname2' => array(
'frontend_label'=>'Apellido Materno',
'label' => 'Apellido Materno',
'input' => 'text',
'type' => 'varchar',
//System = False and visible true = Show in 'customer_account_create', 'customer_account_edit', 'checkout_register'
'system'=>true,
'visible'=>true, //Watch out!! Only visible fields get processed by the form controllers!!!
'user_defined'=>false,
'used_in_forms' => array('adminhtml_customer', 'customer_account_edit', 'checkout_register','customer_account_create'),
'required' => 0,
'position' =>69
));
foreach($attributes as $attribute_code=>$definition)
{
$installer->addAttribute('customer', $attribute_code, $definition);
/**
* #var Mage_Eav_Model_Config
*/
Mage::getSingleton('eav/config')
->getAttribute('customer', $attribute_code)
->setData('used_in_forms',$definition['used_in_forms'])
->save();
}
The code in name.phtml is
<div class="<?php echo $this->getContainerClassName()?>">
<?php if ($this->showPrefix()): ?>
<div class="field name-prefix">
<label for="<?php echo $this->getFieldId('prefix')?>"<?php if ($this->isPrefixRequired()) echo ' class="required"' ?>><?php if ($this->isPrefixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('prefix') ?></label>
<div class="input-box">
<?php if ($this->getPrefixOptions() === false): ?>
<input type="text" id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getPrefix()) ?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?> />
<?php else: ?>
<select id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?>>
<?php foreach ($this->getPrefixOptions() as $_option): ?>
<option value="<?php echo $_option?>"<?php if ($this->getObject()->getPrefix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<div class="field name-firstname">
<label for="<?php echo $this->getFieldId('firstname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('firstname') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" title="<?php echo $this->getStoreLabel('firstname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php if ($this->showMiddlename()): ?>
<?php $isMiddlenameRequired = $this->isMiddlenameRequired(); ?>
<div class="field name-middlename">
<label for="<?php echo $this->getFieldId('middlename')?>"<?php echo $isMiddlenameRequired ? ' class="required"' : '' ?>><?php echo $isMiddlenameRequired ? '<em>*</em>' : '' ?><?php echo $this->getStoreLabel('middlename') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('middlename')?>" name="<?php echo $this->getFieldName('middlename')?>" value="<?php echo $this->escapeHtml($this->getObject()->getMiddlename()) ?>" title="<?php echo $this->getStoreLabel('middlename') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('middlename') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php endif; ?>
<div class="field name-lastname">
<label for="<?php echo $this->getFieldId('lastname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('lastname') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('lastname')?>" name="<?php echo $this->getFieldName('lastname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname()) ?>" title="<?php echo $this->getStoreLabel('lastname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<div class="field name-lastname">
<label for="<?php echo $this->getFieldId('lastname2')?>"><?php echo $this->getStoreLabel('lastname2') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('lastname2')?>" name="<?php echo $this->getFieldName('lastname2')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname2()) ?>" title="<?php echo $this->getStoreLabel('lastname2') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname2') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php if ($this->showSuffix()): ?>
<div class="field name-suffix">
<label for="<?php echo $this->getFieldId('suffix')?>"<?php if ($this->isSuffixRequired()) echo ' class="required"' ?>><?php if ($this->isSuffixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('suffix') ?></label>
<div class="input-box">
<?php if ($this->getSuffixOptions() === false): ?>
<input type="text" id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getSuffix()) ?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?> />
<?php else: ?>
<select id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?>>
<?php foreach ($this->getSuffixOptions() as $_option): ?>
<option value="<?php echo $_option?>"<?php if ($this->getObject()->getSuffix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
This fields in the checkout you are talking about are address attributes, not customer attributes. So you need to load them differently. For registered users you could use Mage::getSingleton('customer/session')->getCustomer()->getLastname2() but it won't be saved to your address, as there is no attribute yet.
Depending on where you want lastname2 to be accessible you can create corresponding attributes for the entities customer_address,quote_address and order_address. They are created the same way you did for customer with
$installer->addAttribute($entityName, $attribute_code, $definition);
But that's not all. For the attributes to be converted correctly you need to set up conversion rules in your module config.xml. See for example the configuration of Mage_Sales.
In the global node there is a node fieldsets with the corresponding rules. There is a node customer_address to convert address attributes to quote address attributes. In sales_convert_quote there are rules to convert this attributes to order attributes.
So for your attributes to be accessible in all you config should look like this:
<global>
<fieldsets>
<customer_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastname2>
</customer_address>
<sales_copy_order_billing_address>
<lastname2>
<to_order>*</to_order>
</lastname2>
<sales_copy_order_billing_address>
<sales_copy_order_shipping_address>
<lastname2>
<to_order>*</to_order>
</lastname2>
</sales_copy_order_shipping_address>
<sales_convert_quote_address>
<lastname2>
<to_order_address>*</to_order_address>
<to_customer_address>*</to_customer_address>
</lastname2>
</sales_convert_quote_address>
<sales_convert_order_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastnam2e>
<sales_convert_order_address>
<customer_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastname2>
</customer_address>
</fieldsets>
</global>

Post request is not sending all variables

I have a form which consists of rows set up like:
<div class="row unit" onmouseover="this.style.background = '#eeeeee';" onmouseout="this.style.background = 'white';" onclick="if(event.srcElement.nodeName != 'INPUT' && event.srcElement.nodeName != 'SELECT' && event.srcElement.nodeName != 'TEXTAREA'){goToByScroll(event.srcElement,-160);}">
<div class="col">
<div class="select">
<input type="checkbox" id="select<?php echo $i; ?>" name="select<?php echo $i; ?>">
</div>
</div>
<div class="col name">
<p><input class="searchable" type="text" id="name<?php echo $i; ?>" name="name<?php echo $i; ?>" value="<?php echo $name; ?>"></p>
<p>Badge ID: <input class="searchable" type="text" id="badge<?php echo $i; ?>" name="badge<?php echo $i; ?>" value="<?php echo $badge; ?>" style="width: 50px;"></p>
</div>
<div class="col phone">
<p>Work: <input class="searchable" type="text" id="phone<?php echo $i; ?>" name="phone<?php echo $i; ?>" value="<?php echo $phone; ?>"></p>
<p>Cell: <input class="searchable" type="text" id="cell<?php echo $i; ?>" name="cell<?php echo $i; ?>" value="<?php echo $cell; ?>"></p>
<p>Home: <input class="searchable" type="text" id="home<?php echo $i; ?>" name="home<?php echo $i; ?>" value="<?php echo $home; ?>"></p>
</div>
<div class="col email">
<p>Work: <input class="searchable" type="text" id="email<?php echo $i; ?>" name="email<?php echo $i; ?>" value="<?php echo $email; ?>"></p>
<p>Personal: <input class="searchable" type="text" id="perEmail<?php echo $i; ?>" name="perEmail<?php echo $i; ?>" value="<?php echo $perEmail; ?>"></p>
</div>
<div class="col file">
<p class="removeFile"><input type="text" id="filename<?php echo $i; ?>" name="filename<?php echo $i; ?>" class="file" value="<?php echo $filename; ?>" readonly>
Remove: <input type="checkbox" id="removeFile<?php echo $i; ?>" name="removeFile<?php echo $i; ?>"></p>
<input type="file" id="file<?php echo $i; ?>" name="file<?php echo $i; ?>" onchange="myForm.elements['filename<?php echo $i; ?>'].value=myForm.elements['file<?php echo $i; ?>'].value;">
</div>
</div>
These rows get repeated using a javascript function which increments the name:
function addTableRow(table){
for(i=0; i<myForm.elements['entriesNum'].value; i++){
var $tr = $(table).find(".row:last").clone();
$tr.find("input,select,textarea").attr("name", function(){
var parts = this.id.match(/(\D+)(\d+)$/);
return parts[1] + ++parts[2];
}).attr("id", function(){
var parts = this.id.match(/(\D+)(\d+)$/);
return parts[1] + ++parts[2];
});
$(table).find(".row:last").after($tr);
$tr.find('.fileElements').remove();
$tr.find('input[type!="radio"], textarea').removeAttr("value");
$tr.find('input').removeAttr("checked");
$tr.find('select option:first-child').attr("selected", true);
$tr.find('input[type!="radio"]').removeAttr("disabled");
$tr.find('input[type="radio"]').attr("disabled", "disabled");
$tr.find('.error').hide();
}
}
This works perfectly until the number of rows gets higher than 111. At this point when I submit no more data gets included in the array no matter how many rows I add. I was able to deduce this by using print_r($_REQUEST);. I have edited my php.ini and set all the maxes to be absurdly high with still no change.
Whenever I've had issues with javascript created form elements not showing up in the POST, it's always had to do with the way I've structured the FORM tags.
Things to look for...
Is your FORM tag properly closed off?
Is your FORM start and close at the same level in the DOM? For example, you wouldn't want to do this...
<form method='post'>
<div>
<!--lots of stuff-->
</form>
</div>
You would instead want this...
<form method='post'>
<div>
<!--lots of stuff-->
</div>
</form>
Do you have another FORM tag that's opened and not closed before the one you're actually working with? Like this...
<form method='get'>
<!--some kind of form about something else-->
<form method='post'>
<!--the data you're currently focused on-->
</form>

isset multiple check boxes in php from mysql database

i am populating a form from mysql. code is. .
task.php
<div data-role="content">
<h2> Please select cars </h2>
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="car" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php }
?>
<input type="submit" name="submitcars" id="submitcars" value="View Details"/>
</form>
</div>
now in cars.php i want to make a query to display the details of car selected,
<div data-role="content">
<?php
if(isset($_POST['submitcars'])){
echo $_POST[$cname];?????????????
}
?>
</div>
now how to process the form here in cars.php ?
thanks
Make car attribute array::
<input type="checkbox" name="car[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
and get them on next page with
if(isset($_POST['submitcars'])){
foreach($_POST['car'] as $car){
// do something with $car
}
}
Try this
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="car[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php }
?>
Firstly, let me preface this by saying that you shouldn't be using the mysql functions. They're depreciated and no longer recommended.
On task.php, your opening form tag was being printed inside your loop. Secondly, you should probably use an array of checkboxes using [].
<div data-role="content">
<h2> Please select cars </h2>
<form method="post" action="cars.php">
<?php
$carq = "select * from cars";
$executecars = mysql_query($carq);
while($row=mysql_fetch_assoc($executecars)){
$cname = $row['name'];
?>
<label for="<?php echo $cname; ?>"><?php echo $cname; ?></label>
<input type="checkbox" name="cars[]" id="<?php echo $cname; ?>" value="<?php echo $cname; ?>"/>
<?php } ?>
<input type="submit" name="submitcars" id="submitcars" value="View Details"/>
</form>
</div>
Then, on cars.php:
<div data-role="content">
<?php
if(isset($_POST['submitcars'])){
if(isset($_POST['cars'] && count($_POST['cars']) > 0){
foreach($_POST['cars'] as $key => $car){
echo $car . '<br /'>;
}
}
else{
echo 'No cars found!';
}
}
?>
</div>

Categories