How to query database using javascript?

后端 未结 8 1182
渐次进展
渐次进展 2020-12-09 20:07

Another question by a newbie. I have a php variable that queries the database for a value. It is stored in the variable $publish and its value will change (in the database)

相关标签:
8条回答
  • 2020-12-09 20:33

    Well, i think i understand your quaestion, but you have to get a starting point, try to understand this:

    • try to understand what are client variables and server variables.
    • javascript does not comunicate with database.
    • you can use javascript to retrieve data to a specific "Object variable".
    • Using ajax methods of jquery you can post that data do other page, that will execute the proper actions
    0 讨论(0)
  • 2020-12-09 20:33

    you can ;) at first you must create php file to query database and return something like true or flase and then with file url check the function and get answer

    function find_published(folder_id) {
        var aj_url = "{{server_path}}/ajax/url"
        var list;
        $.getJSON(aj_url+"?callback=?&",
              function(data) {
                  //here is your data... true false ... do every thing you want
              }
        );
    };
    
    0 讨论(0)
  • 2020-12-09 20:37

    I'll try to leave the technical jargon aside and give a more generic response since I think you might be confused with client-side and server-side scripting.

    Think of javascript as a language that can only instruct your WEB BROWSER how to act. Javascript executes after the server has already finished processing your web page.

    PHP on the other hand runs on your web server and has the ability to communicate with your database. If you want to get information from your database using javascript, you'll need to have javascript ask PHP to query the database through an AJAX call to a PHP script.

    For example, you could have javascript call a script like: http://www.myserver.com/ajax_function.php?do=queryTheDatabase

    In summary: Javascript can't connect to the database but it can ask PHP to do so. I hope that helps.

    0 讨论(0)
  • 2020-12-09 20:37

    Let me try, you want to change the link in a page from a pop-up that handles a form processing. Try to give your link a container:

    <div id="publish_link"><a href="#" id="publish">Publish</a></div>
    

    As for the form submission use Ajax to submit data to the server to do an update and get a response back to change the link to edit or something:

    $.post("submit.php", { some_field: "some_value"}, function(response) {
        if(response.isPublished)
           $('#publish_link', window.opener.document).html('<a href="edit.html">Edit</a>');
    });
    

    Basically your publish link is contained in a div with an ID publish_link so you change its content later after data processing without reloading the page. In the pop-up where you would do the form processing it is done using jQuery Ajax POST method to submit the data. Your script then accepts that data, update the database and if successful returns a response. jQuery POST function receives that response and there's a check there if isPublished is true, get the pop-up's opener window (your main window) and update the link to Edit. Just an idea, may not be the best out there.

    0 讨论(0)
  • 2020-12-09 20:37

    this app for node.js does mysql queries https://github.com/felixge/node-mysql

    0 讨论(0)
  • 2020-12-09 20:37

    You need to use AJAX for this, like .post() or .get() or JSON.

    0 讨论(0)
提交回复
热议问题