0

Im fairly new to php and ajax. I am trying to pass along a variable from jquery to a php page that is loaded up in a modal window.

I have php returning the table information for a bunch of users. Next to each users name, I have an edit button. I have jquery grabbing the data-id parameter of that button.

$(document).ready(function() {
$('.editButton').click(function(e){
e.preventDefault();
    var getid = $(this).data('id');

Now I have a php form script that I want to load in a modal window when the "edit" button is clicked. The only way to do that is to make the button that opens the modal window an a tag and use href to call the php script. I want to pass along the variable "getid" to the php script that will use the value to run a query against the database to find the user that was clicked(the data-id parameter is generated by another php script which returns the id of that user). I tried many ways to send the ajax information to the external php script but no matter what I do, cant get it to work. Here is my ajax request:

        $.ajax ({
        url: '/extschedule.php',
        data: {getid : getid},
        type: 'POST',
        success: function(data) {
        alert('success');
        }           
}).error(function() {
Alert('Error');

Here is the php code on extschedule.php:

$connection = mysqli_connect(stuff);

            $idVar = $_POST['getid'];

            $sqledit = "SELECT * from database WHERE id = (".$idVar.")";
            $result = $connection->query($sqledit);


            while($row = $result->fetch_array(MYSQLI_ASSOC)){
                $name = $row['name'];
                $id = $row['id'];
                $team = $row['team'];
                $language = $row['language'];
                $dedicatedHotel = $row['ded_hotel'];
                $satStart = $row['Sat_start_time'];
                $satEnd = $row['Sat_end_time'];..............yadda yadda

I tried using if(isset($_POST['getid']))) but that causes a ton of issues on the php side. Ive searched around a while but still cannot get to any resolution. Can someone help me with this? Thanks in advance!

18
  • 1
    Have you checked the console for errors with the request? Jan 26, 2015 at 15:20
  • 1
    you are vulnerable to sql injection attacks, and need to do some basic debugging: Have you checked if getid ever gets set properly in your JS code? Did you look at what's coming across in $_POST?
    – Marc B
    Jan 26, 2015 at 15:20
  • 1
    "I tried using if(isset($_POST['getid']))) but that causes a ton of issues on the php side." - What errors did this cause? The code should have been wrapped in the if block to stop further execution if getid is not defined. Then optionally, log the error or handle a case where getid is not defined.
    – War10ck
    Jan 26, 2015 at 15:22
  • @ Rory - I have checked the console and I see XHR request, I dont see any errors
    – user247326
    Jan 26, 2015 at 15:22
  • @marc In troubleshooting earlier, I had jquery printing out the variable's value in the console and it was correct
    – user247326
    Jan 26, 2015 at 15:24

2 Answers 2

0

I don't see you echo data when query is success. Can you try it again with: echo "bla bla bla"; in file php.

1
  • the echo is very long..its basically returning values for every user that are in the database. My situation was confusing enough and didnt want to add additional junk here. The call works under certain situations but not the way I want it to.
    – user247326
    Jan 26, 2015 at 15:44
0

I've had some issues several times with this going between PHP versions. I thought I was going insane, and this post - Sending JSON to PHP using ajax explains things a bit better.

  1. In my exp. posting $.ajax messages to PHP evaluating $_POST like that requires you to JSON.stringify. I'll typically set dataType = 'text' in this case, and then things start working. But the above post actually claims you don't have to do that. But I've switched things back and forth a number of times until I could get it to resolve. (I even had cases where things were double escaped. Bug in PHP 5.2.8 as I recall).

  2. You could switch to dataType = 'json', but on the PHP side use file_get_contents('php://input'). That will accept a raw post of application/json (content type).

    $.ajax({
        type: "POST",
        url: 'yourserviceorscript.php',
        contentType: 'application/json; charset=utf-8', // (optional) text/plain; charset=utf-8
        dataType: 'json', // or text
        async: true, // or false
        data: JSON.stringify(data_obj), // obj gets urlencoded
        cache: false,
        success: function(obj) {},
        error: function(xhr, desc, exeptionobj){}
    });
    

What I've noticed is if you don't set the contentType properly it uses the default of 'application/x-www-form-urlencoded; charset=UTF-8' too.

Bottom line though, regardless of what I've seen people use in examples - when they put a raw object vs data: JSON.stringify(data_object), I have not been able to get it to work. PHP side states the original post without stringify looks like "data%5Baction%5D=Server_Checkup", and doesn't resolve. It gets url encoded unless you stringify. See also - How can I use JQuery to post JSON data?

There are a lot of posts on this subject.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.