$.ajax POST Undefined index - php

Apologies in advance for any mistakes. This is my first post on StackOverflow.
So basically I have a php function. Inside it is a jQuery click function with a variable, into which I'm saving the clicked element's text. I'm trying to send that variable's value to my Uredi.php file, via $.ajax function, to save it into a php variable. However, I'm getting an "Undefined index" error. Help would be greatly appreciated. Thank you.
Here's the code:
PHP function:
public function uredi()
{
?>
<script>
$('.edit').click(function(){
$('.naslov_edit').val($(this).text());
$('#on_top').hide();
var refId = $(this).text();
$.ajax({
url: 'uredi.php',
type: "POST",
data: {id: refId},
success: function(data){
alert(data);
}
});
});
</script>
<?php
}
Uredi.php:
<?php
include 'Funkcije.php';
$uredi = new urejanje();
$uredi->uredi();
$id = $_POST['id'];
echo $id;
?>
Funkcije.php is the file with the uredi() function.
EDIT: Thank you everyone for helping. I figured out the problem. Turns out I just had to clean up my code a bit.

Why are you assigning $(this).text() to var id and then not using it in the $.ajax()? Remember scope and what doe $(this) actually reference within the $ajax()? I would also shy away from naming variables "id" Try:
var refId = $(this).text();
$.ajax({
url: 'uredi.php',
type: "POST",
//dataType: 'json',
data: {id: refId},
success: function(data){
alert(data);
}
});

Use this;
$.ajax({
url: 'uredi.php',
type: "POST",
data: "id=" + refId,
success: function(data){
alert(data);
}
});

perhaps you meant to read the post vars before initializing the class and maybe even pass Id in?
$id = $_POST['id'];
$uredi = new urejanje();
$uredi->uredi($id);
In any case I think You'll need to show us whats inside Funkcije.php

Related

ajax access php page return value

im fairly new to ajax.. but I have this function which takes and order id and query sql from it in a php page. I can set the returned data to a div but what id like to do is set the return from the function to the returned data in html or text form so i can email it later. also any better ways to do this code would be greatly appreciated
<script>
function getInvoice(id){
var request = "driverInvoice.php"+"?oID="+id;
$.ajax({
url: request,
success: function(data){
$("#theDiv").html(data);
}
});
}
</script>
Use data: {oID: id} to send with POST.
function getInvoice(id){
$.ajax({
async: false,
type: "POST", //Method POST, you can edit and use GET
url: 'driverInvoice.php', //URL
data: {oID:id}, //Variables
success: function(data){
$('#theDiv').html(data);
},
error: function(data) {
}
});
}
If I undestend, you want transform data in text, like a strip_tags php function?
...
success: function(data){
$("#theDiv").html(data);
var text = $("#theDiv").text()
}

Using AJAX to pass variable to PHP and retrieve those using AJAX again

I want to pass values to a PHP script so i am using AJAX to pass those, and in the same function I am using another AJAX to retrieve those values.
The problem is that the second AJAX is not retrieving any value from the PHP file. Why is this? How can I store the variable passed on to the PHP script so that the second AJAX can retrieve it?
My code is as follows:
AJAX CODE:
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
PHP CODE:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Use dataType:"json" for json data
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
dataType:'json', // add json datatype to get json
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
Read Docs http://api.jquery.com/jQuery.ajax/
Also in PHP
<?php
$userAnswer = $_POST['name'];
$sql="SELECT * FROM <tablename> where color='".$userAnswer."'" ;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
// for first row only and suppose table having data
echo json_encode($row); // pass array in json_encode
?>
No need to use second ajax function, you can get it back on success inside a function, another issue here is you don't know when the first ajax call finished, then, even if you use SESSION you may not get it within second AJAX call.
SO, I recommend using one AJAX call and get the value with success.
example: in first ajax call
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
alert(data);
//or if the data is JSON
var jdata = jQuery.parseJSON(data);
}
});
$(document).ready(function() {
$("#raaagh").click(function() {
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data) {
console.log(data);
$.ajax({
url:'ajax.php',
data: data,
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
Use like this, first make a ajax call to get data, then your php function will return u the result which u wil get in data and pass that data to the new ajax call
In your PhP file there's going to be a variable called $_REQUEST and it contains an array with all the data send from Javascript to PhP using AJAX.
Try this: var_dump($_REQUEST); and check if you're receiving the values.
you have to pass values with the single quotes
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: '145'}), //variables should be pass like this
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
try it it may work.......

Getting variable jQuery -> PHP (not working)

I'm having problems with passing variables from jQuery into PHP. I search for the solutions on the internet, and i came to the AJAX. Never used Ajax before, so i guess that the problem is known.
So i have the following code in "index.php"
$("#inviteForm").submit(function(e) {
e.preventDefault();
var emailVal = $("#email").val();
$.ajax({
type: "POST",
url: "processAjax.php",
data: {email: emailVal},
success: function(data) {
alert(data);
}
});
});
In form, i have one input box (for email) and a submit button (the method is POST).
In processAjax.php i have the following code
<?php
$x = $_POST['email'];
return $x;
?>
So if i'm correct, if the $.ajax function is OK, the alert box should pop up. But it doesn't.
i've also tried alert(x); but it didn't work.
Any idea what i'm doing wrong
Try echo $x; instead of return $x;
Try this:
<?php
$x = $_POST['email'];
echo $x;
?>
Try this for better data manipulation. Use json_encode from the server side and json datatype to your ajax calls. Then to alert server response, just alert the key of the array like alert(data.value):
$.ajax({
type: "POST",
url: "processAjax.php",
data: {email: emailVal},
dataType: 'json'
success: function(data) {
alert(data.value);
}
processAjax.php
$result['value'] = $_POST['email'];
echo json_encode($result);

getting an error when fetching a json or xml data from a url using jquery-ajax

I have this code:
jQuery(document).ready(function(){
var user_key = "asdflk134";
var app_key = "pigasdb95r91bva";
var params = "app_key="+app_key+"&user_key="+user_key+"&format=xml";
jQuery.ajax({
type: "POST",
url: "http://hellotxt.com/api/method/user.latest",
data: params,
dataType: "xml",
success: function(data){
jQuery(".result").html(data);
}
});
});
When checking on my firebug console, I noticed this error:
XML Parsing Error: no element found Location: moz-nullprincipal:{3d9469e7-683c-41ea-9bd4-c761a0568b30} Line Number 1, Column 1:
^
What do you think is this error? I'm really stuck on this problem. Any help would be greatly appreciated and rewarded! Thanks! :)
try to change the code that you have do it this way:
jQuery(document).ready(function(){
var user_key = "asdflk134";
var app_key = "pigasdb95r91bva";
var params = "app_key="+app_key+"&user_key="+user_key+"&format=xml";
jQuery.ajax({
type: "POST",
url: "http://hellotxt.com/api/method/user.latest",
data: {
"user_key": user_key,
"app_key":app_key,
"format":"xml"},
dataType: "xml",
success: function(data){
jQuery(".result").html(data);
}
});
});
you should submit an object to your data parameter for jquery to read it properly <key>:<value> the key will be taken as the parameter name.

jquery ajax not working with data field

When i submit a jquery ajax request without the data value, it works, when i submit it with the data value, nothing happens. I check if it works using firebug. I think its a simple mistake but i cant seem to figure it out. Please Help.
Here is the Jquery Code
var inputString = $("something").val();
var suggestions = $.ajax({
url: "temp.php",
type: "POST",
data: {valueInput : inputString},
dataType: "html"
});
temp.php just has some simple code since I'm testing:
echo "We got sumn here";
another thing is the suggestions variable is empty, any ideas?
You can try:
data: 'valueInput=' + encodeURIComponent(inputString),
Update
suggestions is being set to the jqXHR object returned from the $.ajax() function. If you want to do work on the server-response then you need to set a success callback somehow. Here are two ways:
var inputString = $("something").val();
$.ajax({
url : "temp.php",
type : "POST",
data : 'valueInput=' + encodeURIComponent(inputString),
dataType : "html",
success : function (serverResponse) {
//you can now do work on the server-response, it's stored in the serverResponse variable
alert(serverResponse);
}
});
OR
var inputString = $("something").val(),
suggestions = $.ajax({
url : "temp.php",
type : "POST",
data : 'valueInput=' + encodeURIComponent(inputString),
dataType : "html"
});
$.when(suggestions).then(function () {
//this is your callback function
});
I suggest the first method, the second is more advanced and is really only helpful if you want to wait for a set of AJAX requests to complete before doing something.
valueInput should be in quotes as it's a name. 'valueInput'
var inputString = $("something").val();
var suggestions = $.ajax({
url: "temp.php",
type: "POST",
data: {'valueInput': inputString},
dataType: "html"
});
You need to pass data in a form of query string. It should be something like a=1&b=2&c=3&d=4&e=5
You can use .serialize() method over jQuery object that has selected form elements or form tag. So, maybe this code shall be helpful.
var suggestions = $.ajax({
url: "temp.php",
type: "POST",
data: $("something").serialize(),
dataType: "html"
});
You can try
data: JSON.stringify({'valueInput': inputString}),
in you data parameter.

Categories