2

Hopefully I can ask this correctly cuz I know what I want it to do but can't seem to find any answers from searching.

I have a func.php page where I have all my functions and I want ajax to use one function from that page.

func.php

function toptable()
{ 
  echo"something happens in here";
}

index.php

<?php include 'func.php'; ?>
<script type="text/javascript">
function check_username() {
  uname=document.getElementById("username").value;
  var params = "user_id="+uname;
  var url = "topoftable()";
  $.ajax({
      type: 'POST',
      url: url,
      dataType: 'html',
      data: params,
      beforeSend: function() {
        document.getElementById("right").innerHTML= 'checking'  ;
      },
      complete: function() {

      },
      success: function(html) {
        document.getElementById("right").innerHTML= html ;
      }
  });

}

</script>

Make sense?

3
  • Is your Top Table function a PHP or Javascript function? You are including the function outside of your <script type="text/javascript">. Commented Nov 8, 2013 at 21:53
  • possible duplicate of Using JQuery Ajax to call a php function Commented Nov 8, 2013 at 21:53
  • not 100% clear what you are asking Commented Nov 8, 2013 at 22:01

3 Answers 3

11

It's not working like that.

Your AJAX request should look something like this:

$(document).ready(function() {

    //some even that will run ajax request - for example click on a button

    var uname = $('#username').val();
    $.ajax({
        type: 'POST',
        url: 'func.php', //this should be url to your PHP file
        dataType: 'html',
        data: {func: 'toptable', user_id: uname},
        beforeSend: function() {
            $('#right').html('checking');
        },
        complete: function() {},
        success: function(html) {
            $('#right').html(html);
        }
    });

});

And your func.php:

function toptable()
{
  echo 'something happens in here';
}

//here you can do some "routing"
$func = $_POST['func']; //remember to escape it

switch ($func) {
    case 'toptable':
        toptable();
        break;
    default:
        //function not found, error or something
        break;
}

Also check that I change document.getElementById to jQuery selector $('#...').

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

Comments

0

PHP is server side, JS is client side. You need to call the PHP script in the ajax request, not the function.

Something like

[...]
 var url = "func.php";
 $.ajax({
       type: 'POST',
       url: url,
[...]

func.php

<?php
function toptable()
{ 
  echo "something happens in here";
}
toptable();
?>

In this case, the html argument in the success handler of the ajax request will be equal to "something happens in here".

Comments

0

Make sense? - no. You can't mix PHP (a server-side technology) with Javascript (client-side). You can use AJAX to ask a server to perform some function for you, but not by using the function name as a URL. You should create a PHP page to accept your AJAX request, perform the function and return the result, and use the URL of that page in your AJAX request

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.