The realization of extracting data from database dynamically in PHP input page

Source: Internet
Author: User
Tags php language php and sybase sybase database variable client
Dynamic | data | database | Page SUMMARY: When making a Dynamic Web page in PHP, before submitting the server, let PHP take the value of the relevant other fields from the database and display it to the current page according to the value of a field entered by the user on the current page, which is the difficulty in developing the PHP program. In this paper, a concrete example of how to combine two HTML embedded language PHP and JavaScript skillfully combined to solve this difficult point of concrete methods.

Keywords: php, dynamic, HTML.

Today's web sites have evolved from the previous form of static information to interactive ways of providing dynamic information services. The web's information services can be summed up as two: Provide information to customers, and record information submitted by customers. To provide these two services, the problem to be solved is how to quickly let users quickly extract the information they want in a large amount of information on their website, and how to effectively record the information submitted by the users so that they can find it in future. These problems can be solved by adding database support to the Web site.

Because PHP provides good support for a variety of databases, and PHP scripts are embedded directly in HTML documents, they are easy to use. So PHP is one of the most popular server-side embedded languages on the Internet today. In addition, PHP offers free open-source and cross-platform support compared to other server-side scripting languages such as ASP, which makes it easy to adapt to the heterogeneous network environments of today's networks, and allows web-makers to quickly and easily create powerful, dynamic Web pages. However, because PHP is server-side embedding, it is more intuitive to understand that the PHP statement is executed on the server, so it will only submit the contents of the current page received and processed. And when you need the content is based on the customer on the current page entered the value of a field, and then dynamically extracted from the library, PHP can do nothing. For example: to provide customers with an "Order Contract" entry page, it contains a number of "supplier information" input, and the details of each supplier in advance in a "merchant" dictionary entry, now requires when the customer on the current page to select a "supplier", immediately from the "merchant" In the dictionary form, some information such as "account Bank, account number, address, telephone" and so on is extracted from the supplier and displayed on the current page for direct use or modification by the customer. Such requirements, if using a visual programming language such as Pb, VB and other implementation is an easy thing, but Pb, VB is not suitable for writing dynamic Web pages, PHP is suitable for writing dynamic Web pages, but because it is server-side embedding, can not be submitted in time before the value of the variables on the page, so to achieve the above requirements have a certain degree of difficulty. In the process of programming, I combine PHP and JavaScript skillfully, solve this difficulty.

We know that the same embedded statement, but JavaScript is different from the PHP language. Because PHP is server-side embedding and JavaScript is client embedded, JavaScript statements are executed on the client's browser, which determines that JavaScript can get the variable values on the current page in a timely manner, but cannot directly manipulate the server-side database. So, combining the two to create a powerful Dynamic Web page is a perfect combination. For the sake of the description, the following only takes the address of the selected supplier from the dictionary table as an example to illustrate the specific practice. The method is similar when you need to remove more than one field, but be careful when you take a JavaScript function out of a string.

1. Write a PHP function

The function is to remove all qualified supplier information from the Merchant Dictionary table and store it in a string variable $khsz.

function Khqk_tq ($QUESTR) {

Global $dbconn;

$DBQ _resl=sybase_query ($questr, $dbconn); Send out a query string for Sybase execution.

$DBQ _rows=sybase_num_rows ($DBQ _resl); Gets the number of rows returned.

$j = 0;

For ($i =0 $i < $DBQ _rows $i + +) {//Build loops, remove the required fields from each row and place them in the array $k[].

$k []=sybase_result ($DBQ _resl, $i, "kh_id"); Remove the vendor number selected by the user.

$add =sybase_result ($DBQ _resl, $i, "address"); Remove the supplier address.

if ($add = = ""):

$k []=];

else:

$k []=sybase_result ($DBQ _resl, $i, "address");

endif

$khsz = $khsz. $k [$j]. "|" $k [$j +1]. "|"; The values for each field are "|" As a delimiter, connected to the variable $khsz to form a long string.

$j = $j +2;

}

return $khsz;

}

2. Write a JavaScript function

The function is to locate the supplier's address from the string based on the kh_id value and embed it in the HTML file.

<script language= "JavaScript" >

var khstr= "<?= $k?>"//convert PHP variable to javascript variable khstr first.

function Khxz_onclick () {

Frm=document.frmplanfill;

Ghstj=frm.kh_id.value; Gets the kh_id value of the "supplier" that is just selected on the current page.

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

Addr=khstr.substring (Khstr.indexof ("|", Numkh) +1,khstr.indexof ("|", Khstr.indexof ("|", Numkh) +1)); Remove the value of the Address field corresponding to the kh_id from the KHSTR string.

FRM.ADDRESS.VALUE=ADDR; Assigns the fetched value to the field variable address on the current entry page.

}

</script>

3. Combine the two in HTML and use them for each other

$khinfo = "Select Kh_id,address from kh where co_id= $s _coid and type= ' G ' ORDER by kh_id";

Place the SQL statement that takes supplier information into 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" >



<tr>

<td> Contract number </td><td><input NAME=HTBH size=15></td>

<td> Contract Overview &LT;/TD&GT;&LT;TD colspan=5><input NAME=HTGS size=53></td>

</tr>

<tr>

<td> Suppliers </td>

<td><select name= "kh_id" onchange= "Khxz_onclick ()" >//The JavaScript function is triggered each 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 choose from. Where Prt_sele is a custom PHP function.

<td> Address </td>

&LT;TD colspan=3><input type=text name=address size=34 ></td>// The result of triggering a JavaScript function is to make the adrress variable get a value and display it on the page.

The system environment of the above code is: Linux operating system, Sybase database, Web server is Apache, PHP4 program interface. The idea of program development, which combines 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.