0

I've made a script that use an ajax request, this script basically is activated when an user press a button in the index.php page, like:

Risorse.php

<form  method='post'>
        Name <input type='text' name='nome' id='nome'>
        Col <input type='text' name='colore' id='colore'>
        Plan <select name='planning' id='planning'><option value='p1'>p1</option></select>
        <button type='submit' id='Aggiungi'>Register</button>
</form>

and this is the script:

Index.php

<script>
  $("#Aggiungi").click(function(e){
    e.preventDefault();
    var nome = $("#nome").val();
    var colore = $("#colore").val();
    var planning = $("#planning").val();
    $.ajax({
        url: "Class/Risorse.php",
        data: "function=test", 
        /*type: "POST",
        url: "Class/Risorse.php",
        data: 
        {  
            nome: nome,
            colore: colore,
            planning: planning
        }*/
    })
    .done(function( msg ) {
        //alert("Success");

    })
        .fail(function() {
        alert('Problem');
    });
});

Risorse.php

in the class Risorse.php I've create the test(); function

function test()
{
    echo "inserimento avvenuto con successo!";
}

update- code that update which function is called by ajax:

if(isset($_GET['function'])) 
{
    if($_GET['function'] == 'test') 
    {   
        test(); //but this function isn't called and is located in the same file
    }
}

but nothing happean, also ajax working good if I execute the commented code. What am i doing wrong?

13
  • What do you mean by "but nothing happen"? You commented out the alert(success) part, how would you tell when the response has returned. Commented Oct 2, 2015 at 14:27
  • I mean the test function isn't called Commented Oct 2, 2015 at 14:29
  • You know that you can't call directly a php function from js... right? Commented Oct 2, 2015 at 14:29
  • yeah. Infact I want to call the function test by an ajax request, am I wrong on this? Commented Oct 2, 2015 at 14:32
  • How do you know the "test function isn't called"?, like I said earlier, you commented out the alert(success) part, which is supposed to let you know when your request/response have finished processing. Try to log() to the console the returned value, to see if it corresponds with the returned value from the server side function. Commented Oct 2, 2015 at 14:36

1 Answer 1

2

You have data declared twice in your $.ajax call. Is the php test function located in Class/Risorse.php? If so, your php will not automatically enter this function, but it will run the php file. Add test();to the top of that php file to get it to enter the funciton.

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

10 Comments

I need to execute the test function inside the ajax request. If I place test(); to the top as you said all working, but I need to call it when my ajax request is performed
url: "Class/Risorse.php" will get the server to run the script Risorse.php, but there's nothing you can do about telling ajax specifically to run test(). That is why you'd want to put it at the top of the php file, or omit the function and have the php script only contain echo "inserimento avvenuto con successo!";
Nope, I've update my question with the code that check which function was passed!
Try data: {function: 'test'} instead of data: "function=test"
Have you tried checking the developer console and seeing what the GET request looks like, if it's returning an HTTP error codes or anything? In Chrome developer tools for example, it should have a GET request for Class/Risorse.php?function=test If that 404's or anything like that, you might need to modify the url slightly
|

Your Answer

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