codeigniter ajax post value undefined error on index page - php

I'm working on the codeigniter web application I need to get users' data from the database but based on user id?
The problem is that ajax posting value to the index page browsers, network showing that Ajax posting value to the index page, When I try to echo or print variable on the index page.
Index Page Showing Error After Echo leadID
Notice: Undefined index: leadID and leadID = 0
Index Page
$addnote ="<a class='btn' data-popup-open='popup-1' href='#'
data-id='$lead->id'>Add Note</a>";
Ajax Posting
$('.btn').on('click', function(e) {
var targeted_popup_class = jQuery(this).attr('data-popup-open');
$('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);
var leadID = $(this).attr('data-id');
var dataString = 'leadID=' + leadID;
$.ajax({
type: "POST",
dataType: 'text',
url: "index.php",
data: dataString,
success: function(response) {
}
});
});
Database Query
$leadID = intval($_POST['leadID']);
$sql = "select Note from {$dbPre}users where leadID='$leadID'";
$exists = $db->extQueryRowObj($sql);
$displaynote = $exists->Note;

If you need simple php solution then try this code
$('.btn').on('click', function(e) {
var targeted_popup_class = jQuery(this).attr('data-popup-open');
$('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);
var leadID = $(this).attr('data-id');
$.ajax({
type: "POST",
dataType: 'text',
url: "index.php",
data: {
leadID : leadID
}
success: function(response) {
alert(response);
}
});
});
Now in index.php
$leadID = intval($_POST['leadID']);
$sql = 'select Note from '.$dbPre.'users where leadID='.$leadID;
$exists = $db->extQueryRowObj($sql);
$displaynote = $exists->Note;
echo $displaynote;

Related

Form loaded via ajax wont append data to parent window

So I am basically creating a Restaurant Menu editor for their website. The issue I am having is when the click a category named Brunch, I load the file edit_cat.php via ajax onto the page. All data is passed correctly, but on success of the form being filled out I would like to post a success message on the parent window and am having no luck. Coincidentally, upon success the alert(data) does popup with the response.
edit_cat.php both handles the form submission and is the form
$(document).ready(function(){
$('.editBtn').on('click', function(e) {
e.preventDefault();
var $this = $(this),
id = $this.data('id'),
type = $this.data('type');
if (type == 'cat') {
data = "&cat=" + id;
$.ajax({
data: data,
type: 'POST',
url: 'edit_cat.php',
success: function(data) {
$('#contentLeft').html(data);
}
});
}
});
$('#contentLeft').on('click', 'input[type="submit"]', function(e) {
e.preventDefault();
var $this = $(this),
frm = $('#edit-cat-frm'),
id = $('form').data('id'),
name = $(frm).find('input[name="name"]').val(),
desc = $(frm).find('textarea[name="desc"]').val(),
send = $(frm).find('input[name="submit"]').val();
data = "&cat=" + id + "&name=" + name + "&desc=" + desc + "&submit=" + send;
$.ajax({
data: data,
type: 'POST',
url: 'edit_cat.php',
success: function(data) {
window.parent.$('.left-container-head').innerHtml(data);
}
});
});
});
Can you please try this as simply,
$('.left-container-head').html(data);
Try this:
$(".left-container-head", window.parent.document).html(data);

Error when using jQuery Ajax POST (json) with php

On page load I use this code to display data from database:
<script>
$(function feed(){
var page = 1;
var type = '<?php echo $filter ;?>';
var theData = {};
theData['page'] = 'profile';
theData['type'] = type;
theData['username'] = '<?php echo $user_data->username; ?>';
theData['get_activities'] = 'true';
$.ajax({
type: "POST",
url: "data.php",
data: theData,
dataType:'json',
success: function(data){
$("#activities").html(data.activity_feed);
if(page < data.total_pages){
$("#activities").after('<div id="loader"><button id="load_more_activities">Load more</button></div>');
}
}
});
$("#activity_container").on("click", "#load_more_activities", function(){
var next = page+=1;
var type = '<?php echo $filter ;?>';
var theData = {};
theData['page'] = 'profile';
theData['type'] = type;
theData['username'] = '<?php echo $user_data->username; ?>';
theData['get_activities'] = 'true';
theData['page_num'] = next;
$.ajax({
type: "POST",
url: "data.php",
data: theData,
dataType: "json",
success: function(data){
$("#activities").append(data.activity_feed);
if(next == data.total_pages){
$("#loader").html("No more data");
} else {
$("#loader").html('<div id="loader"><button id="load_more_activities">Load more</button></div>');
}
},
});
});
});
</script>
Everything work fine, but if I refresh page more than 5 or 6 times or if load_more_activities function is called more than 5 or 6 times...then post is not executed and I don't get any data displayed...
Is something wrong with this code or there are maybe some restrictions from my host provider?
When post is executed:
When post is not executed:
My host provider has a protection from several sending ajax data with variables named "username" or "password". So when this happens, server detect some kind »brute-force« and then drop-all-packets.

Ajax call returns nothing

I am trying to make an ajax call on click on anchor tag dynmically generated from $.each loop for a JSON response.
For Information : #records is my table and .update is the class of anchor tag in that table.
Please be informed that the table is generated dynamically.
Now the problem is that my ajax call is returning nothing even i have checked it error: but no response received. I have tried alerting my var data just before the ajax call and it worked.So the problem starts from the ajax call. Moreover, my server side code is running fine.
// Update existing customers
$("#records").on('click', ".update", function() {
var data = '?'+ $(this).attr('id');
$.ajax({
type: "GET",
url: "viewcustomers.php",
data: data,
success: function(response) {
console.log(response);
}
});
});
Thanks in advance.
For reference below is the code that generates the table.
// Function to make datagrid
function getRecords() {
$.getJSON("viewcustomers.php", function(data) {
var items = [];
var xTd = '';
var xTr = '';
$.each(data, function(key, val) {
var c = 0;
var id = 0;
$.each(val, function(key1, val1) {
if (c == 0)
{
id = val1;
}
c++;
xTd += '<td>' + val1 + '</td>';
});
xTd += '<td>Edit</td>';
xTd += '<td>Delete</td>';
xTr = '<tr>' + xTd + '</tr>';
items.push(xTr);
xTd = '';
xTr = '';
});
$("#records").append(items);
});
}
Updated the server side code:
page url : localhost/hotel/viewcustomers.php
/**
* Fetch single row for the purpose of update / delete.
*/
if(isset($_GET['update'])){
$customer = new Customers;
$Id = $_GET['update'];
$customer_single = $customer->View_Single_Customer($Id);
echo json_encode($customer_single);
unset($customer);
}
This line is not used the right way var data = '?'+ $(this).attr('id');
Change it like this: var my_id = $(this).attr('id');
Then update the line data: data with data : {id:my_id}
Complete code :
$("#records").on('click', ".update", function() {
var my_id = $(this).attr('id');
$.ajax({
type: "GET",
url: "viewcustomers.php",
data : {id : my_id},
success: function(response) {
console.log(response);
}
});
});
Or do it like this:
$("#records").on('click', ".update", function() {
var param = '?id='+ $(this).attr('id'); /*notice that I have added "id=" */
$.ajax({
type: "GET",
url: "viewcustomers.php" + param,
/* remove the data attribute */
success: function(response) {
console.log(response);
}
});
});
Modify it as
$("#records").on('click', ".update", function() {
var request = '?id='+ $(this).attr('id');
$.ajax({
type: "GET",
url: "viewcustomers.php" + request,
success: function(response) {
console.log(response);
}
});
});

Post ajax to PHP - variable isn't being uploaded

I am trying to pass a variable from my ajax to my php script. I can't see to get it to work. It keeps on giving me NULL when I do a var_dump on the variable.
JQUERY:
$(document).ready(function() {
$('.trigger').click(function() {
var id = $(this).prev('.set-id').val();
$.ajax({
type: "POST",
url: "../modules/Slide_Show/slide_show.php",
data: id
});
LinkUpload(id);
function LinkUpload(id){
$("#link-upload").dialog();
}
});
});
</script>
PHP:
$id = $_POST['id'];
$query = mysql_query("SELECT * FROM xcart_slideshow_slides where slideid='$id'")or die(mysql_error());
$sli = mysql_fetch_array($query);
$slide_id = $sli['slideid'];
$link = $sli['link'];
var_dump($id);
I need the $id variable to post so I can dynamically change the dialog box when the click function is activated.
EDIT:
So I have changed some of my coding:
Jquery:
$(document).ready(function() {
$('.trigger').click(function() {
var id = $(this).prev('.set-id').val();
$.post(
"slide-show-link.php",
{ id: id },
function(data,status){alert("Data: " + data + "\nStatus: " + status); }
);
// alert(id);
LinkUpload(id);
});
function LinkUpload(id){
$("#link-upload").dialog();
}
});
I wanted to see if the data was in fact being passed so I threw an alert in the .post. This is the error I'm getting now:
I have tried passing plain text and echoing it back on the page but it fails. It is just not passing.
Try this -
$.ajax({
type: "POST",
url: "../modules/Slide_Show/slide_show.php",
data: { id : id }
});

Selecting data Ajax

I would like to select streamitem_id from my insert.php and add it to my existing post div. I'm needing the id of the post made so I can add it into my div so later when I add my delete button it will delete the post and the div that holds it. Hope I've made sense and I can do this with what I have as I'm trying to learn but it is a difficult language like any when first starting out.
AJAX
<script>
$(document).ready(function(){
$("form#myform").submit(function(event) {
event.preventDefault();
var content = $("#toid").val();
var newmsg = $("#newmsg").val();
$.ajax({
type: "POST",
url: "insert.php",
data: "toid=" + content + "&newmsg=" + newmsg,
success: function(){
$("#homestatusid").prepend("<div id='divider-"+WHERE MY STREAMITEM_ID NEEDS TO BE+"'><div class='userinfo'>"+newmsg+"</div></div>");
}
});
});
});
</script>
INSERT.PHP
$check = "SELECT streamitem_id FROM streamdata WHERE streamitem_id=$user1_id";
$check1 = mysql_query($check);
$check2 = mysql_num_rows($check1);
echo $check2;
In your javascript should be
$.ajax({
type: "POST",
url: "insert.php",
data: {toid:content, newmsg: newmsg}, # pay attention to this line
success: function(data){
$("#homestatusid").prepend("<div id='divider-"+data+"'><div class='userinfo'>"+newmsg+"</div></div>");
}
});
PHP
$toid = isset($_POST['toid']) ? $_POST['toid'] : null;
$newmsg = isset($_POST['newmsg']) ? $_POST['newmsg'] : null;
And do not use mysql_* since it deprecated
The first argument passed to the success callback is the responseText from the AJAX call. Modify your jQuery code to this:
success: function(responseText){
$("#homestatusid").prepend("<div id='divider-"+responseText+"'><div class='userinfo'>"+newmsg+"</div></div>");
// I'm assuming that insert.php returns just the ID you're interested in
}
success: function(response){
$("#homestatusid").prepend("<div id='divider-"+response+"'><div class='userinfo'>"+newmsg+"</div></div>");
}

Categories