0

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.

  • since you are getting alert in success, problem should be with server side. – Anoop Joshi P Mar 13 '14 at 13:24
  • What does the full error say? – MonkeyZeus Mar 13 '14 at 13:25
  • Alert is returning the HTML code of Uredi.php. Also, full error is Undefined index: id in ... on line 36. – NoStupidQuestions Mar 13 '14 at 13:34
  • The undefined index is on line 36 of the PHP code being returned, in other words $_POST['id'] is undefined. – Jay Blanchard Mar 13 '14 at 13:37
  • Your file structure seems to be very strange. Could you properly label the filenames for your functions and HTML? Also, please see this answer for an ideal AJAX setup: http://stackoverflow.com/questions/20150130/ajax-and-php-to-enter-multiple-forms-input-to-database/20150474#20150474 – MonkeyZeus Mar 13 '14 at 14:11

3 Answers3

0

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);
    }
}); 
Charles Harmon
  • 380
  • 2
  • 10
  • Oops. Thank you, I forgot to change that bit. I renamed my variable and used it in the $.ajax. It's still returning Undefined index error though. – NoStupidQuestions Mar 13 '14 at 13:30
  • If it's a PHP error, do a echo '
    ';
        print_r($_POST);
        echo '
    ';
    – Charles Harmon Mar 13 '14 at 13:56
  • What is that supposed to return? – NoStupidQuestions Mar 13 '14 at 14:02
  • If you're getting an undefined index error, you need to see if "id" is actually coming through to the $_POST array. That way, you eliminate any javascript ajax problems and that the data is being passed correctly first. Then, you can narrow down where the error is coming from. – Charles Harmon Mar 13 '14 at 14:03
  • It returned an undefined index error, so I suppose it's javascript/ajax that are causing the problem – NoStupidQuestions Mar 13 '14 at 14:06
  • Did you try print_r($_POST) and not print_r($_POST['id'])? If you did just the post it would come back with undefined error not index error if you weren't posting. What I want you to look at is everyting that the POST array has in it - may be that the structure of the data is not correct - may be $_POST[0]['id'] or something. – Charles Harmon Mar 13 '14 at 18:01
0

Use this;

$.ajax({
    url: 'uredi.php', 
    type: "POST",
    data: "id=" + refId,
    success: function(data){
        alert(data);
    }
});
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
0

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

andrew
  • 9,313
  • 7
  • 30
  • 61