How to dynamically extract data from the database on the PHP entry page

Source: Internet
Author: User
Tags sybase sybase database

Abstract: when using PHP to create a dynamic web page, before submitting the page to the server, let PHP immediately retrieve the values of other related fields from the database and display them to the current page based on the value of a field entered on the current page. Program Difficulties in development. This article describes in detail how to combine PHP and JavaScript, two embedded HTML languages, to solve this difficulty.

Keywords: PHP, dynamic, HTML.

The current website has evolved from providing static information to interactive dynamic information services. The Web Information Service can be summarized into two forms: providing information to customers and recording information submitted by customers. To provide these two services, you need to solve the problem: how to quickly allow users to quickly extract the information they want from a large amount of information on their websites, how to effectively record information submitted by users for future user searches. These problems can be solved by adding database support to the website.

Because PHP supports a variety of databases, and PHP scripts are directly embedded in HTML documents, it is very convenient to use. Therefore, PHP is one of the most popular server-side embedded languages on the Internet. In addition, compared with other server-side scripting languages such as ASP, PHP provides free open source code and cross-platform support, which makes it easy to adapt to various heterogeneous network environments in today's network; this allows web page producers to quickly and conveniently create powerful dynamic web pages. However, because PHP is embedded on the server, it is more intuitive to understand that PHP statements are executed on the server, so it receives and processes the content on the current page only when it is submitted. PHP is powerless when you need to dynamically extract data from the database based on the value of a field entered on the current page of the customer. For example, to provide a "order contract" entry page to the customer, which contains some "supplier information" entry, the detailed information of each supplier has already been input in a "seller" dictionary table. Now, when the customer selects a "supplier" on the current page, some information of the supplier, such as the "opening bank, account, address, and phone number", is extracted from the "Merchant" dictionary table and displayed on the current page for direct use or modification by the customer. This requires visualization.Programming LanguageSuch as Pb and VB is easy to implement, but Pb and VB are not suitable for compiling dynamic web pages. php is suitable for compiling dynamic web pages, but because it is embedded on the server, the variable value on the page before submission cannot be obtained in time, so it is difficult to implement the above requirements. During programming, I skillfully combined PHP with JavaScript to solve this difficulty.

We know that it is also an embedded statement, but Javascript is different from the PHP language. Because PHP is embedded on the server, while Javascript is embedded on the client, JavaScript statements are executed on the client's browser, which determines that javascript can obtain the variable values on the current page in a timely manner, however, you cannot directly operate the database on the server. Therefore, the combination of the two is a perfect combination of dynamic web pages. For ease of description, the following describes how to retrieve the address of the selected supplier from the dictionary. When you need to retrieve multiple fields, the method is similar, but when using JavaScript Functions to extract them from strings one by one, you should be careful.

1. Compile a PHP function

This function extracts all qualified "supplier information" from the "seller" dictionary and stores it in a string variable $ khsz.

Function khqk_tq ($ questr ){

Global $ dbconn;

$ Dbq_resl = sybase_query ($ questr, $ dbconn); // sends a query string for Sybase to execute.

$ Dbq_rows = sybase_num_rows ($ dbq_resl); // obtain the number of returned rows.

$ J = 0;

For ($ I = 0; $ I <$ dbq_rows; $ I ++) {// create a loop to retrieve the required fields in each row and put them in the array $ K.

$ K [] = sybase_result ($ dbq_resl, $ I, "kh_id"); // retrieve the supplier ID selected by the user.

$ Add = sybase_result ($ dbq_resl, $ I, "Address"); // obtain the supplier address.

If ($ add = ""):

$ K [] = "NONE ";


$ K [] = sybase_result ($ dbq_resl, $ I, "Address ");


$ Khsz = $ khsz. $ K [$ J]. "| ". $ K [$ J + 1]. "|"; // separate the field values with "|" and connect them to the variable $ khsz to form a long string.

$ J = $ J + 2;


Return $ khsz;


2. Compile a JavaScript function

The function is to find the address of the supplier based on the kh_id value in the string and embed it into the HTML file.

<Script language = "JavaScript">

VaR khstr = "<? = $ K?> "// Convert the PHP variable into the Javascript variable khstr first.

Function khxz_onclick (){

FRM = Document. frmplanfill;

Ghstj = FRM. kh_id.value; // obtain the kh_id value of the selected "supplier" on the current page.

Numkh = khstr. indexof (ghstj, 0); // locate the location of the kh_id value from the khstr string.

ADDR = khstr. substring (khstr. indexof ("|", numkh) + 1, khstr. indexof ("|", khstr. indexof ("|", numkh) + 1); // retrieves the value of the address field corresponding to the kh_id from the khstr string.

FRM. Address. value = ADDR; // assign the retrieved value to the field variable address on the current input page.



3. combine the two in HTML for mutual use

$ Khinfo = "select kh_id, address from KH where co_id = $ s_coid and type = 'G' order by kh_id ";

// Place the SQL statement that obtains the supplier information in the variable $ khinfo.

$ K = khqk_tq ($ khinfo); // call the PHP function and place the returned string value in the variable $ K.

<Meta name = "generator" content = "Microsoft developer Studio">

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb_2312-80">

<Title> order contract management </title>


<Body style = "background-color: Ivory">

<Form name = frmplanfill method = post action = "dhht_crt.php">

<Table align = center border = 1 cellpadding = 0 cellspacing = 0 width = "90%" style = "font-size: 10pt">


<TD> Contract No. </TD> <input name = htbh size = 15> </TD>

<TD> contract overview </TD> <TD colspan = 5> <input name = htgs size = 53> </TD>



<TD> supplier </TD>

<TD> <select name = "kh_id" onchange = "khxz_onclick ()"> // triggers the preceding JavaScript function every time the selected supplier is changed.

<? Prt_sele ("select convert (INT, kh_id), name from KH where co_id = $ s_coid and type = 'G'", 0, 1)?> </SELECT> </TD> // put all qualified supplier names in the drop-down list box for users to select. Prt_sele is a custom PHP function.

<TD> address </TD>

<TD colspan = 3> <input type = text name = address size = 34> </TD> // The result of triggering the JavaScript function is that the adrress variable obtains the value and is displayed on the page..

AboveCodeThe Linux operating system, Sybase Database, and web server are the Apache and PhP4 programming interfaces. The above code combined with PHP and JavaScript has been successfully applied to multiple dynamic web pages we have developed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.