Javascript 在使用jQuery';表单上的默认值是多少?
我正在使用jQuery、AJAX和PHP验证我网站上的大多数表单。实际的输入验证是通过PHP完成的(我认为这将最好地防止用户使用浏览器源代码检查器绕过验证来编辑脚本),但我使用jQuery和AJAX将错误加载到表单提交按钮下方的错误消息div中 所有这些都很好,但是当表单成功提交时,我想调用Javascript 在使用jQuery';表单上的默认值是多少?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在使用jQuery、AJAX和PHP验证我网站上的大多数表单。实际的输入验证是通过PHP完成的(我认为这将最好地防止用户使用浏览器源代码检查器绕过验证来编辑脚本),但我使用jQuery和AJAX将错误加载到表单提交按钮下方的错误消息div中 所有这些都很好,但是当表单成功提交时,我想调用header('Location:foo.php')将我的用户发送回某个页面。但是,由于我使用的是preventDefault(),因此我的新页面被加载到错误消息div中,使浏览器窗口看起来好像有两个页面相互
header('Location:foo.php')
将我的用户发送回某个页面。但是,由于我使用的是preventDefault()
,因此我的新页面被加载到错误消息div中,使浏览器窗口看起来好像有两个页面相互重叠(当前url也不会更改)
这有解决办法吗?我想我可以在PHP代码完成后通过包含一个脚本来解除PHP文件中事件的绑定,但我没有成功
jQuery:
$(document).ready(function() {
$("form").submit(function(event) {
event.preventDefault();
var url = window.location.href.toString().split("=");
var id = url[1];
var title = $("#title").val();
var content = $("#content").val();
var submit = $("#submit").val();
//this is where the PHP is loading the new page, along with error messages
$(".form-message").load("/php/_create.thread.php", {
title: title,
content: content,
id: id,
submit: submit
});
});
});
PHP文件结尾:
<?php
//if successful, exit the script and go to a new page
$submissionSuccessful = true;
exit(header('Location: /index.php'));
?>
<reference path="/javascript/jquery-3.3.1.min.js"></reference>
<script type="text/javascript">
var submissionSuccessful = "<?php echo $submissionSuccessful; ?>";
if (submissionSuccessful)
{
$("#title, #content").css(
{
"border": "2px solid #24367e"
}
);
$("#title, #content").val("");
}
</script>
var submissionSuccessful=“”;
如果(提交成功)
{
$(“#标题,#内容”).css(
{
“边框”:“2px实心#24367e”
}
);
$(“#标题,#内容”).val(“”);
}
我谈论的方法与此类似
$(document).ready(function () {
$("form").submit(function(event) {
event.preventDefault();
var url = window.location.href.toString().split("=");
var id = url[1];
var title = $("#title").val();
var content = $("#content").val();
var submit = $("#submit").val();
// AJAX POST request to PHP
$.post("/php/_create.thread.php", {
title: title,
content: content,
id: id,
submit: submit
}).done(function (response) {
// response is a JSON document
if (response.error) {
// Here you basically modify the UI to show errors
$(".form-message").text(response.error)
} else {
// Here you basically modify the UI to show success
$("#title, #content").css({ "border": "2px solid #24367e" });
$("#title, #content").val("");
location.href = '/index.php' // REDIRECT!
}
});
});
});
在服务器端
<?php
if ($someSuccessCondition) {
$response = ['success' => true];
} else {
$response = ['error' => 'The Error Message'];
}
echo json_encode($response);
exit();
我谈论的方法与此类似
$(document).ready(function () {
$("form").submit(function(event) {
event.preventDefault();
var url = window.location.href.toString().split("=");
var id = url[1];
var title = $("#title").val();
var content = $("#content").val();
var submit = $("#submit").val();
// AJAX POST request to PHP
$.post("/php/_create.thread.php", {
title: title,
content: content,
id: id,
submit: submit
}).done(function (response) {
// response is a JSON document
if (response.error) {
// Here you basically modify the UI to show errors
$(".form-message").text(response.error)
} else {
// Here you basically modify the UI to show success
$("#title, #content").css({ "border": "2px solid #24367e" });
$("#title, #content").val("");
location.href = '/index.php' // REDIRECT!
}
});
});
});
在服务器端
<?php
if ($someSuccessCondition) {
$response = ['success' => true];
} else {
$response = ['error' => 'The Error Message'];
}
echo json_encode($response);
exit();
我认为这不会按你想要的方式工作。当您使用jQuery处理表单提交时,您基本上停止了任何可能的浏览器级重定向。相反,您需要使用JS重定向(如果成功的话),可能需要使用类似以下内容:location.href='/index.php'
(抱歉,我过早提交了我的评论)这是什么意思?我已经能够使用Javascript解决方案重定向用户,在最糟糕的情况下,禁用JS后,用户可以在成功提交表单后单击按钮返回主页。我只是想知道如何使用PHP来解决这个问题,或者我是否完全错了。这是我在使用我的方法时遇到的唯一问题。我的解决方法是:exit('location.assign(“/index.php”)
我推荐一种稍微不同的方法:而不是$(x).load(url,数据)
使用$.post(url,数据,回调)
,url应该返回一些json,比如{error:“message”}
或{success:true}
(也可以是HTTP错误代码!),然后返回JS中,您可以对响应执行一个条件:if(response.error){showError}else{location.href='/index.php'}
,正如@mxcoder所说,在进行Ajax调用时,您不能使用php重定向用户。如果您这样做,请求将跟随该重定向并从中返回响应(即您重定向到的页面)。您需要在回调中使用location.href
。通过ajax加载的JS代码不会被触发。如果ajax请求返回错误,则显示错误,如果返回成功,则使用location.href
重定向用户。我也不太明白标题与您的问题或问题有什么关系?我认为这不会按您希望的方式工作。当您使用jQuery处理表单提交时,您基本上停止了任何可能的浏览器级重定向。相反,您需要使用JS重定向(如果成功的话),可能需要使用类似以下内容:location.href='/index.php'
(抱歉,我过早提交了我的评论)这是什么意思?我已经能够使用Javascript解决方案重定向用户,在最糟糕的情况下,禁用JS后,用户可以在成功提交表单后单击按钮返回主页。我只是想知道如何使用PHP来解决这个问题,或者我是否完全错了。这是我在使用我的方法时遇到的唯一问题。我的解决方法是:exit('location.assign(“/index.php”)
我推荐一种稍微不同的方法:而不是$(x).load(url,数据)
使用$.post(url,数据,回调)
,url应该返回一些json,比如{error:“message”}
或{success:true}
(也可以是HTTP错误代码!),然后返回JS中,您可以对响应执行一个条件:if(response.error){showError}else{location.href='/index.php'}
,正如@mxcoder所说,在进行Ajax调用时,您不能使用php重定向用户。如果您这样做,请求将跟随该重定向并从中返回响应(即您重定向到的页面)。您需要在回调中使用location.href
。通过ajax加载的JS代码不会被触发。如果ajax请求返回错误,则显示错误,如果返回成功,则使用location.href
重定向用户。我也不太明白标题与您的问题有什么关系?您可能可以替换window.location.href.toString().split(=”
(返回带有位置的数组),只需window.location.href
(将返回带有位置的字符串)侧注:如果呼叫成功返回,您也只需要拥有location.href
。因为我们将用户从这个页面重定向出去,所以不需要设置任何css或清空任何输入。是的,你是对的。但是在一瞬间,他们可以看到提交的表单,我想让他们直观地知道他们做得很好。@Dalsia-当表单重定向时,他们不知道吗?这是他们应该得到一些反馈的地方。大多数用户可能看不到这些更改。在重定向之前,他们可能会看到一些东西在闪烁,但不知道是什么。是的,但在发送到下一页之前(在我看来)看到您的输入被清除是一种玩笑。您可能可以替换window.location.href.toString().split(“=”)代码>(返回带有位置的数组)仅包含