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

Source: Internet
Author: User
Tags sybase database

Abstract: when using php to create a dynamic web page, before submitting the page to the server, it is a difficulty in php program development to allow php to immediately retrieve and display the values of other related fields from the database based on the values of a field entered on the current page. 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 requirement is easy to implement with visual programming languages such as pb and vb, but pb and vb are not suitable for compiling dynamic web pages. php is suitable for compiling dynamic web pages, however, because it is embedded on the server, the variable values 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 ";

Else:

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

Endif;

$ 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.

}

</Script>

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>

</Head>

<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">



<Tr>

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

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

</Tr>

<Tr>

<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..

The system environment of the above Code is: linux operating system, sybase Database, and web server. It is the apache and php4 program 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: info-contact@alibabacloud.com 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.