Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 请教:PHP js 实现复杂的按钮功能

请教:PHP js 实现复杂的按钮功能

Jun 23, 2016 pm 02:03 PM

请教各位大虾:我要制作的网页里有一个按钮,点击这个按钮要实现以下几个功能:

先判断页面上给出的姓名是否已经存在于SQL数据库中了。如果存在,就给个提示框,然后跳出。如果不存在,要将页面上的内容(如姓名、性别、Email、电话等)存入SQL数据库,再提示已经写入数据库,然后跳转到另一个页面。

我定义的按钮如下(a.php文件):



然后打算在a.js文件的function data_send()里实现按钮的功能,目前给出提示框和跳转网页我知道怎么实现,可是如何判断是否应该写入数据库,以及如何在js里写入数据库,我就不得而知了。

我是初来乍到的菜鸟,还望各位前辈多多指教,先谢谢大家帮忙啦~!


回复讨论(解决方案)

ajax检测和入库,然后再跳转

路过

利用ajax请求php,由php来检查姓名是否存在。

ajax检测和入库,然后再跳转

谢谢回复。

能说得详细点儿么?最好能有个例子。谢谢~!

能说得详细点儿么?最好能有个例子。谢谢~!
例子。。。。ajax网上的例子很多,随便 google哈一大堆,
原理就是 input框失去焦点后ajax提交,然后php处理返回json之类的,然后js在操作显示提示

我最近也在研究,分享下:
文件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 || 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 "可以使用";
}
?>

文件4、(b.php)代码如下:
include "conn.php";
if(isset($_POST['sub'])){
    $sql = "insert into user(name) value ($_POST['name'])";
    $result = mysql_query($sql);
    if($result){
        echo "成功!";
    }
}
?>

不知道楼主用过jquery没有,如果用过或者想用的话我可以给详细方案。

function check(){var name=$("#name").val();$.post("checkUser.php",{name:name},function(result){   if(result==1){      alert('已经存在用户名');      return false;    }});}
Copy after login


checkUser.php
$sql="select * from user where name=".$_POST['name'];$resut=mysql_query($sql);if($result){  echo 1;exit;}
Copy after login

得用jquery post和php很好解决这个,现在很多注册页面都是这样做!

感谢LS各位大虾的帮助,问题解决了。我用的是Ajax。

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

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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

How to Register and Use Laravel Service Providers How to Register and Use Laravel Service Providers Mar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

See all articles