Php 在模式窗口中发送表单
很抱歉,我开始了这个话题。我知道这件事有很多话题。但我仍然无法处理它,因为我需要成功/失败消息显示如下:Php 在模式窗口中发送表单,php,jquery,Php,Jquery,很抱歉,我开始了这个话题。我知道这件事有很多话题。但我仍然无法处理它,因为我需要成功/失败消息显示如下: <!-- Form in modal --> <?PHP if(isset($_SESSION['error_msg'])){echo $_SESSION['error_msg']; unset($_SESSION['error_msg']);}?> <?PHP if(isset($_SESSION['success_msg'])){echo $_SESSION
<!-- Form in modal -->
<?PHP if(isset($_SESSION['error_msg'])){echo $_SESSION['error_msg']; unset($_SESSION['error_msg']);}?>
<?PHP if(isset($_SESSION['success_msg'])){echo $_SESSION['success_msg']; unset($_SESSION['success_msg']);}?>
<form id="test-form action="test.php" method="POST">
<input type="text" name="name" placeholder="Name">
<input type="email" name="email" placeholder="Email">
<input type="submit" name="save-test-form" value="Save">
</form>
我的观点是在不重新加载页面的情况下提交此表单(因为它位于模式窗口中),并且我必须在表单中显示成功/失败消息(也不重新加载页面)。我不知道怎么做
我将非常感谢您对如何一步一步完成此操作的帮助和解释。您的PHP脚本在页面重新加载时执行,因此在使用Ajax时,您必须手动显示来自服务器的消息:
// PHP
$response = [];
if(isset($_POST['save-test-form'])){
if(!empty($_POST['name'])){
if(!empty($_POST['email'])){
$response['success'] = 'All is well.';
}else{
$response['error_msg'] = 'Enter the email.';
}
}else{
$response['error_msg'] = 'Enter the name.';
}
echo json_encode($response); // Format array as json and output it
die(); // No other output, just JSON
}
// jQuery
$.ajax({
url: '',
method: 'POST',
dataType: 'json',
data: {},
success: function (response) {
if (typeof response.success !== 'undefined') {
$('#responseMessage').text(response.success);
} else {
$('#responseMessage').text(response.error_msg);
}
}
})
我不知道你到底有什么问题。但是我发现你有一个打字错误
$\u SESSION['success\u msg']
!=<代码>$\u会话['success\u msg']对不起,我刚才在这里给了您一个代码示例。我有不同的剧本。我的观点是在不重新加载页面的情况下提交此表单(因为它位于模式窗口中),并且我必须在表单中显示成功/失败消息(也不重新加载页面)。我不知道怎么做。试试AJAX,有一些很好的教程。你的PHP只在重新加载页面时执行,当使用AJAX时,你必须手动设置错误消息的显示,作为服务器的响应。Justinas,你能用这些消息为我写一个示例代码吗?请我不太懂javascript。我需要这个解决方案。我已经为此挣扎了几天:(我想我已经慢慢开始理解了。谢谢。再问一个问题-jquery脚本是否需要初始化,只有在单击表单中的提交按钮后才能运行?@LukaszStaszewski是的。但这部分不包括在问题或我的回答中。Googlejquery表单劫持
And jquery?
// PHP
$response = [];
if(isset($_POST['save-test-form'])){
if(!empty($_POST['name'])){
if(!empty($_POST['email'])){
$response['success'] = 'All is well.';
}else{
$response['error_msg'] = 'Enter the email.';
}
}else{
$response['error_msg'] = 'Enter the name.';
}
echo json_encode($response); // Format array as json and output it
die(); // No other output, just JSON
}
// jQuery
$.ajax({
url: '',
method: 'POST',
dataType: 'json',
data: {},
success: function (response) {
if (typeof response.success !== 'undefined') {
$('#responseMessage').text(response.success);
} else {
$('#responseMessage').text(response.error_msg);
}
}
})