最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - using ajax url to call function - Stack Overflow

programmeradmin4浏览0评论

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'  ;
      },
      plete: function() {

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

}

</script>

Make sense?

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'  ;
      },
      plete: function() {

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

}

</script>

Make sense?

Share Improve this question edited Nov 8, 2013 at 21:59 Daniel Wärnå 7888 silver badges18 bronze badges asked Nov 8, 2013 at 21:50 Steven VanerpSteven Vanerp 391 gold badge1 silver badge7 bronze badges 3
  • Is your Top Table function a PHP or Javascript function? You are including the function outside of your <script type="text/javascript">. – Ryan Commented Nov 8, 2013 at 21:53
  • possible duplicate of Using JQuery Ajax to call a php function – user1864610 Commented Nov 8, 2013 at 21:53
  • not 100% clear what you are asking – charlietfl Commented Nov 8, 2013 at 22:01
Add a ment  | 

3 Answers 3

Reset to default 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');
        },
        plete: 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 $('#...').

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".

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

发布评论

评论列表(0)

  1. 暂无评论