i have a jQuery code that posts an ID to another php file
<script type="text/javascript">
$(function() {
//More Button
$('.more').live("click",function() {
var ID = $(this).attr("id");
if(ID){
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
}else{
$(".morebox").html('The End');
}
return false;
});
});
</script>
How can I make it post another query along with "last message id"? I have a MySQL query that will output a value (pid). How can I post this along with "last message id"?
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID + "&pid=" + your_pid,
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
Here is my solution:
var salji = {
rate: score,
image_id: img_id
};
$.ajax({
type: "POST",
url: "widgets/rate.php",
data: salji,
success: function(msg){
alert('wow');
}
});
Related
How can I update a 'status' field as approved when I click on a button?
function UpdateRecord(id) {
jQuery.ajax({
type: "POST",
url: "update.php",
data: 'id=' + id,
cache: false,
success: function(response) {
alert("Record successfully updated");
}
});
}
<script>
function UpdateRecord(id)
{
jQuery.ajax({
type: "POST",
url: "update.php",
data: {id:id},
cache: false,
success: function(response)
{
alert("Record successfully updated");
}
});
}
</script>
and in update.php
<?php
if(isset($_POST['id']))
{
$id = $_POST['id'];
// update here to this id..
}
?>
I am facing on problem, I have made a CRUD code with AJAX for my CI page, it is working fine on XAMPP server but when I uploaded it to Live Server (Godaddy), it's not fetching data from database and showing 405 Method not Allowed error.
http://www.fenxteksolutions.com/admin/metalinks
Here is my Code.
<script>
$(function(){
showAllEmployee();
//Add New
$('#btnAdd').click(function(){
$('#myModal').modal('show');
$('#myModal').find('.modal-title').text('Add New Meta Detail');
$('#myForm').attr('action', '<?php echo base_url() ?>admin/addEmployee');
});
$('#btnSave').click(function(){
var url = $('#myForm').attr('action');
var data = $('#myForm').serialize();
//validate form
var empoyeeName = $('input[name=txtEmployeeName]');
var address = $('textarea[name=txtAddress]');
var meta_tag = $('input[name=txtMetaTag]');
var meta_desc = $('input[name=txtMetaDesc]');
var result = '';
if(empoyeeName.val()==''){
empoyeeName.parent().parent().addClass('has-error');
}else{
empoyeeName.parent().parent().removeClass('has-error');
result +='1';
}
if(address.val()==''){
address.parent().parent().addClass('has-error');
}else{
address.parent().parent().removeClass('has-error');
result +='2';
}
if(meta_tag.val()==''){
meta_tag.parent().parent().addClass('has-error');
}else{
meta_tag.parent().parent().removeClass('has-error');
result +='3';
}
if(meta_desc.val()==''){
meta_desc.parent().parent().addClass('has-error');
}else{
meta_desc.parent().parent().removeClass('has-error');
result +='4';
}
if(result=='1234'){
$.ajax({
type: 'ajax',
method: 'post',
url: url,
data: data,
async: false,
dataType: 'json',
success: function(response){
if(response.success){
$('#myModal').modal('hide');
$('#myForm')[0].reset();
if(response.type=='add'){
var type = 'added'
}else if(response.type=='update'){
var type ="updated"
}
$('.alert-success').html('News '+type+' successfully').fadeIn().delay(4000).fadeOut('slow');
showAllEmployee();
}else{
alert('Error');
}
},
error: function(){
alert('Could not add data');
}
});
}
});
//edit
$('#showdata').on('click', '.item-edit', function(){
var id = $(this).attr('data');
$('#myModal').modal('show');
$('#myModal').find('.modal-title').text('Edit Employee');
$('#myForm').attr('action', '<?php echo base_url() ?>admin/updateEmployee');
$.ajax({
type: 'ajax',
method: 'get',
url: '<?php echo base_url() ?>admin/editEmployee',
data: {id: id},
async: false,
dataType: 'json',
success: function(data){
$('input[name=txtEmployeeName]').val(data.page);
$('textarea[name=txtAddress]').val(data.title);
$('input[name=txtMetaTag]').val(data.meta_tag);
$('input[name=txtMetaDesc]').val(data.meta_desc);
$('input[name=txtId]').val(data.id);
},
error: function(){
alert('Could not Edit Data');
}
});
});
//delete-
$('#showdata').on('click', '.item-delete', function(){
var id = $(this).attr('data');
$('#deleteModal').modal('show');
//prevent previous handler - unbind()
$('#btnDelete').unbind().click(function(){
$.ajax({
type: 'ajax',
method: 'get',
async: false,
url: '<?php echo base_url() ?>admin/deleteEmployee',
data:{id:id},
dataType: 'json',
success: function(response){
if(response.success){
$('#deleteModal').modal('hide');
$('.alert-success').html('Employee Deleted successfully').fadeIn().delay(4000).fadeOut('slow');
showAllEmployee();
}else{
alert('Error');
}
},
error: function(){
alert('Error deleting');
}
});
});
});
//function
function showAllEmployee(){
$.ajax({
type: 'ajax',
url: '<?php echo base_url() ?>admin/showAllEmployee',
async: false,
dataType: 'json',
success: function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html +='<tr class="odd gradeX">'+
// '<td>'+data[i].id+'</td>'+
'<td>'+data[i].page+'</td>'+
'<td>'+data[i].title+'</td>'+
'<td>'+data[i].meta_tag+'</td>'+
'<td>'+data[i].meta_desc+'</td>'+
'<td>'+
'Edit'+
'</td>'+
'<td>'+
'Delete'+
'</td>'+
'</tr>';
}
$('#showdata').html(html);
},
error: function(){
alert('Could not get Data from Database');
}
});
}
});
</script>
This code is working very well in XAMPP Server and giving all data from database and all functions are working very well, but on hosting it is not fetching data and when I inspect the page in console it gave 405 method not allowed error.
Your error is in type parameter in ajax. Remove it or set it to GET. It is actually request method
$.ajax({
type: 'GET',
url: '<?php echo base_url() ?>admin/showAllEmployee',
async: false,
dataType: 'json',
success: function(data){
.......................
I Use this code for paging
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var dataModul = 'modul='+modul;
var dataUser = 'userid='+userid;
var dataString = 'pageId='+ pageId;
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: dataString,
data: dataModul,
data: dataUser,
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);
}
});}
And this for an action file with name loadDataVerifikasi.php
if(isset($_POST['pageId']) && !empty($_POST['pageId'])){
$id=$_POST['pageId'];
}
else{
$id='0';
}
$pageLimit=PAGE_PER_NO*$id;
$query="select * from pengajuan_".$_POST['modul']." join verifikasi on pengajuan_".$_POST['modul'].".ketkdpengajuan = verifikasi.ketkdpengajuan where kdadmin = '".$_POST['userid']."' group by kdverifikasi limit $pageLimit,".PAGE_PER_NO;
But $_POST['modul'] post nothing,
I am not familiar with your syntax.
Please try:
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var datastring = {
modul: modul,
userid: userid,
pageId: pageId
}
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: dataString,
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);
}
});
}
The $.ajax data attribute accepts an object with key-value pairs
I believe it is because you overwrite the data with dataUser.
I haven't tested i but the code below should work.
Also check out this for more info
function changePagination(pageId,liId,modul,userid){
$(".flash").show();
var dataModul = 'modul='+modul;
var dataUser = 'userid='+userid;
var dataString = 'pageId='+ pageId;
$.ajax({
type: "POST",
url: "loadDataVerifikasi.php",
data: { 'dataString':dataString, 'dataModul':dataModul, 'dataUser':dataUser },
cache: false,
success: function(result){
$(".flash").hide();
$(".link a").removeClass("In-active current") ;
$("#"+liId+" a").addClass( "In-active current" );
$("#pageData").html(result);
I have a error on ajax data:value
see my code
<script>
$(document).ready(function(){
$("#pros").change(function(e){
e.preventDefault()
var value = $("#pros").val();
$.ajax({
type: "GET",
url: "product.php",
dataType: "html",
data: value,
success: function(msg){
$("#products").html(msg);
}
});
});
});
</script>
when i pass the value to product page then when i echo I get error
Undefined index: value product.php on line 2
product.php page
$q = $_GET['value'];
echo $q;
You have to send an hash:
$.ajax({
type: "GET",
url: "product.php",
dataType: "html",
data: { 'value' : $("#pros").val() },
success: function(msg){
$("#products").html(msg);
}
});
Notice { 'value' : $("#pros").val() }.
$(document).ready(function () {
$("#pros").on('change', function (e) {
e.preventDefault()
$.ajax({
type: "GET",
url : "product.php",
dataType: "html",
data: {value: this.value} //key / value
}).done(function(msg) {
$("#products").html(msg);
});
});
});
<script>
$(document).ready(function(){
$("#pros").change(function(e){
e.preventDefault()
var value = $("#pros").val();
$.ajax({
type: "GET",
url: "product.php",
dataType: "html",
data: {'value':value},
success: function(msg){
$("#products").html(msg);
}
});
});
});
</script>
I want to send a hash string to my php file. but failed, can anyone help me please?
my javascript is like this:
var hashString = "#support_form";
$.ajax ({
type: "POST",
url:"index.php",
data: hashString,
success: function() {
console.log("message sent!");
}
});
and the php:
<?php
$theHash = $_POST['hashString'];
?>
What should I do? Thanks
You need to specify the name/value for data
data: {hashString:hashString},
You have to do like this-
$.ajax ({
type: "POST",
url:"index.php",
data: "hashString=value",
success: function() {
console.log("message sent!");
}
});
So you can get the value as-
$theHash = $_POST['hashString'];
echo $theHase; //will print value
Use:
data: 'hashString=' + encodeURIComponent(hashString),
Your code would work if you used an object instead of a string. try this:
var hashString = {hashString: "#support_form"};
$.ajax ({
type: "POST",
url:"index.php",
data: hashString,
success: function() {
console.log("message sent!");
}
});
Your JS should be this:
var hashString = "#support_form";
$.ajax ({
type: "POST",
url:"index.php",
data: {hashString: hashString},
success: function() {
console.log("message sent!");
}
});
data key must be of object type
This will work for you
var hashString = "#support_form";
$.ajax ({
type: "POST",
url:"index.php",
data: {'hashString':hashString},
success: function() {
console.log("message sent!");
}
});
$.ajax ({
type: "POST",
url:"index.php",
data: {"hashString":hashString},
success: function() {
console.log("message sent!");
}
});
var queryString = "f_name=" + f_name + "&l_name=" + l_name + "&contact_no=" + contact_no + "&email=" + email;
$.ajax({
type:'POST',
url:'insert1.php',
data:queryString,
dataType:'json',
success: function(success){
console.log("message sent!");
}
});