php 如何在弹出窗口中显示错误消息而不是移动到新页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18635358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display error message inside a pop up window instead of moving to a new page?
提问by Anagh
I'm new to php, and I'm new here and I don't know if it's right for me to ask a question in such a long manner. I'm sorry if that's not how it is done here.
我是 php 新手,我也是新来的,不知道问这么长的问题对我来说是否合适。如果这里不是这样,我很抱歉。
- I have the following code which is used to enter a question and it's four choices.
- 4 choices are entered in text boxes with a radio button corresponding to each.
- The button clicked out of those 4 will be the value of $correct
- $difflevel, $agegroup and $catId are again, the values from a set of radio buttons.
- 我有以下代码用于输入问题,它有四个选项。
- 在文本框中输入了 4 个选项,每个选项都有一个单选按钮。
- 从这 4 个按钮中点击的按钮将是 $correct 的值
- $difflevel、$agegroup 和 $catId 又是一组单选按钮的值。
The following is my code
以下是我的代码
if(isset($_POST['submit']))
{
extract($_POST);
$question=mysqli_real_escape_string($con,$question);
$option[0]=mysqli_real_escape_string($con,$option[0]);
$option[1]=mysqli_real_escape_string($con,$option[1]);
$option[2]=mysqli_real_escape_string($con,$option[2]);
$option[3]=mysqli_real_escape_string($con,$option[3]);
mysqli_query($con,"insert into questions(question, op1,op2,op3,op4,correct,difflevel,ageGroup,catId) values
('$question','$option[0]','$option[1]','$option[2]','$option[3]',$correct,$difflevel,$ageGroup,$catId)") or die("Some error");
$questionId = mysqli_insert_id($con);
if($_POST['catId']=="")//QUESTIONS MUST CONTAIN AT LEAST ONE CATEGORY
die("Please select category of the question entered.");
if($_POST['difflevel']=="")//DIFFICULTY LEVEL SHOULD BE SET
die("Please select difficulty of the question entered.");
if($_POST['ageGroup']=="")//AGEGROUP SHOULD BE SET
die("Please select age group of the question entered.");
if($_POST['correct']=="")//CORRECT ANSWER SHOULD BE SET
die("Please select correct answer for the question entered.");
}
Since all fields are mandatory, I need an error report if not all are filled(or clicked). Right now, when the error occur, it's taken to a new page, and hence all that I typed are lost and I have to type them again. So instead of going to the next page, is it possible to show a pop-up window with the error so that I can correct the mistake and continue from where I stopped? Any help is so much appreciated. Thanks in advance.
由于所有字段都是必填字段,如果未填写(或单击)所有字段,我需要一个错误报告。现在,当错误发生时,它被带到一个新页面,因此我输入的所有内容都丢失了,我必须再次输入它们。因此,不是转到下一页,是否可以显示一个带有错误的弹出窗口,以便我可以纠正错误并从我停止的地方继续?非常感谢任何帮助。提前致谢。
采纳答案by Kemal Da?
If you allow the user to POST the form by clicking the submit button, the page will redirect to the action parameter of form tag. You will then lose all entered data on the form. There are workarounds to overcome this using this method but it is a little bit quirky.
如果您允许用户通过单击提交按钮来 POST 表单,页面将重定向到表单标签的 action 参数。然后,您将丢失表单上所有输入的数据。有一些解决方法可以使用这种方法克服这个问题,但它有点古怪。
But instead of posting form using standard html form elements, post the form using AJAX, then the page will not redirect/refresh, and all previously entered form data will be preserved. Then parse response text and put corresponding error messages accordingly.
但是,不是使用标准的 html 表单元素发布表单,而是使用 AJAX 发布表单,然后页面将不会重定向/刷新,并且之前输入的所有表单数据都将被保留。然后解析响应文本并相应地放置相应的错误消息。
Here is an example:
下面是一个例子:
<form>
<input type="text" id="someText" value="" /><br />
<button id="submit_button">Submit</button>
</form>
<script type="text/javascript">
$(function(){
$("#submit_button").click(function(){
var someText = $("#someText").val();
$.post("/handle_ajax.php",{someText:someText},function(resp){
if(resp !== "OK"){
alert("ERROR OCCURED "+resp)
}
});
});
});
</script>
here is very simple php ajax handler
这是非常简单的 php ajax 处理程序
<?php
if(empty($_POST["someText"])){
echo "someText field cannot be left blank";
}else{
echo "OK";
}
?>
Now if you want to be fancy, instead of alerting the error message, put all error messages in a pretty html form in to the form, and clear them if all went ok.
现在,如果你想变得更漂亮,不要提醒错误消息,而是将所有错误消息以漂亮的 html 形式放入表单中,如果一切顺利,则清除它们。
回答by Shankar Damodaran
Make use of JavaScript alert
使用 JavaScript alert
<?php
if(isset($_POST['submit']))
{
extract($_POST);
$question=mysqli_real_escape_string($con,$question);
$option[0]=mysqli_real_escape_string($con,$option[0]);
$option[1]=mysqli_real_escape_string($con,$option[1]);
$option[2]=mysqli_real_escape_string($con,$option[2]);
$option[3]=mysqli_real_escape_string($con,$option[3]);
mysqli_query($con,"insert into questions(question, op1,op2,op3,op4,correct,difflevel,ageGroup,catId) values
('$question','$option[0]','$option[1]','$option[2]','$option[3]',$correct,$difflevel,$ageGroup,$catId)") or die("Some error");
$questionId = mysqli_insert_id($con);
if($_POST['catId']=="")//QUESTIONS MUST CONTAIN AT LEAST ONE CATEGORY
echo "<script>alert('Please select category of the question entered.')</script>";
exit;
//die("Please select category of the question entered.");
if($_POST['difflevel']=="")//DIFFICULTY LEVEL SHOULD BE SET
echo "<script>alert('Please select difficulty of the question entered')</script>";
exit;
if($_POST['ageGroup']=="")//AGEGROUP SHOULD BE SET
echo "<script>alert('Please select age group of the question entered.')</script>";
exit;
if($_POST['correct']=="")//CORRECT ANSWER SHOULD BE SET
echo "<script>alert('Please select correct answer for the question entered.')</script>";
exit;
}
回答by pinkal vansia
You can use java script validation.So, if some one tries to submit the form without filling in mandatory fields, you can pop an alert message. jQuery validation library handles this perfectly.
您可以使用 java 脚本验证。因此,如果有人尝试提交表单而不填写必填字段,您可以弹出警告消息。jQuery 验证库完美地处理了这个问题。