0

I am having some trouble posting data through AJAX. I created a small save script which determines in which table a POST must be saved (I am using a different form for each table):

$(function() 
{
    $( "#tabs" ).tabs().find( ".ui-tabs-nav" ).sortable({ axis: "x" });
});

function save(target)
{
    switch(target)
    {
        case "praktijk":
            $.ajax({                                                                                                           url: 'webscripts/admin/opslaan.php?type=praktijk',
                type: 'POST',
                data: $("#tabs-2").find('form').serialize(),
                success: function(){
                    alert("gegevens opgeslagen!");
                    $('#popup').dialog('close');
                }
            });
            break;
        case "persoonlijk":
            $.ajax({                                                                                   
                url: 'webscripts/admin/opslaan.php?type=persoonlijk',
                type: 'POST',
                data: $("#tabs-1").find('form').serialize(),
                success: function(){
                    alert("gegevens opgeslagen!");
                    $('#popup').dialog('close');
            }
        });
        break;
    case "vragen":
        $.ajax({
            url: 'webscripts/admin/opslaan.php?type=vragen',
            type: 'POST',
            data: $('#tabs-3').find('form').serialize(),
            success: function(){
                    alert("gegevens opgeslagen!");
                    $('#popup').dialog('close');
            }
        });
        break;
}

}

as you can see, I next determine it for the PHP script by using a GET variable that says which table it needs to save it in. But this doesn't work. The script appears to be crashing at this point. I am not sure where exactly it crashes.. the Firebug terminal doesn't show any obvious errors.

Does somebody know why it doesn't work?

4
  • do you mean to say you are not getting "type" in opslaan.php ? Commented Aug 22, 2012 at 11:14
  • how you are calling the save function??? Commented Aug 22, 2012 at 11:28
  • I am calling it via a button with an onClick event. So for example I do this for the persoonlijk (personal) table: <button onClick='opslaan("persoonlijk")' /> Commented Aug 22, 2012 at 11:36
  • you don't need javascript at all... change the button to <button type="submit">opslaan</button> and let your php script do the rest Commented Aug 23, 2012 at 11:12

2 Answers 2

1

First of all, if you're using a different form for each table, why not send the right table name ('persoonlijk', 'vragen', etc.) with the form itself?

example:

<form ...>
<input type="hidden" name="target" value="persoonlijk" />
...
</form>

And in php:

<?php
if($_POST['target'] == 'persoonlijk') {
    // save in table 'persoonlijk'
} else {
   // save it somewhere else
}
?>

Furthermore, reading out a php var can not break your javascript, simply because you are working on the server, and javascript works in the browser. Please print out the get en post vars using print_r($_GET);, then explain what 'seems to break' means (what happens, what do you see (error messages and such)).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response! I added the input type hidden in the POST values and removed that GET parameter altogether. I tried to add the print_r but that doesn't show up.. in the firebug console the line turns red as soon as I click the save button, so that's why I think it crashes somehow
Please be more precise with your replies. Which line turned red in the console? Is there an error message in the console, and what does it say?
1

data:should contain some data. It should be a text or value and your code doesn't seems to have it correctly set up. I recommend you to use some var there.

1 Comment

The post is constructed the right way. Firebug shows that the POST data is at least contains the right value.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.