admin管理员组

文章数量:1294475

Ok so trying to get a page together that needs to connect to our SQL database. So on the javascript page I have functions that will autoplete a textbox with data out of our mysql DB and then I need to send it to other functions and classes so that it will then look in our SQL DB and return some data. The problem I have is trying to get the .GET call to call in the php page, with the function that calls the class in which I need to get into for the SQL call. I have it setup somewhat but trying to figure out how to send the data through with it as well as just get clarification on how to work the .GET function.

Javascript page:

$.get("dsl_validate.php", calldsl(job));

Php Page

function calldsl($job){
    var $dsljob = $job
    hasfunctioncode($dsljob);
}

The hasfunctioncode function is in my DSL class page that will return the info I need. Any help on if I am in the right direction or not?

Ok so trying to get a page together that needs to connect to our SQL database. So on the javascript page I have functions that will autoplete a textbox with data out of our mysql DB and then I need to send it to other functions and classes so that it will then look in our SQL DB and return some data. The problem I have is trying to get the .GET call to call in the php page, with the function that calls the class in which I need to get into for the SQL call. I have it setup somewhat but trying to figure out how to send the data through with it as well as just get clarification on how to work the .GET function.

Javascript page:

$.get("dsl_validate.php", calldsl(job));

Php Page

function calldsl($job){
    var $dsljob = $job
    hasfunctioncode($dsljob);
}

The hasfunctioncode function is in my DSL class page that will return the info I need. Any help on if I am in the right direction or not?

Share Improve this question edited May 23, 2011 at 20:22 spanky 1,4992 gold badges12 silver badges22 bronze badges asked May 23, 2011 at 20:11 Mike JonesMike Jones 6091 gold badge10 silver badges15 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

It looks like you're trying to physically call the PHP function calldsl() from the JavaScript. This... isn't right. (I'm assuming the $.get() you're using is from jQuery, please correct me if that assumption is incorrect.)

What $.get() does is simply call a resource on the web server. It doesn't have any knowledge of the server-side code (nor should it, for a number of reasons). From the perspective of the server-side code, there's no difference between a page being called via $.get() vs. one that's just loaded in a web browser.

What you essentially need to do is create a PHP page which accepts arguments either as a form post or query string (if you're using $.get() then the query string is the way to go), does its server-side logic, and then simply outputs the results to the "page." In the case of calling the page via AJAX as you are here, it's a good idea to render the page content using JSON notation. (Don't forget to set the content-type header to "application/json" as well.)

Then what you're getting on the client-side from the $.get() call is the response body, which would be that JSON data. It's really just a "page" like any other, the only difference is the content-type telling the browser that it's JSON data and that it doesn't have HTML, just JavaScript objects. The success callback on the $.get() call (the function you pass it, or create in-line) would receive that response data as an argument and can do what you need to with it.

The way I understand jQuery.get(), the second argument is the "callback" (http://api.jquery./jQuery.get/). The callback will hand the results from your server therefore should be a function. Currently your code actually executes the function "calldsl" where you should be only passing a reference like so...

Javascript:

$.get("dsl_validate.php", function(response){
  alert("yay I haz ajax! "+response)
});

PHP: "dsl_validate.php"

echo "this is some data from the server";

No, your are not in the right direction. The first parameter of the get method have to br the plete URL of the page, not just the script (this works if the script resides on the same directory of the javascript file, though). The .php file shall return somehting "usable" for you javascript (JSON, or HTML, or text, or... whatever). The "calldsl" function will be called AFTER the data has been returned from the call. Something like that:

$.get('dsl_validate.php?value=somevalue', function(data) {
  alert("Data returned from dsl_Validate: " + data)
});

i think you are better off passing the function as a param to your php page

$.get("dsl_validate.php?calldsl="+job, function(data) {
  $response = $(data);// create a jquery object from the response
});

`

and in your php file create a switch statement that call the function based on the parameter

Mmm I think you are wrong, the second argument on your get function is the javascript function that will process de data returned by "dsl_validate.php". I mean, if that page returns "foo", job will contain "foo".

But in my experience is better to use the autoplete plugin from Jquery UI

jquery autoplete plugin

本文标签: phpJQuery GET callfunctionStack Overflow