0

I'm trying to call a php function that is defined on a php file.

On the php file I have this that will call the function

if(isset($_GET))
{
    submit();    //Do something with your GET
}

But I'm getting an error when calling the php function within the jquery. I imagine it could be a path problem. The jquery file is in js/file.js and the php function is on data/php/function.php so I try it calling it like this.

       $.ajax({
          url: "../data/php/functions.php&paperid="+$('#table-paperid').text(),
          type: "GET",
          success:function(result)//we got the response
          {
           alert('Successfully called');
          },
          error:function(exception){alert('Exception:'+exception);}
       });

But no matter what I write on the url I always get the exception error. Any ideas?

5
  • 1
    Well firstly paths are relative to the page they're loaded on... Commented May 16, 2016 at 16:50
  • What error specifically are you getting? Commented May 16, 2016 at 16:52
  • 1
    Shouldn't that & in your URL be an ?? Commented May 16, 2016 at 16:53
  • @wogsland simply Exception:[object Object] Commented May 16, 2016 at 17:27
  • Ok so @NiettheDarkAbsol was right all along. I was given the path relative to the js file and not the original loaded page. Now I changed that and it works fine. I will up vote both answers though since they both helped me but non were the actual solution. Commented May 16, 2016 at 17:49

2 Answers 2

2

It's better practice to use a POST ajax call in this case. You should also put the "type" property before the "url" field, although, I doubt this is the source of the error. Try an ajax call that looks something like this.

$.ajax({
      type: 'post',
      url: '/some/path',
      data: { paperid : $('#table-paperid').text()},
      success: function(data) {

        var response = JSON.parse(data);

      }
    })

and your php should be modified to.

if(isset($_POST['paperid']))
{
    submit();    //Do something with your GET
}

I've done this many times with no issue so hopefully this will solve your problem.

Best of luck!

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

6 Comments

Why does in this case is good practice to make a POST Request?
Because of the parameters. It makes more sense to use POST as it is cleaner.
I tried this and I'm still getting the exception error. The alert message is simply Exception:[object Object] so I can't really see what's happening. Could it have to do with the path?
@Atirag change alert(JSON.stringify(exception, null, 4)); instead alert('Exception:'+exception); to see the error
@JoseRojas That was really helpful and, as I suspected, the path is incorrect. I get this "The requested URL /data/php/functions.php was not found on this server". So is it ignoring the "../"??
|
1

You can set the query string this way as well:

 $.ajax({
      url: "../data/php/functions.php",
      type: "GET",
      data: {paperid: $('#table-paperid').text() }
      success:function(result)//we got the response
      {
       alert('Successfully called');
      },
      error:function(exception){alert('Exception:'+exception);}
   });

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.