Home Backend Development PHP Tutorial php录入页面中动态从数据库中提取数据的实现_PHP

php录入页面中动态从数据库中提取数据的实现_PHP

Jun 01, 2016 pm 12:34 PM
php information dynamic accomplish extract data database page

石家庄师范专科学校 计算中心  张书梅

摘要:用PHP制作动态Web页面时,在提交服务器之前,让PHP根据用户在当前页面上录入的某字段的值立即从数据库中取出相关的其它字段的值并显示到当前页面上,是PHP程序开发中的难点。本文以一个具体实例详细介绍了怎样将两个HTML内嵌式语言PHP和JavaScript巧妙结合起来,解决这个难点的具体方法。

关键词:PHP、动态、HTML。

现在的网站已经从以前提供静态信息的形式发展到交互式的提供动态信息业务的方式。Web的信息服务形式可以概括为两点:向客户提供信息;记录客户提交的信息。要提供这两种服务,需解决的问题是:如何快速地让用户在自己网站大量的信息中快速提取他想要的信息,如何有效地记录用户提交的信息,以便于将来用户查找。这些问题都可以通过在网站中加入数据库支持来解决。

因PHP对多种数据库都能提供良好的支持,且PHP的脚本直接嵌入在HTML文档中,使用非常方便。因此PHP是现在Internet上最流行的一种Server端嵌入语言之一。另外,与其它的Server端脚本语言如ASP相比,PHP免费开放源码并且提供跨平台的支持,这使它能够轻易适应当今网络中各种异质的网络环境;可让网页制作人员能够非常快捷、方便地制作出功能强大的动态Web页面。但是,由于PHP是服务器端嵌入,更直观的理解是PHP语句是在服务器上执行,所以它只有提交时才将当前页面上的内容接收和处理。而当你需要的内容是根据客户当前页面上录入的某字段的值,再动态到库中提取时,PHP就无能为力了。例如:要向客户提供一个“订货合同”的录入页面,其中包含一些“供货商信息”的录入,而各供货商的详细信息事先已在一个“商家”字典表中录入,现在要求当客户在当前页面上选中某“供货商”时,立时从“商家”字典表中将该供货商的某些信息如“开户行、帐号、地址、电话”等提取出来显示到当前页面上供客户直接使用或修改使用。这样的要求若用可视化编程语言如PB、VB等实现是一件轻而易举的事,但PB、VB不适合编写动态Web页面;PHP适合编写动态Web页面,但由于是服务器端嵌入,不能及时获得提交前页面上的变量值,所以实现上述要求就有一定的难度。在程序编写过程中,我将PHP与JavaScript巧妙结合起来,解决了这个难点。

我们知道,同样是嵌入语句,但是JavaScript 又不同于PHP语言。因为PHP是服务器端嵌入,而JavaScript是客户端嵌入,既JavaScript语句是在客户的浏览器上执行,这就决定了JavaScript可及时获得当前页面上的变量值,但又无法对服务器端的数据库直接操作。所以,将二者结合起来制作出功能强大的动态Web页面,可谓是珠联璧合。为了叙述方便,下面只以从字典表中取出选中供货商的地址为例,说明具体做法。当需要取出多个字段时,方法类似,但利用JavaScript函数从字符串中逐个取出时,要细心一些。

1.编写一个PHP函数

此函数的功能是将所有符合条件的“供货商信息”从“商家”字典表中取出,并存放到一个字符串变量$khsz中。

   function Khqk_Tq($questr){

     global $dbconn;

     $dbq_resl=sybase_query($questr,$dbconn);  //送出一个query字符串供Sybase执行。

     $dbq_rows=sybase_num_rows($dbq_resl);    //获得返回行的数目。

     $j=0;

for ($i=0;$i

       $k[]=sybase_result($dbq_resl,$i,"kh_id");  //取出用户选择的供货商编号。

       $add=sybase_result($dbq_resl,$i,"address");  //取出该供货商地址。

       if ($add==""):

          $k[]="无";

       else :

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

     endif;

     $khsz=$khsz.$k[$j]."|".$k[$j+1]."|";  //将各字段值以”|”为分隔符,连接到变量$khsz 中,形成一个长字符串。

       $j=$j+2;

   }

  return $khsz;

}

2.编写一个JavaScript函数

该函数的功能是从字符串中根据kh_id值找到该供货商的地址,嵌入到HTML文件中。

  var  khstr="=$k?>"               //先将PHP变量转变成JavaScript变量khstr。

 function khxz_onclick(){

   frm=document.frmplanfill;

   ghstj=frm.kh_id.value;          //获得当前页面上刚刚选中的“供货商”的kh_id值。

   numkh=khstr.indexOf(ghstj,0) ; //从khstr串中找到该kh_id值所在的位置。

addr=khstr.substring(khstr.indexOf("|",numkh)+1,khstr.indexOf("|",khstr.indexOf("|",numkh)+1));           //从 khstr串中取出与kh_id对应的地址字段的值。

   frm.address.value=addr;  //将取出的值赋给当前录入页面上的字段变量address。

}

3.在HTML中将二者结合起来,互为所用

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

//将取供货商信息的SQL语句放到变量$khinfo中。

  $k=Khqk_Tq($khinfo);    //调用PHP函数,并将返回的字符串值放到变量$k中。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles