0

Ok, so my Ajax call looks like this:

var poststring = "id_Client=" + id_client + "&id_File=" + id_file;
        alert(poststring);
        $.ajax({
            type: "POST",
            url: "addclpermission.php",
            data: poststring,
            error: function(xhr, textStatus, errorThrown){
                alert("Error: " +textStatus)
            }
        });

Everything works fine until the $.ajax(). If I use alert(poststring) the output looks like this:

id_Client=7&id_File=32

Using firebug, I found out that the url "addclpermission.php" is actually requested, but then 'aborted'. The path is correct though, if I copy the url out of firebug and call it directly, no error is displayed. The alert in the 'error' option returns "Error: error"

The file addclpermission.php:

<?php
require_once("../allgemein/includes/dbconnect.php");
$id_File = $_POST['id_File'];
$id_Client = $_POST['id_Client'];
$sql = "INSERT INTO permission (id_File,id_Client) VALUES (".$id_File.",".$id_Client.")";
mysql_query($sql);
?>

I'm pretty sure this code once worked and that I haven't changed that much.

Any ideas?

Thanks!

Edit: I don't think that the error is in the php script, I have multiple ajax calls to several php scripts, but all of them fail the same way.

Edit 2: Now it works! Well, at least half of it. The request is still aborted, but the data gets inserted in the database. But as I said, this isn't the only ajax call and the others still aren't working, and this one is aborted. So I'd really like to know what caused this error and how I can fix it for good. Thanks!

7
  • 1
    Your code is vulnerable to sql injections - use php.net/manual/de/function.mysql-real-escape-string.php Commented Nov 11, 2010 at 14:28
  • What is the error you get? Also, alert("Error: " +textStatus) is missing a semi-colon (;) at the end. Commented Nov 11, 2010 at 14:30
  • @Ghommey: I know, I trimmed the code to the relevant part. Commented Nov 11, 2010 at 14:33
  • @stealthyninja: Thanks for the tip with the semi-colon. However, it didn't change anything. I don't get any other error than the one that I display using alert(). Commented Nov 11, 2010 at 14:35
  • You have to use the xhr.responseText instead of textStatus stackoverflow.com/questions/1637019/… Commented Nov 11, 2010 at 14:35

5 Answers 5

1

First, I would try just requesting addclpermission.php in the browser and see what happens.

Then, if that works, what if you just make addclpermission.php contain some text, no PHP content at all. Then for each stage that works, gradually add content (so first the include, for example).

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

2 Comments

Thanks for your answer. Requesting the script works perfectly. I tried echoing "hello", which works on its own, but not via ajax. Same applies for no content at all or just html content.
So does the ajax request work if you don't put any PHP content in the php file? Its a process of elimination to work out what's causing the problem.
1

I think the error could be in dbonnect.php or addclpermission.php. Save this in addclpermission.php (make a backup of your current file) and browse to it directly:

<?php
require_once("../allgemein/includes/dbconnect.php");
$id_File = 1;
$id_Client = 1;
$sql = "INSERT INTO permission (id_File,id_Client) VALUES (".$id_File.",".$id_Client.")";
mysql_query($sql);
?>

Please let us know if it works or if you get an error.

1 Comment

Thanks, it the request is still aborted. The query however wouldn't work because of foreign keys, but I will try it using values that won't cause a mysql error.
1

When I do jQuery Ajax, I set the data as a Javascript object that jQuery then serializes. Do you have better luck if you provide data: property as an object like this:

data: {
    id_Client: id_client,
    id_File: id_file
}

1 Comment

Nothing changed, but then I'm never lucky when it's about anything code related :)
1

I am pretty sure your problem is that you are not returning an expected dataType to the .ajax call, if you explicity set the datatype (json or text for example):

$.ajax({
        type: "POST",
        url: "addclpermission.php",
        data: poststring,
        dataType: "json",
        error: function(xhr, textStatus, errorThrown){
            alert("Error: " +textStatus)
        }
    });

Then just echo out the expected datatype, just so the server responds, then ajax will know the request was successful.

<?php
// if your dataType is json
echo json_encode(true);

// if your dataType is text
echo ' ';

// exit so the server can return the request
exit;

Comments

0

problem is a --> require_once

require_once("../allgemein/includes/dbconnect.php");

remove this line in a php and write all code here

but I don't know why ?

Comments

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.