Php 对动态生成的表单实施验证码
我有一些功能可以生成我页面上的文章以及与之相关的评论:Php 对动态生成的表单实施验证码,php,sql,Php,Sql,我有一些功能可以生成我页面上的文章以及与之相关的评论: function comment_form($id) { // generates a comment box form for every article on the page global $user_data; if (logged_in() === true) { echo " <form method='post' action='' class='comments_for
function comment_form($id) { // generates a comment box form for every article on the page
global $user_data;
if (logged_in() === true) {
echo "
<form method='post' action='' class='comments_form'>
<input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'>
<div class='captcha'>" . create_captcha() . "</div>
<textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>
<input type='hidden' name='blog_id' value='$id'>
<input type='submit' name='submit' id='post' value='post'>
</form>
<hr class='artline'>";
}
}
function list_articles($rows) {
if (empty($rows)) {
return "There are no Articles to display";
}
$previous_blog_id = 0;
$content = '';
foreach ($rows as $row) {
if ($previous_blog_id != $row['content_id']) { // the blog id changed
if ($previous_blog_id != 0) { // not the first section, close out the previous section
$content .= comment_form($previous_blog_id);
}
// start a new blog section
$content .= "<h5 class='posted_by'>Posted by {$row['posted_by']} on {$row['date']}</h5>
<h1 class='content_headers'>{$row['title']}</h1>
<article>{$row['content']}</article>
<hr class='artline'>";
$previous_blog_id = $row['content_id'];
}
if (!empty($row['comment_by']) && !empty($row['comments'])) {
$content .= "<div class='commented_by'>User: {$row['comment_by']} </div>
<div class='comments'>Comment: {$row['comments']}</div>
<hr class='artline2'>";
}
}
if ($previous_blog_id != 0) {
$content .= comment_form($previous_blog_id);
}
return $content;
}
function insert_comments($comments, $comment_by, $blog_id) {
include('core/db/db_connection.php');
$comment_by = sanitize($comment_by);
$comments = sanitize($comments);
$blog_id = (int)$blog_id;
$sql = "
INSERT INTO article_comments (
comments,
comment_by,
blog_id
)
VALUES (
'$comments',
'$comment_by',
'$blog_id'
)
";
mysqli_query($dbCon, $sql);
}
我期望的行为是:
Article_1 title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
-------------------------------------- //comments
Name: User0
Comment: Great article!
--------------------------------------
Name: User1
Comment: Great article! - 2nd comment
-------------------------------------- // end comments
|-------------------------------------| // comments form for article 1
|Name: New User |
|Comment: New comment ! |
| |
|-------------------------------------|
[Submit] [captcha field]
============================================================
Article_2 title: LOREM IPSUM
Content: LOREM IPSUM DOLOR SIT AMET....
-------------------------------------- //comments
Name: User0
Comment: Great article!
--------------------------------------
Name: User1
Comment: Great article! - 2nd comment
-------------------------------------- // end comments
|-------------------------------------| // comments form for article 2
|Name: New User |
|Comment: New comment ! |
| |
|-------------------------------------|
[Submit] [captcha field]
这是否与我插入generate\u captcha
函数的位置有关,该函数会导致注释框浮动在内容上方
编辑:如果我返回表单而不是回显表单,则此操作有效。评论表格位于相应文章下方:
function comment_form($id) {
global $user_data;
if (logged_in() === true) {
return <<<EOT
<form method='post' action='' class='comments_form'>
<input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'>
<textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>
<input type='hidden' name='blog_id' value='$id'>
<input type='submit' name='submit' id='post' value='post'>
</form>
<hr class='artline'>
EOT;
如何返回create_blog_captcha的值?我显然做错了…在函数create\u captcha()中,您没有关闭隐藏的输入字段,因此中断了文档的整个流程
function create_captcha() {
$num1 = generate_captcha(1, 20);
$num2 = generate_captcha(1, 20);
return $num1 . ' + ' . $num2 . ' =
<input type="text" name="captcha_results" size="2">
<input type="hidden" name=\'num1\' value=' . $num1 . ' />
<input type="hidden" name=\'num2\' value=' . $num2 . ' />';
}
函数创建\u验证码(){
$num1=生成验证码(1,20);
$num2=生成验证码(1,20);
返回$num1.'+.$num2'=
';
}
正如您所注意到的,该函数以前会回显内容,而不是作为字符串返回到另一个输出html的函数。您应该能够将这些框放置在您喜欢的位置-检查css!这些都是动态生成的,CSS仅为此进行了优化。当用户发布新文章时,将在该文章下方生成评论表单。如果我从注释表单中删除
create\u captcha
函数,我会得到预期的行为。所以我不认为这是一个css问题。我认为这与我将create\u captcha
函数插入comment\u表单
函数的方式有关,但我无法理解啊!我希望这会奏效,这似乎是正确的方法。然而,我仍然得到与以前完全相同的行为。注释框浮动在文章内容上方如果html流在源代码视图中看起来正确,那么我说如果隐藏字段现在正确关闭,这一定是css问题。我注意到,这个问题与我回显表单的事实有关。请看我更新的问题好发现。。。从create_captcha()函数返回字符串
function comment_form($id) {
global $user_data;
if (logged_in() === true) {
return <<<EOT
<form method='post' action='' class='comments_form'>
<input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'>
<textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>
<input type='hidden' name='blog_id' value='$id'>
<input type='submit' name='submit' id='post' value='post'>
</form>
<hr class='artline'>
EOT;
function comment_form($id, $captcha) {
global $user_data;
if (logged_in() === true) {
return <<<EOT
<form method='post' action='' class='comments_form'>
<input type='text' name='username' placeholder='your name... *' id='name' value='{$user_data['username']}'>
<textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'></textarea>
<input type='hidden' name='blog_id' value='$id'>
<input type='submit' name='submit' id='post' value='post'>
</form>
<hr class='artline'>
EOT;
}
}
function list_articles($rows) {
if(empty($rows)){
return "There are no Articles to display";
}
$create_blog_captcha = create_blog_captcha();
$previous_blog_id = 0;
$content = '';
foreach($rows as $row) {
if ($previous_blog_id != $row['content_id']) { // the blog id changed
if($previous_blog_id != 0) { // not the first section, close out the previous section
$content .= comment_form($previous_blog_id, $create_blog_captcha);
}
// start a new blog section
$content .= "<h5 class='posted_by'>Posted by {$row['posted_by']} on {$row['date']}</h5>
<h1 class='content_headers'>{$row['title']}</h1>
<article>{$row['content']}</article>
<hr class='artline'>";
$previous_blog_id = $row['content_id'];
}
if (!empty($row['comment_by']) && !empty($row['comments'])) {
$content .= "<div class='commented_by'>User: {$row['comment_by']} </div>
<div class='comments'>Comment: {$row['comments']}</div>
<hr class='artline2'>";
}
}
if($previous_blog_id != 0){
$content .= comment_form($previous_blog_id, $create_blog_captcha);
}
return $content;
}
function create_blog_captcha() {
$num1 = generate_captcha(1, 20);
$num2 = generate_captcha(1, 20);
$captchanum = $num1 . ' + ' . $num2 . ' = ';
$captchanum .= '<input type="text" name="captcha_results" size="2">
<input type="hidden" name=\'num1\' value=' . $num1 . '>
<input type="hidden" name=\'num2\' value=' . $num2 . '>';
return $captchanum;
}
function create_captcha() {
$num1 = generate_captcha(1, 20);
$num2 = generate_captcha(1, 20);
return $num1 . ' + ' . $num2 . ' =
<input type="text" name="captcha_results" size="2">
<input type="hidden" name=\'num1\' value=' . $num1 . ' />
<input type="hidden" name=\'num2\' value=' . $num2 . ' />';
}