0

I've got a table of which I'm trying to add inline-edit to, but I'm really new to ajax. I suspect there's something wrong with the dataString of my Ajax code but I can't find it. The code itself works, but I'm having trouble with posting the data.

I'm sorry if this kind of question has been asked before, I'm really stressing about this. If anybody can tell me what to do, I'd appreciate it a lot.

Database

rowid | Person

4     | mike

12    | peterson

PHP

//Every row in the sql table has an row id
<php $id = $row['rowid']; ?>

<tr id="<?php echo $id ?>" class="tredit>
    <td>
        <span id="person_<?php echo $id ?>" class="text"><?php echo $row["Person"] ?></span>
        <input type="text" class="ip" id="person_ip_<?php echo $id ?>" value="<?php echo $row["Person"] ?>">
    </td>
</tr>

Ajax

$(document).ready(function(){
    $(".tredit").click(function(){
        var ID=$(this).attr('id');

        $("#person_"+ID).hide();
        $("#person_ip_"+ID).show();
    }).change(function(){
        var ID=$(this).attr('id');
        var first=$("#person_ip_"+ID).val();
        var dataString = 'id='+ ID +'&person='+first;
        //alert(dataString);
        $("#person_"+ID).html('<img src="load.gif" />');

        if(first.length > 0){
            $.ajax({
                type: "POST",
                url: "post_table.php",
                data: dataString,
                cache: false,
                success: function(html){
                    $("#klant_"+ID).html(first);
                }
            });
        }else{
            alert('Voer iets in');
        }
    });
});

post_table.php

<?php
    $person= $_POST['person'];
    $id = $_POST['id'];
    //This is where I get stuck, I don't know how to extract data from the dataString to insert into an update
    $query = "update people set Person='$person' where id='$id'";
    mysql_query($query, $con);
?>
Vasco
  • 191
  • 1
  • 2
  • 12
  • in php, your `dataString` would be accessed using `$_POST['id']` and `$_POST['person']`. note, you will want to sanitize that data to prevent sql injection. – Sean May 12 '15 at 13:54
  • also, if you are doing a string for data, you need an `&` between your params - `var dataString = 'id='+ ID +'&person='+first;`, just like if it was a url – Sean May 12 '15 at 13:56

1 Answers1

0

About retrieving the data you specified in the post() function, have a look at the jquery official documentation :

https://api.jquery.com/jquery.post/

You'll see there is a format available which allow you to specify names to params :

{ name: "John", time: "2pm" }
Cr3aHal0
  • 809
  • 4
  • 11
  • Should I make `$.post();` for that or merge that somehow with the `$.ajax();`? Also, don't I already collect that data in my `dataString`? – Vasco May 12 '15 at 14:00
  • OPs data string `var dataString = 'id='+ ID +'&person='+first;` is just as valid to use as using an object `data: {id: ID, person: first},` http://api.jquery.com/jquery.ajax/ `data Type: PlainObject or String or Array - Data to be sent to the server. It is converted to a query string, if not already a string.` so in fact your object is converted into a query string just like the OP currently uses. – Sean May 12 '15 at 14:02
  • @Vasco have a look at Sean's answer above about your parameters format – Cr3aHal0 May 12 '15 at 14:04
  • @Cr3aHal0 I read something about that in the documentation too. But how do I parse that data to the post_table.php though? – Vasco May 12 '15 at 14:10
  • @Vasco well, if you send dataString = 'id='+ ID +'&person='+first; with a POST method, then you're likely to access those data with $_POST["id"] and $_POST["person"] which are the parameters provided with your request – Cr3aHal0 May 12 '15 at 14:15
  • @Cr3aHal0 I updated my question, and my code and tried what you guys suggested. But it still doesn't seem to work. – Vasco May 12 '15 at 14:19
  • @Vasco what does it display ? does it send something ? do you manage to receive data ? – Cr3aHal0 May 12 '15 at 14:22
  • @Cr3aHal0 it doesn't do anything at all. I can see it change the text, but when I refresh te page it goes back to normal. There are also no changes in database. Should I include my `config.php` in the `post_table.php` or something like that? – Vasco May 12 '15 at 14:33
  • 1
    well, it's necessary for post_table.php to have access to database access so if there is any file where it's declared, feel free to include it at the beginning ! – Cr3aHal0 May 12 '15 at 14:36
  • @Cr3aHal0 I've read somewhere once that I only need to include the `config.php` the page where I'm working in, in this case `index.php`. Because the `post_table.php` is included with the `ajax script` which is included in the `index.php` and so the circle is finished. I read that there's no reason for a second include. – Vasco May 12 '15 at 14:45
  • 1
    @Vasco I'm really not sure about what you're talking about though. Since it consist on another request, even if you do not reload your page, that mean that your second page does not necessarily knows the current context you're coming from and it does not have any contact with index.php. It likely acts like a standalone file and that's why it needs to have access to config.php. Try to include config.php in it (as the first instruction) and tell us if it works – Cr3aHal0 May 12 '15 at 14:49