PDA

View Full Version : Display Errors Against Text Boxes, display error on next page why?



muskan
06-05-2009, 12:27 PM
its the code of adding text in website. but when i leave a box blank then it display an error on next. i want that it display error on same page against text boxes.

here is the code





<?php include("C:\\Program Files\\VertrigoServ\\www\\portal\\password_protect .php"); ?>
<html> <title>hotnews</title>

<body background=new2.jpg>
<?php



include("config.php");


if(isset($_POST['submit']))
{//begin of if($submit).
// Set global variables to easier names
$title = mysql_escape_string($_POST['title']);

$text1 = mysql_escape_string($_POST['text1']);

$text2 = mysql_escape_string($_POST['text2']);


//check if (title) field is empty then print error message.
if(!$title){ //this means If the title is really empty.
echo "Error: News title is a required field. Please fill it.";


exit(); //exit the script and don't do anything else.


}
if(!$text1){ //this means If the title is really empty.
echo "Error: News text1 is a required field. Please fill it.";
exit();
}
if(!$text2){ //this means If the title is really empty.
echo "Error: News text2 is a required field. Please fill it.";
exit();
}// end of if

if ((bool) preg_match('/[0-9]/', $title)) {
echo "Error: News title must not contain numbers. Please refill it, with all numbers removed.<br><b>Sorry! Please add only text data !<br>You'll be redirected to ADD Latest News Page after (4) Seconds";

echo "<meta http-equiv=Refresh content=4;url=ALN.php>";
exit();
}
if ((bool) preg_match('/[0-9]/', $text1)) {
echo "Error: News title must not contain numbers. Please refill it, with all numbers removed.<br><b>Sorry! Please add only text data !<br>You'll be redirected to ADD Latest News Page after (4) Seconds";


echo "<meta http-equiv=Refresh content=4;url=ALN.php>";

exit();
}
if ((bool) preg_match('/[0-9]/', $text2)) {
echo "Error: News title must not contain numbers. Please refill it, with all numbers removed.<br><b>Sorry! Please add only text data !<br>You'll be redirected to ADD Latest News Page after (4) Seconds";


echo "<meta http-equiv=Refresh content=4;url=ALN.php>";

exit();
}
//run the query which adds the data gathered from the form into the database
$result = mysql_query("INSERT INTO news (title, dtime, text1, text2)
VALUES ('$title',NOW(),'$text1','$text2')",$connect);
//print success message.
echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
echo "<meta http-equiv=Refresh content=4;url=ALN.php>";
}//end of if($submit).


// If the form has not been submitted, display it!
else
{//begin of else

?>

<br>
<h3>::Add News</h3>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">

Title: <input name="title" size="40" maxlength="255">
<br>
Text1: <textarea name="text1" rows="7"cols="30"></textarea>
<br>
Text2: <textarea name="text2" rows="7" cols="30"></textarea>
<br>
<input type="submit" name="submit" value="Add News">
</form>
<?php
}//end of else
?>
</body>
</html>





i need urgent help please

pghcollectibles
06-05-2009, 01:10 PM
instead of echoing the error when it happens, set it as a variable then wherever you want the error to display, echo the variable

muskan
06-05-2009, 02:44 PM
can you please explain with the help of example?

pghcollectibles
06-05-2009, 08:44 PM
change this:

echo "Error: News title is a required field. Please fill it.";to this:

$error_news_title = "Error: News title is a required field. Please fill it.";im not sure what you mean about displaying "against" text boxes.

wherever you want the code to be displayed, just put this:

echo $error_news_title;and when you place your

<meta http-equiv=Refresh content=4;url=ALN.php>it needs to be in the <head></head> section.

you have this:

<html> <title>hotnews</title>

<body background=new2.jpg>but it should be this:

<html><head><title>hotnews</title>
<meta http-equiv=Refresh content=4;url=ALN.php>
</head>
<body background=new2.jpg>so if you want to choose whether to add the redirect or not (i would not use it either by the way... its not really the right application for it here)
you might do something like this:

echo "<html><head><title>hotnews</title>";
if (isset($_POST['submit']) && (!$title || !$text1 || !$text2)){
echo "<meta http-equiv=Refresh content=4;url=ALN.php>";
}
echo "</head><body background=new2.jpg>";i keep seeing things that dont look right to me. actually i would rewrite what you have a lot more.

Explain where you would like the error message. In the text area? under? over?
i can understand you not wanting the title to have digits but you also seemed to copy the same for text1 and text2 is that correct?

answer those and ill show you how i think it should be.

muskan
06-06-2009, 01:02 PM
i want to display error message under or over the text box. and i also not wanting the title,text1 and text2 to have digits. it any one put digits then it should display an error.

in short i just want two things. any one leave any text box blank then it should display error under or over the text box.

second i want that if any one enter digits in any text box(title,text1 or text2) then it should display an error that digits are not allowed or what ever. just thats my conditions.

pghcollectibles
06-19-2009, 08:43 PM
sorry i forgot about this thread. here is the code if you still need it:

<?php

include("C:\\Program Files\\VertrigoServ\\www\\portal\\password_protect .php");
include("config.php");

//--------------------------------------------------------------------------------------------------------------------------

if(isset($_POST['submit'])) {

$title = mysql_escape_string($_POST['title']);
$text1 = mysql_escape_string($_POST['text1']);
$text2 = mysql_escape_string($_POST['text2']);

$error="no";
if (!$title) {
$title_error = "Error: News title is a required field. Please fill it.<br>";
$error="yes";
}
if (!$text1) {
$text1_error = "Error: News text1 is a required field. Please fill it.<br>";
$error="yes";
}
if (!$text2) {
$text2_error = "Error: News text2 is a required field. Please fill it.<br>";
$error="yes";
}

$num_error .= "Error: News must not contain numbers, text only.<br>&nbsp;&nbsp;Please refill it with all numbers removed.<br>";
if ((bool) preg_match('/[0-9]/', $title)) {
$title_error .= $num_error;
$error="yes";
}
if ((bool) preg_match('/[0-9]/', $text1)) {
$text1_error .= $num_error;
$error="yes";
}
if ((bool) preg_match('/[0-9]/', $text2)) {
$text2_error .= $num_error;
$error="yes";
}

$meta = "";
//run the query which adds the data gathered from the form into the database
if ($error=="no"){
$result = mysql_query("INSERT INTO news (title, dtime, text1, text2) VALUES ('$title',NOW(),'$text1','$text2')",$connect);

//print success message.
$success = "<br>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds";
$meta = "<meta http-equiv=Refresh content=4;url=ALN.php>";
}
}

//--------------------------------------------------------------------------------------------------------------------------

$main = "<br><h3>::Add News</h3><br>";
$main .= "<form method='POST' action=''>";
$main .= "{$title_error}Title: <input name='title' size='40' maxlength='255'><br>";
$main .= "{$text1_error}Text1: <textarea name='text1' rows='7'cols='30'></textarea><br>";
$main .= "{$text2_error}Text2: <textarea name='text2' rows='7' cols='30'></textarea><br>";
$main .= "<input type='submit' name='submit' value='Add News'></form>";

//--------------------------------------------------------------------------------------------------------------------------

echo "<html><head><title>hotnews</title>{$meta}</head><body background=new2.jpg>";
if (isset($success)) {
echo $success;
} else {
echo $main;
}
echo "</body></html>";

?>