How to fix this undefined index error? Ajax to PHP - javascript

Notice: Undefined index: query in C:\xampp\htdocs\Java\Search\instant-search.php on line 71
{"names":[]}
This is my Php
$query=$_POST["query"];
$matchType=isset($_POST["match_type"])? $_POST["match_type"]:MatchType::CONTAINS;
processRequest($query,$matchType);
Here is my ajax script
$("#query").keyup(function(){
var q=$(this).val();
var match_type=$("input[type=radio]:checked").val();
var data={'query':q,'match_type':match_type};
if(q.length==0){
$("#results").html("");
return false;
$.ajax({
url:"/Java/Search/instant-search.php",
data:data,
type:"post",
dataType:"json",
success:function(res) {
var tmpl=$("#names_tmpl").html();
var html=Mustache.to_html(tmpl,res);
$("#results").html(html);
}
});

As per your comments if both of them match_type and q are getting values than use ajax with data as:
$.ajax({
url:"/Java/Search/instant-search.php",
data: "query="+q+"&match_type="+match_type ,
type:"post",
dataType:"json",
success:function(res) {
var tmpl=$("#names_tmpl").html();
var html=Mustache.to_html(tmpl,res);
$("#results").html(html);
}
});

To change the type of request use method property not type and use Uppercase string POST jQuery.ajax:
$.ajax({
url: "/Java/Search/instant-search.php",
data: data,
method: "POST",
dataType: "json",
success: function(res) {
var tmpl=$("#names_tmpl").html();
var html=Mustache.to_html(tmpl,res);
$("#results").html(html);
}
});

Related

I don't know if I use button id jquery ajax serialize right, but doesn't work

$('button.red-button').on('click', function(form)
{
form.preventDefault();
var obj = {};
obj.id = $(this).attr('id');
//var id['id'] = $(this).attr('id');
alert(JSON.stringify(obj));
$.ajax(
{
type: 'POST',
dataType: 'json',
url: 'delete-comment',
data: JSON.stringify(obj),
success: function(data)
{
alert(data);
},
error: function(data)
{
alert(0);
}
});
});
This is what I made.
For example we have this button
<button id="4" class="red-button">Usuń</button>
This script will alert first
{"id":"4"}
So it alerts it's ID in JSON format.
This is what i wanted, to delete this item by doing AJAX POST request.
But in PHP by doing
echo json_encode($this->input->post('id'));
I got NULL in second alert.
or
echo json_encode($_POST['id']);
and i got 0 in second alert
I'm doing something wrong but I don't know...
Maybe this json is send wrongly, or json is build wrongly idk
$('button.red-button').on('click', function(form)
{
form.preventDefault();
var obj = {};
obj.id = $(this).attr('id');
//var id['id'] = $(this).attr('id');
alert(JSON.stringify(obj));
$.ajax(
{
type: 'POST',
dataType: 'json',
url: 'delete-comment',
data: obj,
success: function(data)
{
alert(data);
},
error: function(data)
{
alert(0);
}
});
});
Use it like that and it will work, without stringify the object

Call JSON inside AJAX success

I want to call a json data inside my AJAX success. I'm still new on manipulating json and AJAX. Can somebody help me on how to access my json data which is from another URL? And I want to compare the id to the JSON data. Here's my code so far:
function getCard(id){
$.ajax({
type: "GET",
data: "id=" + id,
success: function(data){
#call JSON data here from another URL
# Is it possible to call another AJAX here?
}
});
}
This code will work for you
$.ajax({
url: "url",
method: "post",
data: "id=" + id,
success:function(data) {
// success goes here
$.ajax({
url: "url",
async:false,
method: "post",
data: "id=" + id,
success:function(json) {
JSON.parse(json);
// compare data and json here
},
error: function(){
// error code goes here
}
});
},
error: function(){
// error code goes here
}
});
yes Zuma you can call another Ajax inside Success function of Ajax call. Below is the example:
$.ajax({
type: "post",
url: url1,
data: data1,
success: function(data){
$.ajax({
type: "post",
url: url2,
data: data2,
success: function(data){
});
});
});
function getCard(id){
$.ajax({
type: "Get",
url: "发送请求的地址",
data: "id=" + id,
success: function(data){
#call JSON data here from another URL
# if success,you can call another AJAX.
# Is it possible to call another AJAX here?
# yes!
}
});
}

Retrieve POST JSON response

I'm writing my first Ajax request, on a Groovy/Grails platform.
var newDataB = $.ajax({
method:'post',
url: url,
async: false,
data: {source:"${source}"},
success: function (response) {
jsonData = response;
var res = JSON.parse(jsonData);
alert(res);//
}
});
Here is the response of my controller "url"
def result = [ value: 'ok' ]
render result as JSON
But it does not work and i get an error message in my browser
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
var res = JSON.parse(jsonData);
I don't understand , the response seems to be a nice formatted JSON ?
EDIT i did a print as Paul suggests :
If i execute
var newDataB = $.ajax({
method:'post',
url: url,
async: false,
dataType: 'json',
data: {source:"${source}"},
success: function (response) {
console.log(response)
console.log(response.value)
jsonData = response;
}
});
The first print is :
Object { value="ok"}
The second print is
ok
If i want to get the result, how is the proper way ?
Do i have to assign the value inside the statement "success: function (response) { "
doing something like
var result
var newDataB = $.ajax({
method:'post',
url: url,
async: false,
dataType: 'json',
data: {source:"${source}"},
success: function (response) {
result = response.value
}
});
console.log("result : "+result);
This code works for me !!
Or perhaps there is a way to get the result, doing something like
var newDataB = $.ajax({
method:'post',
url: url,
async: false,
dataType: 'json',
data: {source:"${source}"},
success: function (response) {
}
});
var result = newDataB.response.somethingblablabla
or
var result = OneFunction(newDataB.response)
??????
You can make object stringified before passing it to parse function by simply using JSON.stringify().
var newDataB = $.ajax({
method: 'post',
url: "${createLink(controller: 'util',action: 'test')}",
async: false,
data: {source: "abc"},
success: function (response) {
jsonData = response;
var res = JSON.parse(JSON.stringify(jsonData));
console.log(res);//
}
});
Hopefully this may help.
You shouldn't need to parse it, if your server is providing json.
You can use dataType though to force jQuery to use a particular type:
var newDataB = $.ajax({
method:'post',
url: url,
async: false,
dataType: 'json',
data: {source:"${source}"},
success: function (response) {
console.log(response);
}
});

My ajax code is not send value to other php page?

My ajax code is not send value to other php page??
I want to delete value coming from database ajax code get id that come from database but not sent to other php page where delete code.
<script type="text/javascript">
function deleteBox(id){
if (confirm("Are you sure you want to delete this record?")){
var dataString = id;
$.ajax({
type: "POST",
url: "del.php",
data: dataString,
cache: false,
success: function(){
}
});
}
}
</script>
try below code:
var dataString = id;
$.ajax({
type: "POST",
url: "del.php?datastring="+id,
//data: dataString,
cache: false,
success: function(){
}
});
or
var dataString = id;
$.ajax({
type: "POST",
url: "del.php",
data: {datastring:id},
cache: false,
success: function(){
}
});
send data with parameter name and value not only value, like this
data: {'id':dataString},
please correct this in ajax call. you can pass the data from one page to another page using data. like data : {var1:value1,var2:value2,var3:value3}
$.ajax({
type: "POST",
url: "del.php",
data: {dataString: dataString},
cache: false,
success: function(){
}
});

jquery function problem

I have this function
function onclickRowRecord(recordID) {
$.ajax({
type: "POST",
url: '/file/to/post.to.php' ,
data: {recordID:recordID},
success: function(data) {
//how to post this to function howToPost(recordID) the recordID
}
});
}
function howToPost(recordID){
alert(recordID);
}
so how can I get the response from ajax success and post to the other function
If post you mean call the hotToPost function, then
...
success: function(data) {
howToPost(data.recordID); //or whatever
}
....
Hope this helps.
$.ajax({
type: "POST", url: '/file/to/post.to.php' , data: {recordID:recordID}, success: function(data) {
alert(recordID)
}
});
Does that work? Kinda new to jQuery

Categories