user5401691
user5401691

Reputation:

Call specific php function through ajax don't working

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?

Upvotes: 0

Views: 71

Answers (1)

neilsimp1
neilsimp1

Reputation: 1259

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.

Upvotes: 2

Related Questions