简体   繁体   English

如何将检查值从php脚本返回到调用页面?

[英]How to return check value from a php script to the calling page?

Hi: I'm very new to the whole web scene. 嗨:我是整个网络领域的新手。 I'm trying to do a page where the user has to enter some information and then what I want to do is check its format and if it is correct added to a database. 我正在尝试做一个页面,用户必须在其中输入一些信息,然后我要检查的是它的格式以及是否正确添加到数据库中。 If it is not, an error message has to be shown. 如果不是,则必须显示错误消息。

My code is essentially this, so far (in a file newtask.php): 到目前为止,我的代码基本上是这样(在文件newtask.php中):

<form action="addtask.php" method="post" name="newtaskform">

<!-- The form itself, a bunch of fields -->

<input type="submit" value="Agregar nueva tarea">
</form>

When I click submit it calls the addtask.php script that modifies the database. 当我单击提交时,它会调用修改数据库的addtask.php脚本。 This is already working. 这已经在工作了。 I want to add the code to check the format of the data submitted in the form. 我想添加代码以检查表单中提交的数据的格式。 However I want the error message to appear in newtask.php (the user input webpage). 但是我希望错误消息出现在newtask.php(用户输入网页)中。 My problem is that if I check the data with a javascript function I have to use ajax to call addtask.php (which I want to try to avoid, if possible) and if I do the checking in the addtask.php script I don't know how to convey the error message back to the newtask.php webpage. 我的问题是,如果我使用javascript函数检查数据,则必须使用ajax来调用addtask.php(如果可能,我想尽量避免这样做),并且如果我在addtask.php脚本中进行检查,我不会不知道如何将错误消息传达回newtask.php网页。

Any help or suggestions? 有什么帮助或建议吗?

you can do two layers of checks. 您可以进行两层检查。 you can use javascript to validate the form first and if it passes then you can use $('#'+formid).submit() to submit the form as per usual (Note, give your an ID property). 您可以使用javascript首先验证表单,如果表单通过,则可以使用$('#'+ formid).submit()照常提交表单(请注意,提供您的ID属性)。 Then you can check the backend as well and if something goes wrong you can send an error back to the page. 然后,您也可以检查后端,如果出现问题,可以将错误发送回页面。

Now, the problem with sending the error back to the page is how do you get back to the previous page with the inputs all still in place, as a typical form submission would actually send the request to the processing page. 现在,将错误发送回页面的问题是如何在所有输入仍然保留的情况下返回上一页,因为典型的表单提交实际上会将请求发送到处理页面。 One trick i learned was that you can submit the form to an iframe which will submit the page to the backend without reloading the front end page 我学到的一个技巧是,您可以将表单提交到iframe,该框架将页面提交到后端而无需重新加载前端页面

<FORM name='iform' id='iform' method='POST' action='addtask.php' TARGET='submitiframe' accept-charset='UTF-8'>

and have this code at the bottom of your page 并在页面底部添加此代码

<IFRAME STYLE='border:0px;display:none;' id='submitiframe'></iframe>

In the backend, you process as normal but rather than echo'ing a status back to the browser, you can do something like this. 在后端,您可以正常进行处理,而不是将状态回显到浏览器,而是可以执行以下操作。 In this example, if the processing was successful i send back a "status" of "1" 在此示例中,如果处理成功,我会发回“ 1”的“状态”

    echo "<!DOCTYPE HTML>
        <HTML>
        <HEAD>
            <script type='text/javascript' src='/js/jquery-1.10.2.min.js'></script>
            <script type='text/javascript'>
                $(document).ready(function() {
                    $(window).load(function() {
                        var r = $('#response').html();
                        parent.getresult(r);
                    });
                });
            </script>
        </HEAD>
        <BODY>
        <DIV ID='response'>".json_encode(Array("status"=>$status))."</DIV>
        </BODY>
        </HTML>";
exit();

On the front end page you would have corresponding javascript code to capture this request and process the success or failure 在前端页面上,您将具有相应的JavaScript代码来捕获此请求并处理成功或失败

        function getresult(text) {
        try {
            var rc = $.parseJSON(text);
            if(parseInt(rc.status)  == 1) 
                location.reload();
            else
                alert (rc.msg);
        }
        catch(e) {
            alert('Something went wrong!')
        }
    }

The easiest KISS solution would be to keep the code responsible for adding tasks (and validating the form) in same file as the form. 最简单的KISS解决方案是让代码负责在与表单相同的文件中添加任务(并验证表单)。 This will also make it easy for you to keep all the form elements filled with submitted data if the task couldn't be added. 如果无法添加任务,这也将使您轻松地使所有表单元素都充满提交的数据。

You can check if form was submited by naming the submit button (adding name attribute) and then checking, if it's set in $_POST array. 您可以通过命名“提交”按钮(添加名称属性)来检查表单是否已提交,然后检查是否在$ _POST数组中设置了表单。

Make a single PHP file. 制作一个PHP文件。 This is for your demonstration only. 这仅用于您的演示。

<?php

if(isset($_POST['submit'])) {
//IF FORM IS SUBMITTED
if($_POST['name'] == '') {
//NAME IS BLANK
$flag = 0;
}
else {
// SQL QUERY TO INSERT DATA
$flag = 1;
}

}

?>

<?php

if(isset($flag)) {

if($flag == 1) {
echo "Data inserted successfully";
}
else {
echo "Name is blank";
}

}

?>

<form action="" method="post" name="newtaskform">
<!-- The form itself, a bunch of fields -->
<input type="" name="name">
<input type="submit" value="submit" name="submit"> 
</form>

Create a function for adding record to the database in addtask.php file may be addRecord(). 在addtask.php文件中创建一个用于将记录添加到数据库的函数可以是addRecord()。 Include that file in newtask.php file. 将该文件包含在newtask.php文件中。 Do a post of the form on newtask.php instead of addtask.php on form submit do the validation of fields, if validation passes call the function addRecord() with POST data, or else if validation fails show the validation errors above the form. 如果验证通过传递带有POST数据的函数addRecord(),或者在验证失败时显示在表单上方的验证错误,则在newtask.php上的窗体上发帖,而不是在表单上的addtask.php上进行字段验证。

Validation can be done using your custom code or may be you can use some class from phpclasses.org 验证可以使用您的自定义代码完成,也可以使用phpclasses.org中的某些类

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM