我一遍又一遍地扫描我的代码,但我似乎找不到问题所在。当我单击该链接时#add-user-btn
,该文件actions.php
被调用了两次(因此 PHP 脚本被执行了两次)。
这是脚本:我想它与 ajax 请求前面的 javascript 有关吗?
$(function () {
$("#add-user-btn").click(function (e) {
e.preventDefault();
var email = $("#email").val();
var name = $("#name").val();
var action = "adduser";
$.ajax({
url: '../actions.php',
type: 'POST',
data: {
action: action,
email: email,
name: name,
},
dataType: 'json',
success: function (data) {
$(".close-reveal-modal").click();
}
});
});
});
的HTML:
<div id="adduser" class="reveal-modal">
<h1>Add new user</h1>
<p><label for="email">E-mail:</label> <input id="email" name="email" type="text" placeholder="name@example.com" /></p>
<p><label for="name">Name:</label> <input id="name" name="name" type="text" placeholder="Adam Smith"/></p>
<p><label for="password">Password:</label> <input id="password" name="password" type="password" placeholder="123456" /></p>
<p>
<label for="authorization">Authorization:</label>
<select id="authorization" name="authorization">
<option value="0">Administrator</option>
<option value="1">Superuser</option>
<option value="2">User</option>
</select>
</p>
<button id="add-user-btn">Submit</button>
<a class="close-reveal-modal">×</a>
</div>