www.webdeveloper.com
Results 1 to 8 of 8

Thread: could I make a jquery ajax call in a php file

  1. #1
    Join Date
    Dec 2013
    Posts
    57

    could I make a jquery ajax call in a php file

    I would like to know if possible how could I make an ajax call with this.

    This would be a function in a .js file
    Code:
    function Transactions()
    {
    	$.ajax({
    		type: "POST",
    		url: "/php/query/transactions.php",
    		dataType: "json",
    		data: ({}),
    		success: "",
    		error: ""
    	});
    }
    in my "something.php" file how would I go about calling that function. Here is an example to give you an idea of what I'm trying to do.

    PHP Code:
    <?php
    include 'something.js';

    if (isset(
    $_POST['submit'])) {
    transactions()
    }

    ?>
    If this was a function from a .php I would instantiate the class then call the function but how would I be able to do that with a javascript function? Also, is that correct for me to include the .js file?

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    You can use fsockopen(), fread(), fwrite(), etc. or sockets extension. You will need to make a wrapper class or functions to handle the HTTP transport. Then there is the cURL extension... which automatically handles the HTTP transport, but you have to use a multitude of settings functions for requests.

    PHP and JavaScript are two different languages. They can, in a way, talk to one another (thru Ajax). But they cannot run code or functions declared in the other.

  3. #3
    Join Date
    Dec 2013
    Posts
    57
    I think my question was sort of confusing. I was asking if I could make an jquery ajax call within a .php file not making the call with a php function.

    would I be able to do something like this?

    Code:
    <!DOCTYPE html>
    <html lang="en">
      <head>
    <title></title>
      </head>
    
      <body>
    
        <div class="container">
    
          <form method="post" action="">
            <h2>Please sign in</h2>
            <input type="text" class="form-control" name="user_name" placeholder="Email address" value="">
            <input type="password" class="form-control" name="user_password" placeholder="Password">
            <button name="submit" type="submit">Sign in</button>
          </form>
    <?php 
    
    
    if(isset($_POST['submit'])) { ?>
    <script language="javascript" type="text/javascript">
    function Transactions()
    {
    	$.ajax({
    		type: "POST",
    		url: "/php/query/transactions.php",
    		dataType: "json",
    		data: ({}),
    		success: "",
    		error: ""
    	});
    }
    </script>
    <?php } ?>
      </body>
    </html>

  4. #4
    Join Date
    Dec 2013
    Posts
    57
    can someone help me? is that possible?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    Just to make sure we're on the same page here:

    PHP: runs on the server, which outputs HTML (or whatever) the browser

    JavaScript: runs in the browser, if what the server (PHP) sent to the it includes some (valid) JavaScript.

    So, PHP (normally) will not run JavaScript, but it can output JavaScript (just like it can output HTML, XML, plain text, whatever) that the browser can then process. You might therefore do something like this:

    example.php:
    PHP Code:
    <!DOCTYPE html>
    <html>
        <head>
            <title>Example</title>
        </head>
        <body>
            <h1>Hello, World!</h1>
    <?php if(isset($_POST['submit'])): ?>
            <script src="something.js" type="text/javascript"></script>
            <script type="text/javascript">
                transactions();
            </script>
    <?php endif; ?>
            <p>Goodbye, cruel world.</p>
        </body>
    </html>
    But if the objective is to have the PHP script actually do whatever it is that the AJAX function does while still processing on the server, then you're looking at directly calling the target of the AJAX request directly (/php/query/transactions.php in this case, possibly by include()ing directly, or else via HTTP such as with the cURL extension for PHP.

    But all that's theoretical at the point until at least I better understand exactly what needs to happen.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Dec 2013
    Posts
    57
    OK. so would this code work?

    example.php:
    Code:
    <!DOCTYPE html>
    <html lang="en">
      <head>
    <title></title>
      </head>
    
      <body>
    
        <div class="container">
    
          <form method="post" action="">
            <h2>Please sign in</h2>
            <input type="text" class="form-control" name="user_name" placeholder="Email address" value="">
            <input type="password" class="form-control" name="user_password" placeholder="Password">
            <button name="submit" type="submit">Sign in</button>
          </form>
    <?php 
    
    
    if(isset($_POST['submit'])) { ?>
    <script language="javascript" type="text/javascript">
    function Transactions()
    {
    	$.ajax({
    		type: "POST",
    		url: "/php/query/transactions.php",
    		dataType: "json",
    		data: ({}),
    		success: "",
    		error: ""
    	});
    }
    </script>
    <?php } ?>
      </body>
    </html>

  7. #7
    Join Date
    Apr 2009
    Location
    Leeds, England
    Posts
    101
    No; that would not work. (If you tried it, you'd find out much quicker than asking)

    This should work, but it is pretty pointless really :

    Code:
    <!DOCTYPE html>
    <html lang="en">
      <head>
    <title></title>
    <script language="javascript" type="text/javascript">
    function Transactions()
    {
    	$.ajax({
    		type: "POST",
    		url: "/php/query/transactions.php",
    		dataType: "json",
    		data: ({}),
    		success: "",
    		error: ""
    	});
    }
    <?php 
    if(isset($_POST['submit'])) {
    echo 'Transactions();';
    ?>
    </script>
      </head>
    
      <body>
    
        <div class="container">
    
          <form method="post" action="">
            <h2>Please sign in</h2>
            <input type="text" class="form-control" name="user_name" placeholder="Email address" value="">
            <input type="password" class="form-control" name="user_password" placeholder="Password">
            <button name="submit" type="submit">Sign in</button>
          </form>
      </body>
    </html>
    [/QUOTE]

    However, like I say, this is pointless. Using this method, you are generating code on the server which tells the client to query the server.

    If you know you're going to be sending a request to transactions.php, you might as well include that page, or make a HTTP request to it (as per ShrineDesigns response). That way the server is giving the client all the information it needs, and reducing the traffic between server and client, thereby improving performance.

  8. #8
    Join Date
    Apr 2009
    Location
    Leeds, England
    Posts
    101
    The other thing to be aware of is that in the code I provided, PHP won't be aware of the results of the AJAX query, because the query isn't executed until it reaches the client.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles