请教:PHP js 实现复杂的按钮功能
请教各位大虾:我要制作的网页里有一个按钮,点击这个按钮要实现以下几个功能:
先判断页面上给出的姓名是否已经存在于SQL数据库中了。如果存在,就给个提示框,然后跳出。如果不存在,要将页面上的内容(如姓名、性别、Email、电话等)存入SQL数据库,再提示已经写入数据库,然后跳转到另一个页面。
我定义的按钮如下(a.php文件):
然后打算在a.js文件的function data_send()里实现按钮的功能,目前给出提示框和跳转网页我知道怎么实现,可是如何判断是否应该写入数据库,以及如何在js里写入数据库,我就不得而知了。
我是初来乍到的菜鸟,还望各位前辈多多指教,先谢谢大家帮忙啦~!
------解决方案--------------------
ajax检测和入库,然后再跳转
------解决方案--------------------
利用ajax请求php,由php来检查姓名是否存在。
------解决方案--------------------
我最近也在研究,分享下:
文件1、(conn.php) 代码如下:
$conn = mysql_connect('localhost','数据库用户名','数据库密码');
mysql_select_db('数据库名',$conn);
?>
文件2、(a.html)代码如下:
<script><br />var xmlHttp<br /><br />function check(str){<br /> <br />xmlHttp=GetXmlHttpObject()<br />if (xmlHttp==null)<br /> {<br /> alert ("Browser does not support HTTP Request")<br /> return<br /> } <br />var url="a.php"<br />url=url+"?n="+str<br />url=url+"&sid="+Math.random()<br />xmlHttp.onreadystatechange=stateChanged <br />xmlHttp.open("GET",url,true)<br />xmlHttp.send(null)<br />} <br /><br />function stateChanged() <br />{ <br />if (xmlHttp.readyState==4 <br><font color='#FF8000'>------解决方案--------------------<br> xmlHttp.readyState=="complete")<br /> { <br /> document.getElementById("name").innerHTML=xmlHttp.responseText <br /> } <br />}<br /><br /><br />function GetXmlHttpObject()<br />{<br />var xmlHttp=null;<br /><br />try<br /> {<br /> // Firefox, Opera 8.0+, Safari<br /> xmlHttp=new XMLHttpRequest();<br /> }<br />catch (e)<br /> {<br /> // Internet Explorer<br /> try<br /> {<br /> xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");<br /> }<br /> catch (e)<br /> {<br /> xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");<br /> }<br /> }<br />return xmlHttp;<br />}<br /></script>
文件3、(a.php) 代码如下:
include "conn.php";
$sql = 'select * from user';//user为你的数据库名,自己修改
$res = mysql_query($sql);
while($row=mysql_fetch_assoc($res)){
$u_name[]= $row['name'];//$row['name']里面的name为你数据库字段名
}
$name = $_GET['n'];
if(in_array($name,$u_name)){
echo "此用户名已被注册请输入新的用户名!";
}else{
echo "可以使用";

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor