使用显示/隐藏功能创建PHP表单 [英] Creating PHP Forms with show/hide functionality
问题描述
我想创建两个报告,并使用类中定义的两个函数将报告数据提交到数据库:这里有两个按钮:Create ES和Create RP。
Rightnow,我的表单工作正常,我可以成功插入数据,但问题是当我在填写表单数据后点击提交,内容隐藏并显示第一个div内容cs_content和再次,我需要点击再次提交。
任何人都可以为此提供解决方案。
要求:
当我点击创建CS时,我应该能够填写表格并成功提交数据,并在cs_content内输入任何表格输入错误,错误应该显示在cs_content中。
当我点击创建RP时,我应该能够填写表格并成功提交数据并留言在rp_content和任何表格输入错误中,错误应显示在rp_content中。
home.php
< ;?php
需要' classes / class.report.php';
$ report = new 报告($ db);
?>
< html >
< head >
< script src = js / jqueryv1.10.2.js > < / script >
< 脚本 >
$(文件).ready(function()
{
// $(#cs_content)。show();
$(' #cs')。click(function()
{
$(' #cs_content')。fadeIn(' slow');
$(' #rp_content')。hide();
});
$(' #rp')。click(function()
{
$(' #rp_content')。fadeIn(' slow');
$(' #cs_content')。hide();
});
});
< / script >
< / head >
< body > ;
< div class = container2 >
< div style = margin:0px 0px; padding:3px 217px; overflow:hidden; >
< div id = cs style = float:left; margin:0px 0px; padding:7px; > < ; 输入 type < span class =code-keyword> = button 值 = 创建CS > < / div >
< div id = rp style = float:left; margin:0px 0px; padding:7px; > < input 类型 = 按钮 value = CREATE RP > < / d iv > < br >
< / div >
< div id = cs_content > <? php $报告 - > create_cs_report(); ?> < / div < span class =code-keyword>>
< div id = rp_content style = display:none; > <? php $ report-> create_rp_report(); ?> < / div < span class =code-keyword>>
< / div >
< / body >
< / html >
class.report.php
<?php
class 报告
{
private $ db ;
public function __construct($ database){
$ this-> db = $ database ;
}
公共函数create_cs_report()
{
if (isset($ _ POST [' create_es_report']))
{
$ report_name = htmlentities($ _ POST [' report_name']);
$ from_address = htmlentities($ _ POST [' FROM_ADDRESS' 跨度>]);
$ subject = htmlentities($ _ POST [' 主题'跨度>]);
$ reply_to = htmlentities($ _ POST [' REPLY_TO'跨度>]);
if (空($ _ POST [' report_name'])|| empty($ _ POST [' from_address' ])|| empty($ _ POST [' subject'])|| empty( $ _POST [' reply_to']))
{
$ errors [] = ' < span class =error>所有字段均为必填项。< / span>';
}
else
{
if (isset ($ _POST [' report_name'])&& empty($ _ POST [' report_name'])){$ errors [] = ' < span class =error>报告名称是必需的< / span>'; }
else if (!ctype_alnum($ _ POST [' report_name']))
{$ errors [] = ' < span class =error>报告名称:不允许空格,只需要字母和数字< / span>'; }
if (isset($ _ POST [' from_address'])&& empty($ _ POST [' from_address']))
{$ errors [] = ' < span class =error> ;需要地址< / span>'; }
else if (filter_var($ _ POST [' from_address'],FILTER_VALIDATE_EMAIL)=== false)
{$ errors [] = ' < span class =error>请输入有效的发件人地址< / span>'; }
if (isset($ _ POST [' 主题'])&& empty($ _ POST [' subject'])){$ errors [] = ' < span class =error>主题是必需的< ; /跨度>'跨度>; }
else if (!ctype_alnum($ _ POST [' subject']))
{$ errors [] = ' < span class =error>主题:不允许空格,只需要字母和数字< / span>'; }
if (isset($ _ POST [' reply_to'])&& empty($ _ POST [' reply_to'])){$ errors [] = ' < span class =error>回复是所需< /跨度>'跨度>; }
else if (filter_var($ _ POST [' reply_to'],FILTER_VALIDATE_EMAIL)=== false)
{$ errors [] = ' < span class =error>请输入有效的回复地址< / span>'; }
}
如果(空($ errors)=== true)
{
< span class =code-sdkkeyword> $ query = $ this-> db-> prepare( INSERT INTO报告(report_name,from_address,subject,reply_to)VALUES(?,?,?,?));
$ query-> bindValue(1,$ report_name);
$ query-> bindValue(2,$ from_address);
$ query-> bindValue(3,$ subject);
$ query-> bindValue(4,$ reply_to);
尝试 {
$ query-> execute();
}
catch(PDOException $ e){
die($ e-> getMessage());
}
标头(' 位置:home.php?success');
exit();
}
}
if (isset($ _ GET ['' success'])&& empty($ _ GET [' success']))
{
header(' 位置:home.php'跨度>);
echo ' < span class =错误>报告已成功创建< / span>';
}
?>
< 表格 action = 方法 = POST accept-charset = UTF-8 >
< div style = font-weight:bold; padding:17px 80px; text-decoration:und erline; > 第A节< / div >
< table class = create_report >
< ; tr > < td > < < span class =code-leadattribute> label > 报告名称< / label > < span 样式 = 颜色:#A60000 > * < / span > < / td >
< td > < input 类型 = text name = report_name 必需 占位符 = 报告名称 value < span class =code-keyword> = <?php if(isset($ _ POST [ report_name])) echo $ report_name; ? < span class =code-keyword>> size =30maxlength =30>
< / td > < / tr >
< tr > ; < td > < label > 来自< / label > < span 样式 = 颜色:#A60000 > * < / span > < / td >
< td > < 输入 类型 = text name = from_address 必需 placehol der = 来自地址 value = <?php if(isset ($ _POST [ from_address])) echo $ from_address; ? > size =30>
< / td > < / tr >
< tr > ; < td > < label > 主题< / label > < span style = 颜色:#A60000 > * < / span > < / td >
< td > < 输入 type = text name = subject 必需 placehol der = 主题 value = <?php if(isset( $ _POST [ subject])) echo $ subject; ? > size =30>
< / td > < / tr >
< tr > ; < td > < label > 回复要< / label > < span class =code-keyword>< span style = color:#A60000 > * < / span > < / td >
< td > < 输入 type = text name = reply_to 必需 placeh较旧的 = 回复地址 value = <?php if(isset ($ _POST [ reply_to])) echo $ reply_to; ? > size =30>
< / td > < / tr >
< tr > ; < td > < 输入 类型 = 提交 value = 创建报告 样式 = 背景:#8AC007;颜色:#080808;填充:6px; < span class =code-attribute> name = create_es_report > < / td > < / tr >
< / table >
< / form >
<? php
// 如果有错误,它们会显示在这里
如果(空($ errors)=== false){
echo ' < div>'。 implode(' < / p>< p>',$ errors)。 ' < / div>';
}
}
} // 报告课程结束
report = new 报告(
分贝);
?>
< html >
< head >
< script src = js / jqueryv1.10.2.js > < / script >
< 脚本 >
(document).ready(function ()
{
//
I want to create two reports and submit the report data to database by using two functions defined in a class: Here I have two buttons: "Create ES" and "Create RP".
Rightnow, my forms are working fine, I can insert data successfully, but the problem was when I click on submit after filling the form data, the content is hiding and displays the fist div content "cs_content" and again I need to onclick to submit again.
Could anyone give a solution for this.
Requirement :
When I click on "Create CS", I should be able to fill the form and submit data successfully with a message within "cs_content" and any form input errors, the errors should display within "cs_content".
When I click on "Create RP", I should be able to fill the form and submit data successfully with a message within "rp_content" and any form input errors, the errors should display within "rp_content".
home.php
<?php
require 'classes/class.report.php';
$report = new Report($db);
?>
<html>
<head>
<script src="js/jqueryv1.10.2.js"></script>
<script>
$ (document).ready(function ()
{
//$("#cs_content").show();
$('#cs').click(function ()
{
$('#cs_content').fadeIn('slow');
$('#rp_content').hide();
});
$('#rp').click(function ()
{
$('#rp_content').fadeIn('slow');
$('#cs_content').hide();
});
});
</script>
</head>
<body>
<div class="container2">
<div style="margin:0px 0px;padding:3px 217px;overflow:hidden;">
<div id="cs" style="float:left;margin:0px 0px;padding:7px;"><input type="button" value="CREATE CS"></div>
<div id="rp" style="float:left;margin:0px 0px;padding:7px;"><input type="button" value="CREATE RP"></div><br>
</div>
<div id="cs_content"> <?php $report->create_cs_report(); ?> </div>
<div id="rp_content" style="display:none;"> <?php $report->create_rp_report(); ?> </div>
</div>
</body>
</html>
class.report.php
<?php
class Report
{
private $db;
public function __construct($database){
$this->db = $database;
}
public function create_cs_report()
{
if (isset($_POST['create_es_report']))
{
$report_name = htmlentities($_POST['report_name']);
$from_address = htmlentities($_POST['from_address']);
$subject = htmlentities($_POST['subject']);
$reply_to = htmlentities($_POST['reply_to']);
if (empty($_POST['report_name']) || empty($_POST['from_address']) || empty($_POST['subject']) || empty($_POST['reply_to']))
{
$errors[] = '<span class="error">All fields are required.</span>';
}
else
{
if (isset($_POST['report_name']) && empty($_POST['report_name'])) { $errors[] = '<span class="error">Report Name is required</span>'; }
else if (!ctype_alnum($_POST['report_name']))
{ $errors[] = '<span class="error">Report Name: Whitespace is not allowed, only alphabets and numbers are required</span>'; }
if (isset($_POST['from_address']) && empty($_POST['from_address']))
{ $errors[] = '<span class="error">From address is required</span>'; }
else if (filter_var($_POST['from_address'], FILTER_VALIDATE_EMAIL) === false)
{ $errors[] = '<span class="error">Please enter a valid From address</span>'; }
if (isset($_POST['subject']) && empty($_POST['subject'])) { $errors[] = '<span class="error">Subject is required</span>'; }
else if (!ctype_alnum($_POST['subject']))
{ $errors[] = '<span class="error">Subject: Whitespace is not allowed, only alphabets and numbers are required</span>'; }
if (isset($_POST['reply_to']) && empty($_POST['reply_to'])) { $errors[] = '<span class="error">Reply To is required</span>'; }
else if (filter_var($_POST['reply_to'], FILTER_VALIDATE_EMAIL) === false)
{ $errors[] = '<span class="error">Please enter a valid Reply-To address</span>'; }
}
if (empty($errors) === true)
{
$query = $this->db->prepare("INSERT INTO report(report_name, from_address, subject, reply_to) VALUES (?, ?, ?, ?) ");
$query->bindValue(1, $report_name);
$query->bindValue(2, $from_address);
$query->bindValue(3, $subject);
$query->bindValue(4, $reply_to);
try {
$query->execute();
}
catch(PDOException $e) {
die($e->getMessage());
}
header('Location:home.php?success');
exit();
}
}
if (isset($_GET['success']) && empty($_GET['success']))
{
header('Location:home.php');
echo '<span class="error">Report is succesfully created</span>';
}
?>
<form action="" method="POST" accept-charset="UTF-8">
<div style="font-weight:bold;padding:17px 80px;text-decoration:underline;">Section A</div>
<table class="create_report">
<tr><td><label>Report Name</label><span style="color:#A60000">*</span></td>
<td><input type="text" name="report_name" required placeholder="Name of the report" value="<?php if(isset($_POST["report_name"])) echo $report_name; ?>" size="30" maxlength="30">
</td></tr>
<tr><td><label>From</label><span style="color:#A60000">*</span></td>
<td><input type="text" name="from_address" required placeholder="From address" value="<?php if(isset($_POST["from_address"])) echo $from_address; ?>" size="30">
</td></tr>
<tr><td><label>Subject</label><span style="color:#A60000">*</span></td>
<td><input type="text" name="subject" required placeholder="Subject" value="<?php if(isset($_POST["subject"])) echo $subject; ?>" size="30">
</td></tr>
<tr><td><label>Reply To</label><span style="color:#A60000">*</span></td>
<td><input type="text" name="reply_to" required placeholder="Reply address" value="<?php if(isset($_POST["reply_to"])) echo $reply_to; ?>" size="30">
</td></tr>
<tr><td><input type="submit" value="create report" style="background:#8AC007;color:#080808;padding:6px;" name="create_es_report"></td></tr>
</table>
</form>
<?php
//IF THERE ARE ERRORS, THEY WOULD BE DISPLAY HERE
if (empty($errors) === false) {
echo '<div>' . implode('</p><p>', $errors) . '</div>';
}
}
}//Report CLASS ENDS
report = new Report(
db); ?> <html> <head> <script src="js/jqueryv1.10.2.js"></script> <script>
(document).ready(function () { //
这篇关于使用显示/隐藏功能创建PHP表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!