是否可以使用Ajax发布HTML代码?

时间:2013-12-12 02:30:48

标签: php html ajax jquery

我正在为我的大学课程制作基于HTML,PHP和Ajax的网站,并遇到一些我无法弄清楚的问题。我可以使用Ajax post方法将基于HTML的注册表发布到我的主PHP站点吗?我的代码如下所示:

的index.php

<form id="loginForm" action="login.php" method="POST">
Username: <input type="text" name="username" id="username"/><br/>
Password: <input type="password" name="password" id="password"/><br/>
<button id="submit">Login</button>
<button id="regButton">Register</button>
</form>

<div id="ack"></div>
<div id="regAjax"></div>

<script type="text/javascript" src="Script/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="Script/scriptAjax.js"></script>

register.html

<html>
<head><title>Registration Form</title></head>
<body>

<form id="regForm" action="process.php" method="POST">
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
First Name: <input type="text" name="fname"/><br/>
Last Name: <input type="text" name="lname"/><br/>
E-mail: <input type="text" name="email"/><br/>
<button id="register">Register</button>
</form>

<div id="rck"></div>

<script type="text/javascript" src="Script/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="Script/scriptAjax.js"></script>
</body>
</html>

scriptAjax.js

$("#regButton").click( function() {

$.post ???

$("#regButton").submit( function() {
        return false;
    });
});

因此,当单击“注册”按钮时,使{2. 1}}位置显示更顺畅的页面和注册表单的主要目的是,该用户可以注册不被重定向到另一个页面。有没有办法做到这一点,或者我现在正走错路?

2 个答案:

答案 0 :(得分:1)

一般的想法是,您必须将表单数据发送到PHP脚本,该脚本将对其进行评估并发送响应。

$.post( "validate.php", function( data ) {
  $( "#regAjax" ).html( data );
});

我建议你学习this page

答案 1 :(得分:0)

$.ajax({
method: 'POST',
url: 'validate.php',
data: data,
success: function(result){
 $( "#regAjax" ).html( result );
}
});