简体   繁体   中英

Look for in Mysql table and pass information from the javascript

My web-server has PHP and MySQL.

One table of database has information about user.

User that uses my site wants to receive some information.

So he or she presses mouse button and happen click event for this button and submit event for a form.
I use framework jQuery and in my javascript (method .submit() ) I use AJAX for retrieving necessary information.

  1. First, I want to know exist so user in the table or not. I receive this information using ajax and php file – first.php .

    • If information about user hasn't in the table I report about this in the script.
    • If information about user hasn't in the table but like user(s) exist(s) in it I inform of this situation.
    • If information about user has in the table I call command: window.location.href = “second.php?param=2&user=userid” in my script and I have to look for necessary information again in the table and show it in the web-page.
  2. So my next question is: How can I refrain from repeating a query on the same table?
    I must show about 10 account's records in my web page.
    I want to know. How can I pass information (10 account's records) from my script into second.php file?
    I am afraid to do the second command $.ajax() in the script. I think there is no need for it.

What you are doing now is pretty much the standard. If you dont want to use this method then I suggest that you look into session.

On your first page you will need to include the line:

<?php
session_start();
then your normal code.
Once you have read the information from the database then place the values into session variables;
$_SESSSION['var1'] = $result['id']; //--- continue for all the values you want to save

On the second page you will need to do the reverse

<?php
session_start();
$id = $_SESSION['var1'];

You could write the query result into a semi-static 1 JavaScript snippet:

<?php 

// do your query
$result = ...;

echo '<script type="text/javascript">'
    . 'var accountData = ' . parseForJavaScript($result) . ';'
    . '</script>';

// do your other stuff

Then you can write code that depends on that (not necessarily global) variable accountData :

// JavaScript:
doStuff(accountData);

because it will essentially be expanded to (if parseForJavaScript returns a representation):

<script type="text/javascript">
    var accountData = {users: {
        0 : { "name" : "John" },
        1 : { "name" : "Jack" },
        2 : { "name" : "James" }
    }};
</script>

Of course the JSON object would look differently depending on your actual data structure.

Or you can skip that variable and inject the data directly as a function parameter:

<?php 

// do your query
$result = ...;

echo '<script type="text/javascript">'
    . 'doStuff(' . parseForJavaScript($result) . ');'
    . '</script>';

// do your other stuff

1 semi-static because it is actually dynamically generated by php but looks static when looking at the final HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM