Not able to receive ajax post data - javascript

I'm trying to print a calendar using php but struggle figuring out how to change the month displayed with ajax
I have two buttons in tag whose value is "+1" and "-1" respectively with a class="selectMonth"
<button class="selectMonth" name="selectMonth" value="-1">previous</button>
<button class="selectMonth" name="selectMonth" value="+1">next</button>
This is my ajax code:
$(".selectMonth").on("click", function(){
$.ajax({
url : "index.php",
type : "POST",
data : {selectMonth : this.value},
success : function(){
alert("success!");
}
});
});
and in my index.php I have
<?php
if(!isset($_POST["selectMonth"]))
$_SESSION["date"] = time();
else
{
$selectMonth = $_POST["selectMonth"];
$_SESSION["date"] = strtotime($selectMonth . 'month');
}
var_dump($_POST["selectMonth"]);
$date = $_SESSION["date"];
print_calendar($date);
?>
After I click one of the buttons, I can get the alert message but not the $_POST variable and what is var_dump is always NULL
Could anybody find out the mistake for me?
I'm still learning ajax.
Thank you very much!

try below line of code :
data : {'selectMonth' : $(this).val()},
OR
data : {'selectMonth' : $(this).attr('value')},

Try
$(".selectMonth").on("click", function(){
var inputVal = $(this).attr('value');
$.ajax({
url : "index.php",
type : "POST",
data : {selectMonth : inputVal},
success : function(){
alert("success!");
}
});
});

Instead of
data : {selectMonth : this.value}
try
data : {selectMonth : this.val()}

Related

How to show day from input date in CodeIgniter

I want to show day into textbox based on my input date
here is my ajax to change the textbox :
$("#date").change(function(){
$.ajax({
url : "<?php echo base_url();?>daftar/get_day",
method : "POST",
data : {date: $("#date").val()},
async : false,
success: function(data){
$("#day").val(day);
}
});
});
this is my controller :
function get_day()
{
$date=$this->input->post('date');
$this->load-model('daftarmodel');
$day = $this->daftarmodel->geDay($date);
echo $day;
}
this is my model :
function getDay($date){
$timestamp = strtotime($date);
$day = date('D', $timestamp);
var_dump($day);
return $day;
}
is there anything wrong with my code which not showing the day
Not sure if this is the part of your problem, but looks like you may be using the wrong variable in your ajax success.
success: function(data){
$("#day").val(day);
}
you could try replacing "day" with the "data" variable that is in your: "success: function(data)"
success: function(data){
$("#day").val(data);
}
First of all you need to remove var_dump($day); from your model.
Secondly need to little bit improvement in your understanding . Ajax
call return response in case of successfully execution ajax return result
in success function. In your case it will return day in data.
success: function(data){
$("#day").val(data);
}

Error by passing variable from JS to PHP

I know this question was already answered one million times but I'm becoming desperate with this code.
I have two files php files and I want to send a variable from a part written in JS to the other PHP file. So here's the code:
carga_datos.php
<script>
function MyAlert() {
var radio1 = $("input[name='inputTipoActividad']:checked").val();
$.ajax({
url : 'post.php',
type : "POST",
data: {'radio1' : radio1},
dataType:'json',
success : function(data) {
alert(data);
},
});
}
</script>
I'm calling to MyAlert() in a radio button form
post.php
<?php
$radio1 = $_POST['radio1'];
echo $radio1;
?>
And I call post.php into carga_datos.php like this:
And this is the error I'm retrieving:
Notice: > Undefined index: radio1 in C:\xampp\htdocs\gis\post.php on line 2
Edit:
It seems you have used dataType "json" so it will expect a "json", if none is returned you will get undefined or an empty response.
If you remove dataType: "json" you should be able to retrieve the data just fine.
function MyAlert() {
var radio1 = $("input[name='inputTipoActividad']:checked").val();
$.ajax({
url : 'post.php',
method : "POST",
data: {"radio1" : radio1},
success : function(data) {
console.log(data);
}
});
}
If you want to check with "dataType: 'json'" still in your code, you need to return your PHP code like the following:
<?php echo json_encode($_POST); ?>
This will then either return your $_POST data or just an empty [] array.
// Old Answer for just true/false values
If you just want to see if the checkbox was checked or not to retrieve a boolean, please try the following:
var radio1 = $("input[name='inputTipoActividad']").prop("checked");
This will return TRUE or FALSE. If not wrong, if you use
$("input[name='inputTipoActividad']:checked").val()
it'll return "on" or nothing (might be wrong here though).
Tested it with the ".prop("checked")" and it worked for me.

POST value not passing to PHP a deleting SQL row

I am trying to post the 'ID' from my html table to delete the row from the SQL database when the delete button is click. So far it would appear the POST is not working but the PHP code is.
Here is the JavaScript code which takes the selected table rows value from the first columns (ID) when I select it, and on pressing the delete button a message box displays the ID (this works fine so it would appear it is reading the value). The next part then displays a message box confirming the delete, which on clicking OK does nothing.
JavaScript
$(function(){
$("#tblAssets tr").click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
});
$('#btnDelete').click(function(e){
var value = $("#tblAssets tr.selected td:first").html();
alert(value);
if (confirm("Are you sure you want to delete this asset?")) {
$.ajax({
type : "POST",
url : "inventory_hardware_delete.php",
data : value;
success : function() {
}
});
}
});
});
And here is the PHP code. When I manually adjust $value to equal an ID in my database table it works and deletes the row.
$value = $_POST['value'];
$sql = "DELETE FROM [Assets].[dbo].[Hardware] WHERE [ID] = $value";
$result = sqlsrv_query( $conn, $sql);
Thanks
this line is wrong
data : value;
Try this
data : {value:value},
^ not semi colon
You are passing value without parameter. Add parameter:
$(function(){
$("#tblAssets tr").click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
});
$('#btnDelete').click(function(e){
var data = {
"value": $("#tblAssets tr.selected td:first").html();
};
if (confirm("Are you sure you want to delete this asset?")) {
$.ajax({
type : "POST",
url : "inventory_hardware_delete.php",
data : $.param(data);
success : function() {
}
});
}
});
});
or your ajax data should be like this:
data: "value="+$("#tblAssets tr.selected td:first").html();
Moreover you cannot use ; here:
data : value;
it should be ,
data : "value"+value,//pass value with the name:ie value=10 . replace the ajax part alone as given below
$.ajax({
type : "POST",
url : "inventory_hardware_delete.php",
data : "value"+value,//
success : function() {
}
});
You didn't create an object and passed the variable on the fly directly.
var value = $("#tblAssets tr.selected td:first").html();
//alert(value);
if (confirm("Are you sure you want to delete this asset?")) {
var dataValue = {
theValue : value // You have to create an object here
};
$.ajax({
type : "POST",
url : "inventory_hardware_delete.php",
data : dataValue, // pass the object here
success : function() {
}
});
}
On the server side:
$value = $_POST['theValue'];
Try this one.

Ajax post doesn't send data

I built a vote system with ajax and php, and I send data to php page for saved data in db.
I tried to send data with ajax post and php.
My problem is the data is not send to the page.
My js code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajaxSetup({
url: 'vote.php',
type: 'POST',
cache: 'false'
});
$('.vote').click(function(){
var self = $(this);
var action = self.data('action');
var parent = self.parent().parent();
var imgid = <?=$array['id'];?>;
if (!parent.hasClass('.disabled')) {
if (action == 'up') {
parent.find('#image-like').addClass('disabled_up');
$.ajax({data: {'imgid' : imgid, 'action' : 'up'}});
}
else if (action == 'down'){
parent.find('#image-dislike').addClass('disabled_down');
$.ajax({data: {'imgid' : imgid, 'action' : 'down'}});
};
parent.addClass('.disabled');
};
});
});
</script>
and my html code:
Use post method. This is not the correct code, but it's an idea, always works for me.
$('.vote').click(function(){
//Your vars
var data='voteup';
//Your actions... ddClass/removeClass...
$.post('vote.php',data,function(data){
//On your vote.php use "if($data=='voteup') else ;"
//And show message here...
alert(data);
});
return false;
});
example of vote.php
<?php
$data=$_POST['data'];
if($data=='voteup')
echo "You voted up!";
else echo "You voted down!";
?>
It's just an idea (:
You can try changing this:
if (!parent.hasClass('.disabled')) {
to this:
if (!parent.hasClass('disabled')) {
Some Notes:
From the docs:
For $.ajaxSetup()
Description: Set default values for future Ajax requests. Its use is not recommended.
Try using .post() function, you can set a callback when your action is done
jQuery.post(URL_TO_REACH, {ID_VALUE1 : 'my value' , ID_VALUE2 : 'my second value' })
.done(function( data_ajax ) { // data_ajax : Your return value from your php script
alert( data_ajax );
})
});
Hope this will help you
Official documentation : http://api.jquery.com/jQuery.post/

Unable to send data using AJAX post

Can someone please help me with this code?
It changes the class, but no data is sent to the server. I Do not get any errors too.I think i might have messed up the var (declaration ) or the html
Here is the html
<a class="reg" id="<?php echo $pID?>" href="#">Registrate</a>
Here is the script
<script>
$(document).ready(function(){
$('a').click(
function(){
if ($(this).hasClass('reg')){
$(this).removeClass('reg').addClass('done').text('done');
var datasend = $(this).html();
$.ajax({type:"POST", url:"data/update.php", data: 'id='+datasend, success:function(result){
}});
}
});
});
</script>
Replace this :
var datasend = $(this).html();
with
var datasend = this.id;
Also send your data as an object, which will remove the need for the datasend variable.
$.ajax({
url : '/url',
type : "POST",
data : {
id : this.id
}
});
replace
data: 'id='+datasend
with
data:{ id : datasend }
Replace:
$.ajax({type:"POST", url:"data/update.php", data: 'id='+datasend
With:
$.ajax({type:"POST", url:"data/update.php", data: {'id' : datasend}

Categories