How to implement ajax to submit form form in php: 1. Create an HTML and PHP file and create the form form; 2. Pass "$.ajax({type: "post",url:"text7.php ",data:dataString...})" code can be used to submit the form.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
How to implement ajax form submission in php?
Submit the form through php jq ajax
The code is as follows:
html
<div id="contact_form"> <form name="contact" method="post" > <label for="name" id="name_label">姓名</label> <input type="text" name="name" id="name" size="30" value="" class="text-input" /> <label class="error" for="name" id="name_error">此项必填</label> <label for="email" id="email_label">您的Email</label> <input type="text" name="email" id="email" size="30" value="" class="text-input" /> <label class="error" for="email" id="email_error">此项必填</label> <label for="phone" id="phone_label">您的联系电话</label> <input type="text" name="phone" id="phone" size="30" value="" class="text-input" /> <label class="error" for="phone" id="phone_error">此项必填</label> <br /> <input type="button" name="submit" class="button" id="submit_btn" value="我要发送" /> </form> </div>
js
<script language="javascript"> $(function(){ $(".error").hide(); $(".button").click(function(){ var name= $("#name").val(); if(name==""){ $("#name_error").show(3600); $("#name_error").focus(); return false; //return false 的作用是为了让input框效果是一次只出现一个 //当type为submit的时候,不return false 的话就会提交form表单而没有效果 //当type为button的时候,不用return false也可以显示效果 //原话:而只有当出现错误,即为空时,错误才会出现,因为有 return false 的作用,每次仅会出现一个错误。 } var email= $("#email").val(); if(email==""){ $("#email_error").show(3600); $("#email_error").focus(); return false; } var phone= $("#phone").val(); if(phone==""){ $("#phone_error").show(3600); $("#email_error").focus(); return false; } var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone; //document.write(dataString); $.ajax({ type: "post", url:"text7.php", data:dataString, success:function(mag){ // alert(mag);return; //ajax提交form表单php里无法看post值,只能在回调函数里面打印,下面为成功之后的返回效果 $('#contact_form').html("<div id='message'></div>"); $("#message").html("<p>联系方式已成功提交!</p>") .append("<p>Script design</p>") .hide() .fadeIn(1500, function() { $('#message').append("<img id='checkmark' src='yes.ico' / alt="How to implement ajax form submission in php" >"); }); } }) return false; }) }) </script>
php
print_r($_POST); echo $_POST['name'].'<br>'."i'm ok!~";die;
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement ajax form submission in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.