我希望能够在网络表单上按下提交按钮后在同一页面上显示错误

发布于 2025-01-01 13:58:35 字数 5709 浏览 1 评论 0原文

我目前有一个用 html 编程的网络表单,并且有一个 php 内容处理程序,它检查错误,并执行所有需要完成的操作,但如果有错误,我很乐意在网络表单页面本身上显示错误,我不知道如何做到这一点,但我可以发送我的代码。

关于这个问题的任何帮助都会很有用,谢谢。

HTML DOC

Do you like the content on our website? Do you want to be a part of it? Want to earn more Karma?<br />
Do you have what it takes to be a content creator? Then sign up here!<br> <br>
<?php 
function getUID()
{
     global $user;
     if ($user->uid) 
     { 
          $userID=$user->uid;
          echo $userID;
     }
     else
    {
          header('Location: http://brokendiamond.org/?q=node/40');
    }
}
function getUN()
{
     global $user;
     if ($user->uid) 
     { 
          $username=$user->name;
     }
     echo $username;
}

?>
<form name="input" action="php-scripts/sendmail.php" method="post">
Username: &nbsp;&nbsp;&nbsp;&nbsp
<input type="hidden" name="UserID" value="<?php getUID() ?>">
<input type="text" name="username" value="<?php getUN() ?>" /><br>
E-mail adress: 
<input type="text" name="mail" /><br><br>
What type of 'Content Creator' do you want to become? <br />
<input type="radio" name="CCT" value="Blogger" /> Blogger<br />
<input type="radio" name="CCT" value="Livestreamer" /> Livestreamer<br> <br>
What's your motivation?<br />
<textarea name="motivation" cols="60" rows="6"></textarea><br><br>
Why should we pick you as content creator?<br />
<textarea name="whyshouldwe" cols="60" rows="6"></textarea><br><br>
Do you have some reference material?<br />
<textarea name="reference" cols="60" rows="6"></textarea><br><br>
<h3>Rules to content creation</h3>
<p>You can only submit this once every day, the other versions will not be read, and you will lose Karma for each submit.<br />
When u submit this form, we will examine your account, and we will take a close look to your reference material.<br>
<h4>For Livestreamers Only</h4>
If we think you have what it takes to be a livestreamer ( frequent hours required ) we will examine your stream, and your computer/internet potential.<br />
If that is good enough for the website, you'll become a content creator.<br>
<h4>For Bloggers</h4>
If we think you are blogging material for out website, you will become a content creator, once you are accepted onto the team we will track your progress.<br />
If however you neglect your power or publish inappropriate content, then we will have no choice but to remove you from our team and will revert you to a regular user account.<br><br>
<input name="Send" type="submit" id="art-button-wrapper" value="I donate 500 Karma, and want to become a 'Content Creator'!" />
</form>

PHP 处理程序 DOC

<?php 
//get content out of form
$nick = $_REQUEST['username'];
$mail = $_REQUEST['mail'];
$cct = $_REQUEST['CCT'];
$motiv = $_REQUEST['motivation'];
$why = $_REQUEST['whyshouldwe'];
$ref = $_REQUEST['reference'];
$userID = $_REQUEST['UserID'];
$errors = '';

//database connection script
include "DBConnection.php";

//get connection
$con = getconnection();

//is connection available
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

//select database
mysql_select_db("brokendi_BD", $con);

//get current karma
$result = mysql_query("SELECT * FROM userpoints WHERE uid='$userID'");
$row = mysql_fetch_array($result);
$currentkarma = (int)$row['points'];


//check karma level
if ($currentkarma < 499)
{
        $errors .= "\n Error: Not enough karma";
}

//check nickname
if(empty($nick))
{
    $errors .= "\n Error: Username is required";
}

//check e-mail field
if(empty($mail))
{
    $errors .= "\n Error: E-mail is required";
}

//check cct field
if(empty($cct))
{
    $errors .= "\n Error: Content Creator Type is required";
}

//e-mail validation
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$mail))
{
    $errors .= "\n Error: Invalid email address";
}

//send e-mail and deduct karma
if( empty($errors))
{
        $newkarma = $currentkarma-500;
        mysql_query("UPDATE userpoints SET points = '$newkarma' WHERE uid='$userID'");

    $to = "[email protected]"; 
    $email_subject = "$nick: I want to become a content creator";
    $email_body = " Nickname: $nick \n Email: $mail\n Content Creator Type: $cct\n Motivation:\n $motiv\n\n Why should we accept you:\n $why\n\n reference:\n $ref"; 

    mail($to,$email_subject,$email_body,"From: $mail");
    mail($mail,"Content Creator application","Dear $nick,\n\nYour application has been recieved and will be answered as soon as we can, usually this takes less than 24 hours,\nbut occasionaly this might take us longer.\n\nKind regards,\nThe Broken Diamond Team","From: [email protected]");
    header('Location: http://brokendiamond.org');
}

//close connection
mysql_close($con);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

i currently have a webform, programmed in html, and i have a php content handler, wich checks for errors, and does all what needz to be done, but i would love to show the errors if there are errors, on the webform page itself, i don't have a clue how to do this, but i could send my code.

Any help on this matter would be useful, thanks.

HTML DOC

Do you like the content on our website? Do you want to be a part of it? Want to earn more Karma?<br />
Do you have what it takes to be a content creator? Then sign up here!<br> <br>
<?php 
function getUID()
{
     global $user;
     if ($user->uid) 
     { 
          $userID=$user->uid;
          echo $userID;
     }
     else
    {
          header('Location: http://brokendiamond.org/?q=node/40');
    }
}
function getUN()
{
     global $user;
     if ($user->uid) 
     { 
          $username=$user->name;
     }
     echo $username;
}

?>
<form name="input" action="php-scripts/sendmail.php" method="post">
Username:     
<input type="hidden" name="UserID" value="<?php getUID() ?>">
<input type="text" name="username" value="<?php getUN() ?>" /><br>
E-mail adress: 
<input type="text" name="mail" /><br><br>
What type of 'Content Creator' do you want to become? <br />
<input type="radio" name="CCT" value="Blogger" /> Blogger<br />
<input type="radio" name="CCT" value="Livestreamer" /> Livestreamer<br> <br>
What's your motivation?<br />
<textarea name="motivation" cols="60" rows="6"></textarea><br><br>
Why should we pick you as content creator?<br />
<textarea name="whyshouldwe" cols="60" rows="6"></textarea><br><br>
Do you have some reference material?<br />
<textarea name="reference" cols="60" rows="6"></textarea><br><br>
<h3>Rules to content creation</h3>
<p>You can only submit this once every day, the other versions will not be read, and you will lose Karma for each submit.<br />
When u submit this form, we will examine your account, and we will take a close look to your reference material.<br>
<h4>For Livestreamers Only</h4>
If we think you have what it takes to be a livestreamer ( frequent hours required ) we will examine your stream, and your computer/internet potential.<br />
If that is good enough for the website, you'll become a content creator.<br>
<h4>For Bloggers</h4>
If we think you are blogging material for out website, you will become a content creator, once you are accepted onto the team we will track your progress.<br />
If however you neglect your power or publish inappropriate content, then we will have no choice but to remove you from our team and will revert you to a regular user account.<br><br>
<input name="Send" type="submit" id="art-button-wrapper" value="I donate 500 Karma, and want to become a 'Content Creator'!" />
</form>

PHP Handler DOC

<?php 
//get content out of form
$nick = $_REQUEST['username'];
$mail = $_REQUEST['mail'];
$cct = $_REQUEST['CCT'];
$motiv = $_REQUEST['motivation'];
$why = $_REQUEST['whyshouldwe'];
$ref = $_REQUEST['reference'];
$userID = $_REQUEST['UserID'];
$errors = '';

//database connection script
include "DBConnection.php";

//get connection
$con = getconnection();

//is connection available
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

//select database
mysql_select_db("brokendi_BD", $con);

//get current karma
$result = mysql_query("SELECT * FROM userpoints WHERE uid='$userID'");
$row = mysql_fetch_array($result);
$currentkarma = (int)$row['points'];


//check karma level
if ($currentkarma < 499)
{
        $errors .= "\n Error: Not enough karma";
}

//check nickname
if(empty($nick))
{
    $errors .= "\n Error: Username is required";
}

//check e-mail field
if(empty($mail))
{
    $errors .= "\n Error: E-mail is required";
}

//check cct field
if(empty($cct))
{
    $errors .= "\n Error: Content Creator Type is required";
}

//e-mail validation
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$mail))
{
    $errors .= "\n Error: Invalid email address";
}

//send e-mail and deduct karma
if( empty($errors))
{
        $newkarma = $currentkarma-500;
        mysql_query("UPDATE userpoints SET points = '$newkarma' WHERE uid='$userID'");

    $to = "[email protected]"; 
    $email_subject = "$nick: I want to become a content creator";
    $email_body = " Nickname: $nick \n Email: $mail\n Content Creator Type: $cct\n Motivation:\n $motiv\n\n Why should we accept you:\n $why\n\n reference:\n $ref"; 

    mail($to,$email_subject,$email_body,"From: $mail");
    mail($mail,"Content Creator application","Dear $nick,\n\nYour application has been recieved and will be answered as soon as we can, usually this takes less than 24 hours,\nbut occasionaly this might take us longer.\n\nKind regards,\nThe Broken Diamond Team","From: [email protected]");
    header('Location: http://brokendiamond.org');
}

//close connection
mysql_close($con);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像极了他 2025-01-08 13:58:35

这不是在 Drupal 中编码的正确方法。
您可以使用内置的表单集错误来根据需要显示。
请浏览一些drupal 教程,了解表单,并且可能是模块开发 以及。
我以为这是 PHP 代码,直到我注意到 $user->uid :)
对于数据库相关 API

This is not proper way to code in Drupal.
You can use inbuilt form set errors to display as you wanted.
please go through some drupal tutorials for forms and may be module development as well.
I thought this is PHP code until i noticed $user->uid :)
For DB related APIs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文