I am making an ajax request from my codeigniter view in javascript function but nothing happens and the success alert (ok) pops up
function show_drop_downn(){
document.getElementById("drop_downn").style.visibility = "visible";
$.ajax({
type: "POST",
url: "http://localhost/ok/index.php/search/ajax_delete_ntify",
success: alert('ok'),
});
}
This is my controller it is working perfect, when i copy paste the url (used in ajax request) in my browser every thing goes good, controller makes a call to the model and it works perfect
function ajax_delete_ntify()
{
echo "incontroller";
$email=$this->session->userdata('email_of_user');
$this->load->model('search_peoplee');
$data['userid']= $this->search_peoplee->get_userid_from_email($email);
foreach ($data['userid'] as $row)
{
$one=$row->userid;
}
$this->search_peoplee->delete_notifications($one);
return;
}
View :
function show_drop_downn(){
document.getElementById("drop_downn").style.visibility = "visible";
$.ajax({
type: "POST",
url: "http://localhost/ok/index.php/search/ajax_delete_ntify",
success:function(
console.log("success");
)
});
}
Controller :
function ajax_delete_ntify()
{
echo "incontroller";
$email=$this->session->userdata('email_of_user');
$this->load->model('search_peoplee');
$data= $this->search_peoplee->get_userid_from_email($email);
$res=$this->search_peoplee->delete_notifications($data[0]->userid);
if($res){
echo json_encode(array('success'=>true));
}else{
echo json_encode(array('success'=>false));
}
}
Related
The AJAX request goes through correctly, I checked with chrome's developer tools, there is a request on quiz.php page, but when I check for $_POST['risultato'] it looks doesn't exist. I noticed though that in Chrome's dev tools there's 2 quiz.php elements (one xhr the other document)
I tried changing the code in several ways, but it seems like it doesn't work
<?php
if(isset($_POST['risultato'])){
print($_POST['risultato']);
}
?>
<script>
function inviaRisultati(ris){
$.ajax({
url: "quiz.php",
type: "POST",
cache: false,
data: {risultato: ris},
success: function(){
alert("INVIATI");
}
})
}
The program is expected to return the result on quiz.php page (the same page where ajax request is fired), and it's supposed to print it somewhere
EDIT: I fixed it
<?php
file_get_contents('php://input');
if(isset($_POST['risultato'])){
print($_POST['risultato']);
}
?>
function inviaRisultati(param) {
return $.ajax({
url:"quiz.php",
method:"POST",
data:{action: "SLC", risultato :param},
dataType:"text"
});
}
inviaRisultati(1).done(function(response){``
document.open();
document.write(response);
});
In Ajax, the data attribute is in JSON format.
your data attribute will be like this
data: {risultato: ris}
Hi you can do it this way:
your php script:
if (isset($_POST["action"])) {
$action = $_POST["action"];
switch ($action) {
case 'SLC':
if (isset($_POST["risultato"])) {
$response = $_POST["risultato"];
echo $response;
}
}
break;
}
Where action is a command you want to do SLC, UPD, DEL etc and risultato is a parameter
then in your ajax:
var param = $('#yourinputid').val();
function getInfo(param) {
return $.ajax({
url:"quiz.php",
method:"POST",
data:{action: "SLC", risultato :param},
dataType:"text"
});
}
call it like this:
getInfo(param).done(function(response){
alert(response);
//do something with your response here
})
Hope it helps
HTML CODE
<script>
jQuery(document).ready(function(){
ris = 'abcd';
jQuery.ajax({
url: "quiz.php",
type: "POST",
cache: false,
data: {risultato: ris},
success: function(){
}
})
});
</script>
PHP CODE
<?php
file_get_contents('php://input');
print($_POST['risultato']);
Console Output
I am trying to call my controller function using ajax but it is not calling with input . Here is my ajax call
if(value)
{
$.ajax({
type:"POST",
dataType:"text",
cache:false,
contentType: "application/json; charset=utf-8",
url:"<?php echo base_url('Dashboard/deleteSpeciality'); ?>",
data:{'id':id},
success:function(data){
alert("i am in success");
},
error:function(data){
alert(data);
}
});
}
and here is my controller function. Ajax call is going but input is not. At server side program throws error Id is not defined.
public function deleteSpeciality($id) {
$result= $this->Dashboard_model->getSpeciality($id);
$path=$result[0]->ImagePath;
$this->load->helper("file");
delete_files($path);
unlink($path);
$this->Dashboard_model->deleteSpeciality($id);
return 1;
}
Try this in your controller $id=$_POST['id'] and as you are using Post method so don't pass id as parameter in function.
public function deleteSpeciality() {
$id=$_POST['id']
//code here
}
Try this
public function deleteSpeciality()
{
$id=$this->input->post('id');
// will print the id that was posted from ajax
//echo $id;
$result= $this->Dashboard_model->getSpeciality($id);
$path=$result[0]->ImagePath;
$this->load->helper("file");
delete_files($path);
unlink($path);
$this->Dashboard_model->deleteSpeciality($id);
return 1;
}
I've got this variable $type and I want it to be month or year.
It should be changed by pressing a div.
I've tried creating an onclick event with an ajax call.
The ajax call and the variable are in the same script (index.php)
Inside the onclick function:
var curr_class = $(this).attr('class');
$.ajax({
type: "POST",
url: "index.php",
data: {
type: curr_class
},
dataType: 'text',
success: function(data) {
// Test what is returned from the server
alert(data);
}
});
But the alert returns the whole html page.
When I console.log the data (create a var data = { type:curr_class }) and console.log *that data* it returnstype = month` (which is correct)
while I just want it to return month or year
So on top of the page I can call
if(empty($_POST['type'])){
$type = 'month';
} else {
$type = $_POST['type'];
}
and change the PHP variable so I can use it in the rest of my script.
But how can I accomplish this?
With kind regards,
as you are sending request to the same page so as a result full page is return .You will have to send it to another page and from that page return the type variable
if(empty($_POST['type'])){
$type = 'month';
} else {
$type = $_POST['type'];
echo $type;
keep this code in separate file and make an ajax call to that page
//Try This It's Work
Get Value
Get Value
$(".btn-my").click(function(){
var curr_class = $(this).data('title');
$.ajax({
type: "POST",
url: "index.php",
data: {
type: curr_class
},
dataType: 'text',
success: function(data) {
// Test what is returned from the server
alert(data);
}
});
});
I have a button Next that changes the page -
<a class="disabled" href="step">
<button class="btn btn-primary launch-btn disabled next">NEXT</button>
</a>
Also, when this button is clicked, I have an Ajax function that sends data to a controller function -
<script type="text/javascript">
$(function(){
$('.next').click(function() {
var a = $('#box').data('val');
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>study/CreateData",
data: a,
success: function(data)
{
console.log(data);
},
error: function()
{
console.log("fail");
}
});
});
});
</script>
And I'm using this to receive the value from Ajax -
public function CreateData() {
$data = $this->input->post('data');
return $data;
}
When the Next button hits its controller where it changes to a new page, I'm trying to retrieve the value posted by Ajax to the CreateData() function -
public function step()
{
$data = $this->CreateData();
if(!empty($data)){
print_r($data); exit;
}
else {
echo "blank"; exit;
}
$this->load->view('step2');
}
However, this keeps echoing blank. This obviously means that the $data being returned is empty but I'm not sure why. The success function in my Ajax script is logging data, so it's posting the value to CreateData(). What am I doing wrong here?
Your javascript should be something like this
<script type="text/javascript">
$(function(){
$('.next').click(function() {
var a = $('#box').data('val');
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>study/CreateData",
data: {my_data: a},
success: function(data)
{
console.log(data);
},
error: function()
{
console.log("fail");
}
});
});
});
</script>
Notice the data property. I have changed it to an object containing the variable a.
Now you should be able to retrieve this in your php function using
public function CreateData() {
$data = $this->input->post('my_data');
return $data;
}
Adr's answer solved part of the problem. In order to actually store and access the data, storing it in the session instead of a variable did the trick -
public function CreateData() {
$data = $this->input->post();
$this->session->unset_userdata('createData');
$this->session->set_userdata('createData', $data);
}
This prevented the $data variable from being overwritten when called the second time.
I'm trying to pass some variables to a php file with ajax
$(document).ready(function(){
$("button").click(function(){
var id = this.id;
var name=this.name;
console.log(id+" "+name);
$.ajax({
type: 'GET',
url: 'utility.php',
dataType: 'text',
data: {id: id, name: name},
success: console.log('aa'),
//error: function( jqXhr, textStatus, errorThrown ){ console.log( errorThrown ); },
//complete: alert(id+' '+name)
}); // Ajax Call
}); //event handler
}); //document.ready
<?php
warning('getting something');
if($_GET['id'] && $_GET['name']){
$id=$_GET['id'];
$name=$_GET['name'];
if($id=='delete'){
my_remove($name);
}
if($id=='modify'){
retrieve($name);
my_remove($name);
modify($name);
}
}
else {
warning('unable to get information');
}
function my_remove($name){
warning('deleting');
//mysqli_query($con,"DELETE FROM `book`.`".$page."` WHERE `".$page."`.`name` =\'".$name."\'");
//echo "<script type='text/javascript'>alert('$name');</script>";
}
function modify($name){
warning('modified');
}
function retrieve($name){
warning('fetching');
}
function warning($message){
echo "<script type='text/javascript'>alert('$message');</script>";
}
?>
The .js part seems to run smoothly, it sets the name and id as it should and returns a success message, but nothing else happens, not even the alert('getting something') which should run regardless of parameters.
Printing out the data gives [object Object] which I'm not even sure what it means.
Please help!
you're using GET not POST so under the line
if($_GET['id'] && $_GET['name']){
should be get, not post as you have it
$id=$_GET['id'];
$name=$_GET['name'];
since your php script runs in the background with your ajax call, I don't think the alert code in that page will work. Instead try returning the plain text to the ajax function and alert it there
$(document).ready(function(){
$("button").click(function(){
var id = this.id;
var name=this.name;
console.log(id+" "+name);
$.ajax({
type: 'GET',
url: 'utility.php',
dataType: 'text',
data: {id: id, name: name},
}).done(function(text){
alert(text);
}); // Ajax Call
}); //event handler
}); //document.ready
and your php file like this.I changed your warning function
<?php
warning('getting something');
if($_GET['id'] && $_GET['name']){
$id=$_GET['id'];
$name=$_GET['name'];
if($id=='delete'){
my_remove($name);
}
if($id=='modify'){
retrieve($name);
my_remove($name);
modify($name);
}
}
else {
warning('unable to get information');
}
function my_remove($name){
warning('deleting');
//mysqli_query($con,"DELETE FROM `book`.`".$page."` WHERE `".$page."`.`name` =\'".$name."\'");
//echo "<script type='text/javascript'>alert('$name');</script>";
}
function modify($name){
warning('modified');
}
function retrieve($name){
warning('fetching');
}
function warning($message){
echo $message;
}
?>