I have been struggling for days. I am doing ajax callback by assigning an input field but I am unable to too. Below is the code:
Script
$('[name="acccode[]"]').each(function(idx,val){
var acccode = $('[name="acccode[]"]').eq(idx).val();
console.log(acccode);
$.ajax({
url : "<?php echo base_url(); ?>readacccode",
type: "POST",
data:"Acc_Code="+acccode,
dataType: "JSON",
success: function(data)
{
$.each(data.results,function(idx,val){
console.log(val);
$('[name="accname[]"]').val(val);
});
},
error: function (data)
{
swal("Oops", "We couldn't connect to the server!", "error");
}
});
});
I am successfully able to get each acc code. But not the name.
Here is the Controller code:
public function readacccode()
{
if (!$this->ion_auth->logged_in() || $this->ion_auth->is_store())
{
redirect('account/login');
}
$acccode = $this->input->post('acccode');
$data['results']=$this->Setting->get_acccode($acccode);
echo json_encode($data);
}
it is getting the acc name where the acc code matches.
Please advise.
Thanks, everyone the problem is resolved. For those wanting to know here is the thing I change.
public function readacccode()
{
if (!$this->ion_auth->logged_in() || $this->ion_auth->is_store())
{
redirect('account/login');
}
$acccode = $this->input->post('Acc_Code'); //All I did is replace acccode to Acc_Code
$data['results']=$this->Setting->get_acccode($acccode);
echo json_encode($data);
}
Related
I want to give some alert message in my page but it working
This is my view code:
$.ajax({
url :"<?php echo base_url();?
>booking/dispatch_challan/DispatchChallanController/createDispatchChallan",
type:"POST",
dataType: "json",
data:$("#dispatch_challan_form").serialize(),
data:$("#dispatch_challan_form").serialize()+'&disp_ch_ids='+allVals,
success: function(data)
{
if(data.create_dispatch_challan.form_status=='false')
{
alertify.error('you may occured some server side errors');
}else if(data.create_dispatch_challan.form_status=='true')
{
alertify.success(data.create_dispatch_challan.form_message);
if (confirm())
{
alert("print");
}else{
alert("Do not print");
}
}
}
});
please give me same solution in this.
Its not OK to have a new line here
url :"<?php echo base_url();?
>booking/dispatch_challan/DispatchChallanController/createDispatchChallan",
Try this code: pastebin
I am trying to check if username already exists in the table using ajax input onchange trigger in CodeIgnitor view.
Ajax function on the view
<script>
$('#username').on('change',function(e){
var u_name=$(this).val();
$.ajax({
type: "POST",
url: "<?php echo site_url('home/checkUserExist');?>",
data: { u_name: u_name },
dataType: "html",
success: function(msg){
$('#error_msg').empty();
$('#error_msg').append(msg);
},
error:function(error){
$('#error_msg').append("There is some errors on server : " + error.error);
return false;
}
});
});
</script>
Controller function
function checkUserExist()
{
$u_name= strip_tags(mysql_real_escape_string(trim($this->input->post('u_name'))));
if($this->public->checkMemberExisit($u_name)==false)
{
echo "<p>Username Exists, Try another username</p>";
}
else
{
echo "<p>Username Available</p>";
}
}
but i keep getting this error :
There is some errors on server : function (){if(h){var d=h.length;!function f(b){n.each(b,function(b,c){var d=n.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}
Can anyone tell me whats going on. it used to work before.
I'm using Yii 2.0 basic version and I need some help.
I created one ajax function like this:
function eliminarColaborador(id) {
$.ajax({
type: "GET",
async: false,
url: "<?= urldecode(Url::toRoute(['colaborador/ajaxEliminarColaborador'])) ?>",
dataType: 'json',
data: {
'id': id
},
complete: function ()
{
},
success: function (data) {
if (data !== null)
{
// SUCCESS
}
else
{
// ERROR
}
}
});
}
My action in controller:
public function actionAjaxEliminarColaborador($id) {
if (Yii::$app->request->isAjax) {
$continuar = false;
if(isset($id) && $id > 0) {
var_dump($model); die;
$continuar = true;
}
echo CJSON::encode(array('continuar'=>$continuar));
Yii::$app->end();
}
}
I'm getting this erro in firebug: Not Found (#404): Page not found.
I tried everything, but i can't figure out what's the problem.
If I change ajax url to urldecode(Url::toRoute(['colaborador/delete'])) the error is gone and all works just fine.
Maybe I need to declare in ColaboradorController my new action ajaxEliminarColaborador, but I don't know how.
What is wrong?
controller
public function actionAjaxEliminarColaborador(){}
ajax
urldecode(Url::toRoute(['colaborador/ajax-eliminar-colaborador']))
It should be <?= urldecode(Url::toRoute(['colaborador/ajax-eliminar-colaborador'])) ?>. Here you can learn why.
Change the ajax response with:
url: '/colaborador/ajaxEliminarColaborador/$id',
data: {id: $id,_csrf: yii.getCsrfToken()},
Try to remove the word 'ajax' on your url.
I am validating a form with ajax and jquery in WordPress post comments textarea for regex. But there is an issue when i want to alert a error message with return false. Its working fine with invalid data and showing alert and is not submitting. But when i put valid data then form is not submit. May be issue with return false.
I tried making variable and store true & false and apply condition out the ajax success block but did not work for me.
Its working fine when i do it with core php, ajax, jquery but not working in WordPress .
Here is my ajax, jquery code.
require 'nmp_process.php';
add_action('wp_ajax_nmp_process_ajax', 'nmp_process_func');
add_action('wp_ajax_nopriv_nmp_process_ajax', 'nmp_process_func');
add_action('wp_head', 'no_markup');
function no_markup() {
?>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('form').submit(function (e) {
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: 'action=nmp_process_ajax&comment=' + comment,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
return false;
}
}
});
return false;
});
});
</script>
<?php
}
And i'm using wordpress wp_ajax hook.
And here is my php code.
<?php
function nmp_process_func (){
$comment = $_REQUEST['comment'];
preg_match_all("/(->|;|=|<|>|{|})/", $comment, $matches, PREG_SET_ORDER);
$count = 0;
foreach ($matches as $val) {
$count++;
}
echo $count;
wp_die();
}
?>
Thanks in advance.
Finally, I just figured it out by myself.
Just put async: false in ajax call. And now it is working fine. Plus create an empty variable and store Boolean values in it and then after ajax call return that variable.
Here is my previous code:
require 'nmp_process.php';
add_action('wp_ajax_nmp_process_ajax', 'nmp_process_func');
add_action('wp_ajax_nopriv_nmp_process_ajax', 'nmp_process_func');
add_action('wp_head', 'no_markup');
function no_markup() {
?>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('form').submit(function (e) {
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: 'action=nmp_process_ajax&comment=' + comment,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
return false;
}
}
});
return false;
});
});
</script>
<?php
}
And the issue that i resolved is,
New code
var returnval = false;
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
async: false, // Add this
data: 'action=nmp_process_ajax&comment=' + comment,
Why i use it
Async:False will hold the execution of rest code. Once you get response of ajax, only then, rest of the code will execute.
And Then simply store Boolean in variable like this ,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
returnval = false;
} else {
returnval = true;
}
}
});
// Prevent Default Submission Form
return returnval; });
That's it.
Thanks for the answers by the way.
Try doing a ajax call with a click event and if the fields are valid you submit the form:
jQuery(document).ready(function () {
jQuery("input[type=submit]").click(function (e) {
var form = $(this).closest('form');
e.preventDefault();
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {'action':'nmp_process_ajax','comment':comment},
success: function (res) {
var count = parseInt(res);
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
} else {
form.submit();
}
}
});
});
});
note : you call need to call that function in php and return only the count!
Instead of submitting the form bind the submit button to a click event.
jQuery("input[type=submit]").on("click",function(){
//ajax call here
var comment = jQuery('#comment').val();
jQuery.ajax({
method: "POST",
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: 'action=nmp_process_ajax&comment=' + comment,
success: function (res) {
count = res;
if (count > 10) {
alert("Sorry You Can't Put Code Here.");
return false;
}else{
jQuery("form").submit();
}
}
});
return false;
})
Plus also its a good idea to put return type to you ajax request.
Let me know if this works.
I have problem with ajax response in prestahop.
I wrote module with method
cheaperproduct.php
public function GetData(){
if(Tools::getValue('method')) echo "asdasd";
echo "string";
}
and in main.js
jQuery(document).ready(function(){
$('#cheaper form').submit(function(){
var url = $("#cheaper .url").val();
var email = $("#cheaper .email").val();
$.ajax({
url: baseDir+'modules/cheaperproduct/cheaperproduct.php',
method:"post",
data:'method=GetData',
success: function(data){
alert(data);
}
})
return false;
})
})
And I have empty alert.
I checked many ways to resolve my problem, but no one help me.
Can somebody explain me what's wrong?
try this:
public function GetData(){
if(Tools::getValue('method')) die(json_encode('ciao'));;
echo "method not found!";
}