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>
Related
Im using google chrome 65.0.3325.181 on windows 10, xampp is on to run the php. title explains the rest.
html/php:
$(document).ready(function (){
$('#sel_edificio').load('data.php');
$( ".form-control" ).change(function() {
var dato = 50;//document.getElementById("sel_edificio").value;
$.ajax({
method: "POST",
data: {'key': dato},
url: "uno.php",
success: function(status){
var asd = $('#test').load('uno.php');
//document.getElementById("NumEstudiantes").value(key);
}
});
});
});
</script>
uno.php:
<?php
echo $_POST['key'];
?>
error:
Notice: Undefined index: key in C:\xampp\htdocs\jqbd\uno.php on line 2
You are sending request two time, try this:
html/php
$(document).ready(function (){
$('#sel_edificio').load('data.php');
$( ".form-control" ).change(function() {
var dato = 50;//document.getElementById("sel_edificio").value;
$.ajax({
method: "POST",
data: {'key': dato},
url: "uno.php",
success: function(data){
$('#test').html(data);
}
});
});
});
change ajax method to type, try this
$.ajax({
type: "POST",
data: {'key': dato},
dataType: "json",
url: "uno.php",
success: function(status){
//var asd = $('#test').load('uno.php');
$('#test').load('uno.php', { key: dato });//document.getElementById("NumEstudiantes").value(key);
}
});
Here is my ajax code:
$("#to").change(function(){
$.ajax({
type: "POST",
url: "<?php echo base_url();?>/index.php/sales/getPrice",
dataType: "html",
data: {'from' : $('#from').val() , to: $('#to').val()},
success: function(response){
//$(".location").html(response);
}
});
});
I want to use ajax result instead of 40. (below code attached).
e: {
price : 40,
category: 'Economy'
}
You want to use the data the ajax call returned? So you can use it in the success part:
$("#to").change(function(){
$.ajax({
type: "POST",
url: "<?php echo base_url();?>/index.php/sales/getPrice",
dataType: "html",
data: {'from' : $('#from').val() , to: $('#to').val()},
success: function(response){
alert(respone.price);
console.log(response);
}
});
});
Use console.log(response); in the success function, to get the data in the developer console of your browser.
var result = '';
$("#to").change(function(){
$.ajax({
type: "POST",
async: false,
url: "<?php echo base_url();?>/index.php/sales/getPrice",
dataType: "html",
data: {'from' : $('#from').val() , to: $('#to').val()},
success: function(response){
result = response;
}
});
});
Now use that result where you want:
e: {
price : result,
category: 'Economy'
}
After ajax success function, I want to redirect page to comment.php without refreshing main page
$.ajax({
type: "POST",
url: "save_edit.php",
data: { id1: id, comment1: comment },
success: function(data) {
$("#feedback").html(data);
$('#feedback').fadeIn('slow', function() {
$('#feedback').fadeOut(4000);
window.location = "comment.php";
}
});
$.ajax ({
type: "POST",
url: "save_edit.php",
data: { id1: id, comment1: comment },
success: function(data) {
$("#feedback").html(data);
$('#feedback').fadeIn('slow', function() {
$('#feedback').fadeOut(4000);
$.ajax ({
type: "POST",
url: "comment.php",
data: { },
});
}
});
You can not redirect to a page without refreshing. You need to load page into your current page like this;
$.ajax ({
type: "POST",
url: "save_edit.php",
data: { id1: id, comment1: comment },
success: function(data) {
$("#feedback").html(data);
$('#feedback').fadeIn('slow', function() {
$('#feedback').fadeOut(4000);
$("body").load("comment.php");
}
});
comment.php should only contain body part
Use like this:
$.ajax ({
type: "POST",
url: "save_edit.php",
data: { id1: id, comment1: comment },
success: function(data) {
$("#feedback").html(data);
$('#feedback').fadeIn('slow', function() {
$('#feedback').fadeOut(4000);
$.ajax ({
type: "POST",
url: "save_edit.php",
});
}
});
On success event use
window.location.href="your page link";
I want to send the data via ajax to other page. I have isolated the problem. This is the code.
Thank you all for your help..But no effect..
updated code
It worked...
<script>
$(document).ready(function(){
$(".edit").click(function(event) {
event.preventDefault(); //<--- to prevent the default behaviour
var box = 1233;
var size=123;
var itemname=123;
var potency=123;
var quantity=12333;
var dataString ={
'box' :box,
'size':size ,
'itemname':itemname,
'potency':potency,
'quantity':quantity
};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
success: function(data) {
alert(data);
},
error: function(data) {
alert(data);
}
});
});
});
</script>
So I click the link,it navigates, to dd.php which has
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']));
echo $_POST['itemname'];
?>
I get Object Object as alert. What am doing wrong? Pls throw some light here..thanks you..
$(document).ready(function(){
$(".edit").click(function(event) {
event.preventDefault();
var data = {"box":1233,
"size":565,
"itemname":565,
"potency":876,
"quantity":234};
$.ajax({
url: "dd.php",
type: "post",
data: data,
dataType: "json",
success: function(data) {
if(console){
console.log(data);
}
},
error: function(data) {
if(console){
console.log(data);
}
}
});
});
});
few things to consider... you can post data as object..which is clean and easier to use
$(".edit").click(function(event) {
event.preventDefault(); //<--- to prevent the default behaviour
var box = 1233;
....
var dataString ={'box':box,'size':size,'itemname':itemname,'potency':potency,'quantity':quantity};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
dataType: "json", //<--- here this means the response is expected as JSON from the server
success: function(data) {
alert(data.itemcode); //<--here alert itemcode
},
error: function(data) {
alert(data);
}
});
so you need to send the response as json in PHP
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']))
?>
Here you are using querystring as sent in GET request.
If you want to send the data in same form, you can use this with GET request type:
$.ajax({
url: "dd.php"+dataString,
type: "get",
dataType: "json",
success: function(data) {
console.log(data);
alert(data.itemcode);
},
error: function(data) {
alert(data);
}
});
Or for POST request,you will have to put data in json object form, So you can use :
var dataString ={
'box' :box,
'size':size ,
'itemname':itemname,
'potency':potency,
'quantity':quantity
};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
dataType: "json",
success: function(data) {
console.log(data);
alert(data.itemcode);
},
error: function(data) {
alert(data);
}
});
});
And put echo in your php code :
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']))
?>
Javascript alert shows [Object object] for object. You can see response using console.log or can use that key with alert.
For more information, refer jQuery.ajax()
HTML/jQuery:
Friends
<script type="text/javascript">
$(document).ready(function() {
$('a#friends').click(function() {
$.ajax({
type: "POST",
url: "data.php",
data: $('#friends').html(),
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
});
});
</script>
data.php:
<?php
echo $_POST['#friends'];
?>
How do I return this POST value of an id in an anchor tag? The variable is being passed to PHP because I can alert it, but the problem is getting it back.
You need to specify the name of the value you are sending across in your AJAX request. Try this:
$.ajax({
type: "POST",
url: "data.php",
data: { 'friends': $('#friends').html() }, // Note the value is sent in an object with a key of 'friends'
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
<?php
echo $_POST['friends']; // retrieve the 'friends' value
?>
How you are passing the data to PHP,
please use the following code,
Friends
<script type="text/javascript">
$(document).ready(function() {
$('a#friends').click(function() {
$.ajax({
type: "POST",
url: "data.php",
data: {'friends' : $('#friends').html()},
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
});
</script>
<?php
echo $_POST['friends'];
?>
Your syntax is wrong for passing friends value to data.php
Try this
$(document).ready(function() {
$('a#friends').click(function() {
$.ajax({
type: "POST",
url: "data.php",
data: "friends="+$('#friends').html(),
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
});
<?php
echo $_POST['friends'];
?>
First of all you can't send data to the ajax page in this way
data: $('#friends').html(),
A more suitable way would be
data : {'key1':'val1', 'key2':'val2'}
Then on the php page, you can retrieve these values in this fashion
$key1 = $_POST['key1']; // will contain 'val1'
$key2= $_POST['key2']; // will contain 'val2'
alternatively you can use
Friends
<script type="text/javascript">
$(document).ready(function() {
$('a#friends').click(function() {
$.post("data.php",{
friends: $("#friends").html()
},function(data){
$("#questions").html($.trim(data)); // trim to be sure
});
});
});
</script>
and in the php:
<?php
echo $_POST['friends'];
?>
Pass the data variable in data field. For more info see below example
$(document).ready(function() {
$('a#friends').click(function() {
alert("");
$.ajax({
type: "POST",
url: "data.php",
data: "#friends="+$('#friends').html(),
success: function(data) {
alert(data);
$('#questions').html(data);
},
dataType: "HTML"
});
});
});