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
Related
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);
}
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.
when i m running my project on local host then the ajax is working properly. but when i m uploading it on server then its not working... please tell me the sollution
This is my Controller
public function select_subcategory()
{
$str='<option value="">Select Option</option>';
$id=$this->input->post('id');
$res=$this->db->query("Select * from tn_topic_subcategory where category='".$id."'")->result();
foreach($res as $key)
{
$str.='<option value="'.$key->subcategory_id.'">'.$key->sub_ctg_name.'</option>';
}
echo $str;
}
And this is my Ajax
<script type="text/javascript">
function change_cat(id)
{
$.ajax({type:'post',
url:'<?PHP echo base_url()?>admin/select_subcategory',
data:{id:id},
success:function(res){
// alert(res);
$('#topic-subcategory').html(res);
}
});
}
</script>
If you are supposed to get your response in json format then try below code.
it will help you to send your variable in json format.
{
$.ajax({type:'post',
url:'<?PHP echo base_url();?>admin/select_subcategory',
data:{id:id},
dataType: "json",
success:function(res){
// alert(res);
$('#topic-subcategory').html(res);
}
});
}
Also try to send your response in json encoded.
I hope it will work for you.
Thanks
I'm trying to submit a form with Ajax.
In my form, I have the followin submit button
<?php echo CHtml::Button('SUBMITAjax',array('onclick'=>'send();')); ?>
the send function is a jquery script
$.ajax({
type: 'POST',
url: '<?php echo Yii::app()->createAbsoluteUrl("post/Ajax"); ?>',
data:data,
success:function(data){
alert("Data Saved");
},
error: function(data) { // if error occured
alert("Error occured.please try again");
alert(data);
},
dataType:'html'
});
the Ajax php function contains this code
$model=new Comment;
if(isset($_POST['CommentForm']))
{
$model->attributes=$_POST['CommentForm'];
if($model->validate())
{
// form inputs are valid, do something here
$model->save();
return;
}
}
Everything works fine but the data is not saved.
What is wrong?
Thank you in advance for your help.
why do not you simply create a form like this and submit it using ajax
<?php
echo CHtml::beginForm();
echo CHtml::textField('name');
echo CHtml::ajaxSubmitButton('submit form', Yii::app()->createAbsoluteUrl("post/Ajax"),array(
'success'=>new CJavaScriptExpression('function(data)
{
alert(data);
}
')
));
echo CHtml::endForm();
?>
Learn about ajaxUbmitButton here
I have an ajax function in jquery calling a php file to perform some operation on my database, but the result may vary. I want to output a different message whether it succeeded or not
i have this :
echo '<button id="remove_dir" onclick="removed('.$dir_id.')">remove directory</button>';
<script type="text/javascript">
function removed(did){
$.ajax({
type: "POST",
url: "rmdir.php",
data: {dir_id: did},
success: function(rmd){
if(rmd==0)
alert("deleted");
else
alert("not empty");
window.location.reload(true);
}
});
}
</script>
and this
<?php
require('bdd_connect.php');
require('functions/file_operation.php');
if(isset($_POST['dir_id'])){
$rmd=remove_dir($_POST['dir_id'],$bdd);
}
?>
my question is, how to return $rmd so in the $.ajax, i can alert the correct message ?
thank you for your answers
PHP
<?php
require('bdd_connect.php');
require('functions/file_operation.php');
if (isset($_POST['dir_id'])){
$rmd=remove_dir($dir_id,$bdd);
echo $rmd;
}
?>
JS
function removed(did){
$.ajax({
type: "POST",
url: "rmdir.php",
data: {dir_id: did}
}).done(function(rmd) {
if (rmd===0) {
alert("deleted");
}else{
alert("not empty");
window.location.reload(true);
}
});
}
i advice to use json or :
if(isset($_POST['dir_id'])){
$rmd=remove_dir($dir_id,$bdd);
echo $rmd;
}
You need your php file to send something back, then you need the ajax call on the original page to behave based on the response.
php:
if(isset($_POST['dir_id'])){
$rmd=remove_dir($dir_id,$bdd);
echo "{'rmd':$rmd}";
}
which will output one of two things: {"rmd": 0} or {"rmd": 1}
We can simulate this return on jsBin
Then use jquery to get the value and do something based on the response in our callback:
$.ajax({
type: "POST",
dataType: 'json',
url: "http://jsbin.com/iwokag/3",
success: function(data){
alert('rmd = ' + data.rmd)
}
});
View the code, then watch it run.
Only I didn't send any data here, my example page always returns the same response.
Just try echoing $rmd in your ajax file, and then watching the console (try console.log(rmd) in your ajax response block)
$.ajax({
type: "POST",
url: "rmdir.php",
data: {dir_id: did},
success: function(rmd){
console.log(rmd);
}
});
You can then act accordingly based on the response
Try echo the $rmd out in the php code, as an return to the ajax.
if(isset($_POST['dir_id'])){
$rmd=remove_dir($dir_id,$bdd);
//if $rmd = 1 alert('directory not empty');
//if $rmd = 0 alert('directory deleted');
echo $rmd;
}
Your "rmd" in success: function(rmd) should receive the callabck.