Getting undefined is not a function - coffeescript

Using coffeescript to call out to Stripe and getting undefined is not a function on this line: $.StripeJS.stripeResponseHandler($form)
I have also tried:
#.stripeResponseHandler($form)
-and-
StripeJS.stripeResponseHandler($form)
Code:
class StripeJS
constructor: (formId) ->
#.setStripeResponseHandlerForForm(formId)
stripeResponseHandler = (status, response, $form) ->
console.log 'In stripeResponseHandler'
if (response.error)
#Clear the errors on the form
$form.find('.payment-errors').html ''
#Show the errors on the form
$form.find('.payment-errors').append ('<div class="alert alert-error">
<a class="close" data-dismiss="alert">×</a>
<li>' + response.error.message + '</li>
</div>')
$form.find('button').prop 'disabled', false
else
# response contains id and card, which contains additional card details
# Insert the token into the form so it gets submitted to the server
$('#stripe_card_token').val response.id
# and submit
$form.get(0).submit()
setStripeResponseHandlerForForm: (formId) ->
Stripe.setPublishableKey $('meta[name="stripe-key"]').attr('content')
$("#{formId}").submit (event) ->
console.log 'In setStripeResponseHandlerForForm'
$form = $(this)
# Disable the submit button to prevent repeated clicks
$form.find('button').prop 'disabled', true
console.log 'About to call out to Stripe'
Stripe.createToken $form, (status,response) ->
$.StripeJS.stripeResponseHandler($form)
console.log 'Back from calling stripe'
# Prevent the form from submitting with the default action
return false
$(document).ready ->
$.StripeJS = new StripeJS('#new_agent')

Related

CodeIgniter - save method and redirects

I have a page to manage courses, built with CI 3. When the user hits the save button, the form is submitted to the save method. My problem is how do I redirect the user to the previous page with the form populated with the data inserted?
At the moment what's happening is that after submit, the save method loads the views of the /new page, and the URL stays as /save with the data populated.
Already tried the redirect, but it loses the data the user had inserted.
Here a snippet of my save method:
public function save()
{
$this->checkIsAdmin();
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', $this->lang->line('common_name'), 'required');
if ($this->form_validation->run() === FALSE)
{
$this->feedback_message->create('Please check fields', 'error');
$this->baseInfo['page_title'] = "Registar novo curso";
$this->mybreadcrumb->add('Registar curso', base_url('course/new'));
$data['schools'] = $this->School_model->listSchools();
$this->load->view('web/top', $this->baseInfo);
$this->load->view($this->templatesFolder.'tabs_start', array('courseId' => 0));
$this->load->view($this->templatesFolder.'manage', $data);
$this->load->view($this->templatesFolder.'tabs_end');
$this->load->view('web/footer');
/*redirect("/course/new", "location");*/
}else{
$saved = $this->Course_model->saveCourse();
if($saved){
$this->feedback_message->create('Success', 'success');
}else{
$this->feedback_message->create('Error '.$_POST['name'], 'error');
}
redirect('course/');
}
}
To re-populate the form you have to use the set_value('field_name') function in your input field like below:
<input type="text" name="name" value="<?php echo set_value('name'); ?>" />
Return the previous view as:
$this->load->view('view_name',$All_Data_For_View);
Still If you aren't understand then read the CI form validation again.

Wordpress plugin: Working with REST api

Before I start, I want to tell you guys that I have no experience in anything Wordpress related. I do have worked in PHP and Codeigniter before.
User Case
The user enters a preferred feature in a form (Air conditioning, etc.).
When the user submits the form, the feature will be send to a REST API.
The results of the REST API ( a list of cars with AC ) will be shown on the page.
It should roughly look something like this.
What i have so far
An empty plugin that is shown in the Wordpress admin panel.
Question(s)
How do create the front-end for this plugin?
How and where do I create the form action?
How do I access the form action?
What I have/know so far
I know there are some action hooks that will place your content in the header and footer by creating something like:
<php add_action('wp_footer', 'mp_footer'); ?>
In your empty plugins php file place this:
function get_search_results(){
$args = array( 's' => $_GET['term'] );
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>'.get_the_title().'</li>';
}
} else {
echo "Nothing Found";
}
die();
}
add_action( 'wp_ajax_get_search', 'get_search_results' );
add_action( 'wp_ajax_nopriv_get_search', 'get_search_results' );
function get_searchbox( $atts ){
ob_start(); ?>
<form id="searchform">
<input type="text" id="searchbox">
<button type="submit">Search</button>
</form>
<ul id="search-result"></ul>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode( 'searchbox', 'get_searchbox' );
function add_search_script() {
wp_enqueue_script( 'search', plugins_url( '/search.js' , __FILE__ ), array( 'jquery' ) );
wp_localize_script( 'search', 'search_ajax', array( 'url'=>admin_url( 'admin-ajax.php' ), 'action'=>'get_search' ) );
}
add_action( 'wp_enqueue_scripts', 'add_search_script' );
In your plugin's directory create a new javascript file - search.js and place this:
jQuery(function($){
$('#searchform').submit(function(e){
e.preventDefault();
$.ajax({
url: search_ajax.url,
data: { 'action': search_ajax.action, 'term': $('#searchbox').val() }
}).done(function(r) {
$('#search-result').html(r);
});
});
});
Now you can use shortcode [searchbox] in your wordpress page to get your searchbox.
In php you can get same result with <? echo do_shortcode('[searchbox]') ?>
Explanation:
When we add [searchbox] shortcode, it is processed and replaced with a form via get_searchbox() function.
In jQuery code On form submit we are sending an action : get_search (defined in wp_localize_script).
Server receives that request via wp_ajax_get_search and processes get_search_results() and returns an output.
In jQuery done(function(r){ r is the response from server. Use that data to manipulate your html.
action is the essential part of REST api in wordpress. We need not have a url. Instead we define an action and for that action return a response.
Once you understand this, modify the action and response according to your need.
Helpful articles: Daniel's, wpmudev

Keep select value on change with laravel

I am having a paginated backend table with db-data. The admin person can filter that table for data status. This happens via ajax. Everything works fine but I do not get the selected filter value to remain selected when I click on the second pagination link.
E.g. I choose select option: '1' => 'Active' so that only db-rows show up that have a status of 1. But when I then click on the second pagination link to see the next 20 rows then again it also displays the inactive db-rows. How would I get the selected option to remain selected in this situation? I tried Input::old('status') and passing $selected to view as below but no success. Thank you for any hint!
View:
<form id="filter_form" onsubmit="" action="<?php echo URL::action('countries#anyIndex'); ?>">
<?php echo Form::select('filter_status', funcs::get_status_options(), $selected, array('id' => 'filter_status')); ?>
</form>
Ajax:
$(function(){
$("#filter_status").on('change', function(){
frm = $("#filter_form");
frm.serialize();
status = $('#filter_status').val();
$.ajax({
type: "POST",
url: $(frm).attr('action'),
data: {status: status},
success: function(data){
$("#list").html(data.list);
},
dataType: "json"
});
});
});
Controller:
class countries extends BaseController {
public $filter = array(0,1,2);
function anyIndex()
{
$data['title'] = "Countries list";
if(Input::has('status')){
$status = Input::get('status');
if($status != 2){
$this->filter = array($status);
}
}
$d['items'] = $this->_getItems(20);
if(Request::ajax()){
$data['list'] = View::make('admin/countries/countries_list', $d)->withInput($status)->render();
return Response::json($data);
}
$data['selected'] = $this->filter;
$data['list'] = View::make('admin/countries/countries_list', $d);
return View::make('admin/admin_layout')->nest('view', 'admin/countries/countries_view', $data);
}
private function _getItems($paginate)
{
$items = Country::whereIn('status', $this->filter)->paginate($paginate);
return $items;
}
}

Confirmation Dialog in Zend Framework

I'm building a Zend Application using doctrine repository classes to update, delete and insert data to the DB. These repositories are called from controller actions and they do exactly what they supposed to do. However, I'd like to add some confirmation dialogs to the application, so for example, if a user wants to edit or delete an item, a Confirm Edit or Delete dialog must first be opened and the data will be edited or deleted depending on what the user selects. Here's an example of some action code for updating a staff members details after the user has clicked on a zend form submit button.
public function updatestaffAction()
{
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$values = $form->getValues();
$user = $this->entityManager->find('\PTS\Entity\Staff', $values['staff_number']);
$staffValues = array('staff_number' => $values['staff_number'],
'title' => $values['title'],
'first_name' => $values['first_name'],
'last_name' => $values['last_name'],
'telephone' => $values['telephone'],
'cellphone' => $values['cellphone'],
'fax' => $values['fax'],
'email' => $values['email'],
'job_title' => $values['job_title']);
$this->staffRepository->saveStaff($staffValues);
$this->entityManager->flush();
}
}
The staff repository saveStaff method simply creates a new Staff object and persists that object if the staff member doesn't exists, or merges the new data if it's an existing staff member as is the case for the update code above.
So my question is, how can I change the action to only save the data once the user has clicked the yes button in a confirmation dialog. BTW, the dialog can be either a JQuery or Dojo dialog box.
When you create form's submit button, set js code:
$submit = new Zend_Form_Element_Submit('delete');
$submit->setAttrib(
'onclick',
'if (confirm("Are you sure?")) { document.form.submit(); } return false;'
);
Or, if you want to set dialogbox on link (if you don't have submit form):
onclick="if (confirm('Are you sure?')) { document.location = this.href; } return false;"
Code for showDialog:
$(function() {
$( "#dialog:ui-dialog" ).dialog( "destroy" );
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Are you sure": function() {
// PUT your code for OK button, for eg.
document.form.submit();
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});`
Thx, I used the second option like this way :
Delete
and it's worked :)

Zend Framework ajaxLink params not being sent to ctr/action method

I have a simple ajaxLink to a controller. It works without the params, but when I add a param I get an error:
Warning: Missing argument 1 for Ez_ContactsController::testyAction()
The params are required and I see them in the jquery that ZF creates. here is the JS that gets generated (/ez is my module):
$(document).ready(function() {
$('a.ajaxLink1').click(function() { $.post('/ez/contacts/testy', {"param1":"1","param2":"456"}, function(data, textStatus) { $('#testy').html(data); }, 'html');return false; });
});
Here is my ajaxLink:
<div id="testy"></div>
<?= $this->ajaxLink("Example 2",
"/ez/contacts/testy",
array('update' => '#testy',
'class' => 'someLink'),
array('param1' => '1',
'param2' => '456')); ?>
Thanks for any help
I found the answer.
You don't send the vars directly to the function, they are sent via GET or POST.
Here is how I got the vars.
$param1 = (int)$this->_request->getParam('param1');
$param2 = (int)$this->_request->getParam('param2');
Here is the entire function:
public function testyAction(){
//The request was made with JS XmlHttpRequest
$this->_helper->viewRenderer->setNoRender(); //Will not render view
$this->_helper->Layout->disableLayout(); // Will not load the layout
$param1 = (int)$this->_request->getParam('param1');
$param2 = (int)$this->_request->getParam('param2');
echo 'testy 123. params sent were: '.$param1.', '.$param2;
}