i have this function in index.php view file.(see it below)
as you see after click button it get data with get method
if my url be http://example.com/admin/site-developer/index
after click button in console i get error
GET http://example.com/admin/site-developer/site-developer/get-note?developer_id=7 404 (Not Found)
but with url without index at end it work correctly
http://example.com/admin/site-developer
if change $.(get) url to
$.get("get-note",{developer_id: Id},function(data) {}
after click http://example.com/admin/site-developer
give me
GET http://example.com/admin/get-note?developer_id=7 404 (Not Found)
error but if i add index to end of url it works corrctly
this is complete function code:
$('#noteBtn').on('click',function(e) {
$.get("site-developer/get-note",{developer_id: Id},function(data) {
var data = JSON.parse(data);
var elm = "";
$.each( data, function( i, val ) {
elm = elm+'<div id="id-'+i+'"><div>'+val.note+'</div><div>'+val.created_at+'</div></div>';
});
$("#developer_note").html(elm);
})
;})
i think maybe its url manager problem??
here is my url manager config
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'/'=>'site/index',
'login' => 'site/login',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
],
],
action getnote
public function actionGetNote($developer_id)
{
$model = DevelopersNote::findAll(['developer_id' => $developer_id]);
echo Json::encode($model);
}
Seems that you url is not build properly
Assuming you javascript code is inside a php file
try use UrlHelper
<?php
use yii\helpers\Url;
?>
$('#noteBtn').on('click',function(e) {
<?php echo 'var myUrl = "' . Url::to(['site-developer/get-note']) . '";
' ; ?>
$.get(myUrl ,{developer_id: Id},function(data) {
var data = JSON.parse(data);
var elm = "";
$.each( data, function( i, val ) {
elm = elm+'<div id="id-'+i+'"><div>'+val.note+'</div><div>'+val.created_at+'</div></div>';
});
$("#developer_note").html(elm);
})
;})
for check your problem with the code above try use this way
$('#noteBtn').on('click',function(e) {
var myUrl "http://example.com/admin/site-developer/get-note";
$.get(myUrl ,{developer_id: Id},function(data) {
var data = JSON.parse(data);
var elm = "";
$.each( data, function( i, val ) {
elm = elm+'<div id="id-'+i+'"><div>'+val.note+'</div><div>'+val.created_at+'</div></div>';
});
$("#developer_note").html(elm);
})
;})
Related
I am trying to query the WP Database but I am receiving an error of Call to a member function get_results() on null. I believe I need to somehow register wpdb but despite reading through multiple similar questions I can't piece together what needs to be done. Any help is greatly appreciated as I am new and learning Wordpress and Ajax.
My JS file is called zip-search-popup.js
(function($) {
$(document).ready(function() {
$('.zip-bar-button').click(function(event) {
event.preventDefault();
submittedZip = $("#zipcode-bar-input").val();
$.ajax({
//need an automatic URL so that this doesn't need to be updated
url: from_php.ajax_url,
type: "GET",
data: {
action : 'zip_search',
submittedZip : submittedZip,
},
success: function (response) {
console.log(response);
alert("working");
}
})
$('.zip-search-popup-con').fadeToggle(350);
})
$('.zip-search-dismiss').click(function() {
$('.zip-search-popup-con').toggle();
})
})
})(jQuery);
I have registered my scripts in functions.php and have my SQL query function within here as well.
add_action('wp_enqueue_scripts', 'hyix_enqueue_custom_js');
function hyix_enqueue_custom_js() {
//enqueue zip-search-popup.js
wp_enqueue_script('zip-search-popup', get_stylesheet_directory_uri().'/js/zip-search-popup.js', array('jquery'), false, true);
wp_localize_script('zip-search-popup', 'from_php', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
//hook zip-search-popup function into ajax
add_action( 'wp_ajax_zip_search', 'ajax_zip_search' );
//same hook for users not logged in
add_action( 'wp_ajax_nopriv_zip_search', 'ajax_zip_search' );
//query for pulling in shipping data
function ajax_zip_search() {
$submitted_zip = $_REQUEST['submittedZip'];
global $wpdb;
// The SQL query
$response = $wpdb-> get_results("SELECT {$wpdb->prefix}woocommerce_shipping_zones.zone_name ".
"FROM {$wpdb->prefix}woocommerce_shipping_zone_locations ".
"INNER JOIN {$wpdb->prefix}woocommerce_shipping_zones ".
"ON {$wpdb->prefix}woocommerce_shipping_zone_locations.zone_id = {$wpdb->prefix}woocommerce_shipping_zones.zone_id ".
"WHERE location_code = '$submittedZip' ");
$response = array(
'request' => $_REQUEST,
'zip' => $submitted_zip,
'test' => 'is ok',
);
wp_send_json( $response );
// echo $response;
die();
}
You should use the Wordpress-Way to use AJAX
All Ajax-Request will be recieved by wp-admin/admin-ajax.php which fires the Ajax-Hooks which run your PHP-Code. The Ajax-Hook depends from the action-parameter in your Ajax-Request.
With wp_localize_script() you can provide some data from PHP to Javascript, i.e. the ajax_url.
Place this code in your functions.php. This include the code that handles your Ajax-Request.
add_action('wp_enqueue_scripts', 'hyix_enqueue_custom_js');
function hyix_enqueue_custom_js() {
//enqueue zip-search-popup.js
wp_enqueue_script('zip-search-popup', get_stylesheet_directory_uri().'/js/zip-search-popup.js', array('jquery'), false, true);
wp_localize_script('zip-search-popup', 'from_php', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_ajax_zip_search', 'ajax_zip_search' );
add_action( 'wp_ajax_nopriv_zip_search', 'ajax_zip_search' );
function ajax_zip_search() {
$submitted_zip = $_REQUEST['submittedZip'];
// do some zip search
$response = array(
'request' => $_REQUEST,
'zip' => $submitted_zip,
'test' => 'is ok',
'foo' => 'bar'
);
wp_send_json( $response );
}
Now you can use the ajax_url provided by wp_localize_script(). Don't forget to send an action-parameter:
$.ajax({
url: from_php.ajax_url,
type: "GET",
data: {
action: 'zip_search',
submittedZip: submittedZip
},
success: function (response) {
console.log(response);
alert('it works');
}
});
Before for submitting I want to send the request to an action. But in return I get 404 not found. The action is obviously there. Also got it in the filters of the controller.
JS:
$('#home-contact').on('beforeSubmit', function(){
$.ajax({
method: 'post',
url: '/site/send-contact',
data: new FormData($(this))[0],
success: function(data){
console.log(data)
}
})
return false
})
Controller filters:
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup', 'send-contact'],
'rules' => [
[
'actions' => ['signup', 'send-contact'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['#'],
],
],
],
And the action also :
public function actionSendContact()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$model = new Contact();
if($model->load(Yii::$app->request->post()) && $model->save()){
return $data['success'] = Yii::t('app', 'Successfully sent message.');
}
return $data['error'] = Yii::t('app', 'Something went wrong. Please try again.');
}
The scenario happens in the frontend if that matters somehow. Thank you!
Not sure about the 404 you are having as the url in the request is correct and the url that would be generated for the ajax request will be like http://example.com/site/send-contact but only if you are using the 'enablePrettyUrl' => true, for the urlManager component, otherwise it should be like index.php?r=site/index that could only be the reason behind the 404, a better way is to get the url from the form action attribute.
Apart from above,
You are using the new FormData() to send the data with the request like
data: new FormData($(this))[0]
which isn't correct and won't send any FormData with the request as it will return undefined, you can check the console or by using the print_r($_POST) inside the action sendContact once you are done with the 404, it should be
data: new FormData($(this)[0]),
you need to get the form via $(this)[0] and pass to the new FormData().
But this is not enough you have to set the contentType and processData to be false to correctly submit the FormData or you will get the exception in console
Uncaught TypeError: Illegal invocation
So your code should be like
$('#contact-form').on('beforeSubmit', function(){
let url=$(this).attr('action');
let form=$(this)[0];
let data=new FormData(form);
$.ajax({
method: 'post',
url: url,
data:data,
contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
processData: false,
success: function(data){
console.log(data)
}
});
return false;
})
EDIT
Note: Above all you should use data:$(form).serialize() simply rather than using new FormData() until unless you are planning to upload files along with the form using ajax.
URL in JavaScript seems to be not fully specified.
The valid way would be:
url: 'index.php?r=site/send-contact',
But this works only if your index.php is in root folder.
To make it work with any situation (e.g., index.php is not in root), then you have different solutions. I personally like to use this:
Declare action and id in your form:
Example:
$form = ActiveForm::begin([
'action' => ['site/send-contact'],
'id' => 'my-form',
]);
Use a link (that is generated by Yii2 itself) in your JS code:
Example:
$('#home-contact').on('beforeSubmit', function() {
$.ajax({
method: 'post',
url: $('#my-form').attr('action'),
data: new FormData($(this))[0],
success: function(data) {
console.log(data)
}
});
return false;
});
This should work in all cases, whenever your files are.
Hope that helps!
did you add 'enableAjaxValidation' => true or add action name in active form?
$form = ActiveForm::begin([
'action' => ['controller/action'],
'enableAjaxValidation' => true
]);
I am new in Yii framework, so I don't have much idea in it.
My question is, I want to get details of the company which is selected in a search box.
Code in my frontend/companies.php
<?= Html::SubmitButton( 'Get info', [ 'class' => 'btn btn-success' , 'id' =>'getinfo']) ?>
Here's my select2 widget code:-
$typeahead = Select2::widget([
'id' => 'front_companies_select',
'name' => 'state_10',
'data' => $companyNameList,
'options' => [
'placeholder' => 'Select ...',
'sigle' => true
],
]);
My js code:-
$("#getinfo").click(function(){
var obj = $("#front_companies_select option:selected").val();
console.log(obj);
});
If you want to get only company name you can try this.
$("#getinfo").click(function(){
var company_id = $("#front_companies_select option:selected").val();
console.log(company_id);
var companyName = $("#front_companies_select option:selected").text();
console.log(companyName); //here you can get company name
});
Or If you want to get whole company details then you have to call ajax like this.
$("#getinfo").click(function(){
var company_id = $("#front_companies_select option:selected").val();
$.ajax({
type: "POST",
url:"YOUR_URL/YOUR_FUNCTION_TO_GET_COMPANY_DETAILS",
data: {company_id:company_id},
dataType:'json',
async: false,
success: function (data)
{
console.log(data); //here you can get all the company details in object
}
});
});
Good day!
How can I add a search on a dropdownlist?
I want a dropdown search like the Select2 widget.
Dropdownlist:
<?= $form->field($modelRis, "[{$i}]lib_item_item_id")->dropDownList(
ArrayHelper::map(LibItem::find()->orderBy('item_description')->all(), 'item_id', 'item_description'),
[
'prompt'=>'Select Item',
'onchange'=>'
var tmp = $(this).attr("id");
var thisId = tmp.split("-");
var tmp2 = "";
var tmp3 = "";
var sample_id = $(this).val();
$.post( "'.Yii::$app->urlManager->createUrl(['online-requisition/listsofunit?item_id=']).'"+$(this).val(),
function( data ) {
$( "#risrequesteditem-"+thisId[1]+"-lib_unit_id").html( data );
$( "#loop-"+thisId[1]+"-lib_item_item_id").val( sample_id );
tmp2 = data;
tmp3 = tmp2.split("=====");
$( "#loop-"+thisId[1]+"-available_stock").val( tmp3[1] );
});
',
'pluginOptions' => [
'allowClear' => true
],
])->label('Item',['class'=>'label-class']); ?>
I can't use the select2 widget because the 'onchange' or this line of code is not supported:
'onchange'=>'
var tmp = $(this).attr("id");
var thisId = tmp.split("-");
var tmp2 = "";
var tmp3 = "";
var sample_id = $(this).val();
$.post( "'.Yii::$app->urlManager->createUrl(['online-requisition/listsofunit?item_id=']).'"+$(this).val(),
function( data ) {
$( "#risrequesteditem-"+thisId[1]+"-lib_unit_id").html( data );
$( "#loop-"+thisId[1]+"-lib_item_item_id").val( sample_id );
tmp2 = data;
tmp3 = tmp2.split("=====");
$( "#loop-"+thisId[1]+"-available_stock").val( tmp3[1] );
});
',
Thanks...
Updates:
If i'm going to used the select2 widget in order to have a search function during the selection of items there will be a problem.
In the :
first selection its working:
And the onchange function has been working also. And automatically fill all the data in form field (Item no, Unit and StockAvailable) after the selection of item.
1st Selection
second selection is not working:
But I can select an item. Only the jquery function onchange is the problem...
2nd Selection
Thanks...
As suggested by Ajith above, you should use the Yii2 Select2 Widget from http://demos.krajee.com/widget-details/select2
If you go through the details you will find that the above widget does allow you to add event handlers by using the pluginEvents parameter settings of the widget.
pluginEvents = [
"change" => "function() { log('change'); }",
"select2:opening" => "function() { log('select2:opening'); }",
"select2:open" => "function() { log('open'); }",
"select2:closing" => "function() { log('close'); }",
"select2:close" => "function() { log('close'); }",
"select2:selecting" => "function() { log('selecting'); }",
"select2:select" => "function() { log('select'); }",
"select2:unselecting" => "function() { log('unselecting'); }",
"select2:unselect" => "function() { log('unselect'); }"
];
Although it's not possible to provide proper code without knowing the context etc of your code snippet, however it can be re-written using Yii2 Select2 widget as below (the code below is untested, but should give you an idea of the structure):
use kartik\widgets\Select2;
$data = ArrayHelper::map(LibItem::find()->orderBy('item_description')->all(), 'item_id', 'item_description');
// Usage with ActiveForm and model
echo $form->field($modelRis, "[{$i}]lib_item_item_id")->widget(Select2::classname(), [
'data' => $data,
'options' => ['placeholder' => 'Select Item'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
'select2:select' => 'function(e) {
var tmp = e.target.id;
var thisId = tmp.split("-");
var tmp2 = "";
var tmp3 = "";
var sample_id = e.target.value;
$.post( "'.Yii::$app->urlManager->createUrl(['online-requisition/listsofunit?item_id=']).'"+$(this).val(),
function( data ) {
$( "#risrequesteditem-"+thisId[1]+"-lib_unit_id").html( data );
$( "#loop-"+thisId[1]+"-lib_item_item_id").val( sample_id );
tmp2 = data;
tmp3 = tmp2.split("=====");
$( "#loop-"+thisId[1]+"-available_stock").val( tmp3[1] );
});
}
'
],
]);
Please note I have used select2:select as the event handler.
I had to provide this as a separate answer because I don't have enough reputation to add a comment.
You can use yii2 select2 widget for this purpose. There is good documentation with demos available at
http://demos.krajee.com/widget-details/select2
An example usage with active form is given below
// Usage with ActiveForm and model
echo $form->field($model, 'state_1')->widget(Select2::classname(), [
'data' => $data,// the select option data items.The array keys are option values, and the array values are the corresponding option labels
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
'change' => 'function(){// write your on change code here}'
]
]);
I'm using CakePHP v2.2.1 stable. I have a UsersController with the action add(). I'm trying to send the user info via ajax (from the home page to /users/add) and save the data. My code is something like this:
// /app/View/Pages/home.ctp
<?php
$data = array('User' => array('username' => 'vegeta_super_sayajin',
'password' => 'over9000!', 'email' => 'vegeta#supersayajin.com',
'profile_pic' => '/home/pics/scouter.jpg', 'firstname' => 'Vegeta',
'lastname' => 'Vegeta', 'level_id' => '9001'));
?>
<script type="text/javascript">
var data = <?php echo json_encode($data); ?> //convert $data into json format
$.ajax({url: '/users/add', data: "data="+data, type: 'post'});
</script>
How do I receive this data in the UsersController, so that I can process and save it?
Currently, I'm trying:
// /app/Controller/UsersController.php
function add() {
if($this->request->is('post') {
//returns "Error: [object Object] in logfile
$this->log($this->request->data);
} else {
$this->Session->setFlash(__("The user could not be saved :("));
}
$this->autoRender = false;
}
$this->log($this->request->data) returns Error: [object Object] in the /app/tmp/logs/error.log file, and this user info does not exist in any of $this->request->params's indexes. All my googling so far has returned only complicated cakephp v1.3 techniques. How is this done in cakephp v2.2.1?
You can try the following code. It will work for you.
<?php
$data = array(
'User' => array(
'username' => 'vegeta_super_sayajin',
'password' => 'over9000!',
'email' => 'vegeta#supersayajin.com',
'profile_pic' => '/home/pics/scouter.jpg',
'firstname' => 'Vegeta',
'lastname' => 'Vegeta',
'level_id' => '9001')
);
?>
<script type="text/javascript">
var data = [<?php echo json_encode($data); ?>] //convert $data into json format
$.ajax({
url: 'checks/add',
data: "data="+JSON.stringify(data),
type: 'post'});
</script>
And in your controller's code:
// /app/Controller/UsersController.php
function add() {
if($this->request->is('post') {
$this->log(json_encode($this->request->data, true)); //returns "Error: [object Object] in logfile
} else {
$this->Session->setFlash(__("The user could not be saved :("));
}
$this->autoRender = false;
}
Here is the json_decode documentation. The second parameter true will convert the object into an array.