0

So I have three different separate files:

  • functions.php (all functions for the database)
  • main.html (my main program)
  • main.js (all javascript functions)

Now, I want to call a function in PHP through AJAX. To do that, I need to pass $conn.

$conn = sqlsrv_connect($serverName, $connectionInfo);

It's a resource, so I can't use json_encode.

The way I set the everything up now is that the php-file is required in the html so I can use the functions and when I change the value of a dropdown, the js is called.

How can I pass the $conn variable to Javascript?

Regards

4
  • just call $conn in the AJAX script and return data only? Commented Aug 30, 2018 at 10:39
  • how can I call $conn if this variable is in a different file than the ajax? Commented Aug 30, 2018 at 10:42
  • then include that file? Commented Aug 30, 2018 at 10:45
  • 1
    or better yet - have a class for db handling and call that class (file include still necessary unless you have namespaces etc.) Commented Aug 30, 2018 at 10:45

3 Answers 3

2

It doesn't work like that.

You should never be directly making calls to the database from the front-end.

Think of it as three separate levels. Your HTML/JS is the front-end, your PHP is your server, and your Database is on its own level.

So when the user does something on the front-end, say changes the value of a field and you want to update that in the database the following actions should happen:

  • Event triggers on JS
  • AJAX is called as a result of the event being triggered
  • PHP server receives the AJAX request and executes code to modify database
  • (optional) PHP server sends something back to the front-end to tell it that the request was successful

Read up on the concept of MVC: https://developer.mozilla.org/en-US/docs/Web/Apps/Fundamentals/Modern_web_app_architecture/MVC_architecture

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

Comments

0

Try this in php code as I assume functions.php

$conn = sqlsrv_connect($serverName, $connectionInfo);
echo $conn;//Don't try echo anything other

In Javascript

$.ajax({
           type: "POST",
           url: "functions.php",

           success: function(data)
           {
               var conn = data; // here is your conn which comes from php file
           }
      });

Comments

0

First of all include jquery latest version from cdn

  1. Create an API Url, and use POST method

    site.com/api/insert.php // to insert into table
    
  2. Use $.post() api of jquery to send data

    var url = ""; // enter your URL HERE
    var postData = {};  // object of post data with table name, cols and values
    $.post(url, postData, function(data, status) {
           // do what ever you want with data        
    }) 
    

    ps: you can also create diff insertion / selection / update / delete api for different table. (recommended)

Read more about $.post() here

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.