Jump to content

Add News Error Message


laado

Recommended Posts

following is the code of adding news on web page

<title>hotnews</title><?php//LAST UPDATE// 27-09-2007include("config.php");  if(isset($_POST['submit']))  {//begin of if($submit).	  // Set global variables to easier names	  $title = $_POST['title'];	  $text1 = $_POST['text1'];	  $text2 = $_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.			  }// end of if		 //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=index.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>	  <?  }//end of else    ?>

when i not enter the title then it gives error News title is a required field. Please fill it. in next page. i want error will display in same page against title box.what changes to this code.reply urgently

Link to comment
Share on other sites

			  if(!$title){  //this means If the title is really empty.					 echo "Error: News title is a required field. Please fill it.";					 $error = "Error: News title is a required field. Please fill it.";					 exit(); //exit the script and don't do anything else.			  }// end of if

	  Title: <input name="title" size="40" maxlength="255"><?php echo $error; ?>

and instead of "else", try....

  // If the form has not been submitted, display it!if($done != "TRUE")  {//begin of else

and add $done="TRUE"; here

		  echo "<meta http-equiv=Refresh content=4;url=index.php>";		  $done="TRUE";  }//end of if($submit).

Link to comment
Share on other sites

can you please insert these lines of code in my code so that i can easily understand that where i add these lines.

			  if(!$title){  //this means If the title is really empty.					 echo "Error: News title is a required field. Please fill it.";					 $error = "Error: News title is a required field. Please fill it.";					 exit(); //exit the script and don't do anything else.			  }// end of if

	  Title: <input name="title" size="40" maxlength="255"><?php echo $error; ?>

and instead of "else", try....

  // If the form has not been submitted, display it!if($done != "TRUE")  {//begin of else

and add $done="TRUE"; here

		  echo "<meta http-equiv=Refresh content=4;url=index.php>";		  $done="TRUE";  }//end of if($submit).

Link to comment
Share on other sites

try this

<title>hotnews</title><?php//LAST UPDATE// 27-09-2007include("config.php");$error = "";  if(isset($_POST['submit']))  {//begin of if($submit).	  // Set global variables to easier names	  $title = $_POST['title'];	  $text1 = $_POST['text1'];	  $text2 = $_POST['text2'];			  //check if (title) field is empty then print error message.			  if(!$title){  //this means If the title is really empty.					 $error = "Error: News title is a required field. Please fill it.";					 exit(); //exit the script and don't do anything else.			  }// end of if		 //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=index.php>";				  $done = "TRUE";  }//end of if($submit).  // If the form has not been submitted, display it!if($done != "TRUE")  {//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"><?php echo $error ?>	  <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>	  <?  }//end of else    ?>

Link to comment
Share on other sites

problem cannot solved. when i press add button a blank page appear. whts wrong?

try this
<title>hotnews</title><?php//LAST UPDATE// 27-09-2007include("config.php");$error = "";  if(isset($_POST['submit']))  {//begin of if($submit).	  // Set global variables to easier names	  $title = $_POST['title'];	  $text1 = $_POST['text1'];	  $text2 = $_POST['text2'];			  //check if (title) field is empty then print error message.			  if(!$title){  //this means If the title is really empty.					 $error = "Error: News title is a required field. Please fill it.";					 exit(); //exit the script and don't do anything else.			  }// end of if		 //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=index.php>";				  $done = "TRUE";  }//end of if($submit).  // If the form has not been submitted, display it!if($done != "TRUE")  {//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"><?php echo $error ?>	  <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>	  <?  }//end of else    ?>

Link to comment
Share on other sites

Oh, you shouldnt have the exit, and you should hae an if/else... if you want to keep running hte page after the error for not filling in the title

<title>hotnews</title><?php//LAST UPDATE// 27-09-2007include("config.php");$error = "";  if(isset($_POST['submit']))  {//begin of if($submit).	  // Set global variables to easier names	  $title = $_POST['title'];	  $text1 = $_POST['text1'];	  $text2 = $_POST['text2'];			  //check if (title) field is empty then print error message.			  if(!$title){  //this means If the title is really empty.					 $error = "Error: News title is a required field. Please fill it.";			  }// end of if	  else 	  {		 //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=index.php>";				  $done = "TRUE";	  }  }//end of if($submit).  // If the form has not been submitted, display it!if($done != "TRUE")  {//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"><?php echo $error ?>	  <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>	  <?  }//end of else    ?>

Link to comment
Share on other sites

Thanx a lot the problem solved. now whts if i want to show error on every empty field like text one and text 2? please write code

Oh, you shouldnt have the exit, and you should hae an if/else... if you want to keep running hte page after the error for not filling in the title
<title>hotnews</title><?php//LAST UPDATE// 27-09-2007include("config.php");$error = "";  if(isset($_POST['submit']))  {//begin of if($submit).	  // Set global variables to easier names	  $title = $_POST['title'];	  $text1 = $_POST['text1'];	  $text2 = $_POST['text2'];			  //check if (title) field is empty then print error message.			  if(!$title){  //this means If the title is really empty.					 $error = "Error: News title is a required field. Please fill it.";			  }// end of if	  else 	  {		 //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=index.php>";				  $done = "TRUE";	  }  }//end of if($submit).  // If the form has not been submitted, display it!if($done != "TRUE")  {//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"><?php echo $error ?>	  <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>	  <?  }//end of else    ?>

Link to comment
Share on other sites

i try the following code

<title>hotnews</title><?php//LAST UPDATE// 27-09-2007include("config.php");$error = "";  if(isset($_POST['submit']))  {//begin of if($submit).	  // Set global variables to easier names	  $title = $_POST['title'];	  $text1 = $_POST['text1'];	  $text2 = $_POST['text2'];			  //check if (title) field is empty then print error message.			  if(!$title){  //this means If the title is really empty.					 $error = "Error: News title is a required field. Please fill it.";				  }			  if(!$text1){  //this means If the title is really empty.					 $error="Error: News text1 is a required field. Please fill it.";					 				}			   if(!$text2){  //this means If the title is really empty.					 $error="Error: News text2 is a required field. Please fill it.";								  			   }// end of if	  else 	  {		 //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=index.php>";				  $done = "TRUE";	  }  }//end of if($submit).  // If the form has not been submitted, display it!if($done != "TRUE")  {//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"><?php echo $error ?>	  <br>	  Text1: <textarea name="text1"  rows="7" cols="30"></textarea><?php echo $error ?>	  <br>	  Text2: <textarea name="text2" rows="7" cols="30"></textarea><?php echo $error ?>	  <br>	  <input type="submit" name="submit" value="Add News">	  </form>	  <?  }//end of else    ?>

but it gives error againest each field "Error: News text2 is a required field. Please fill it. "even i fill two fields and leave one field empty but it gives error against filled fields also.whts wrong in my code??

Link to comment
Share on other sites

you'll need a seperate variable for each, a variable can't store multiple strings and automatically know which you want(try $title_error, $text1_error, $text2_error)

Link to comment
Share on other sites

i try this code

<title>hotnews</title><?php//LAST UPDATE// 27-09-2007include("config.php");$error = "";$error1= "";$error2= "";  if(isset($_POST['submit']))  {//begin of if($submit).	  // Set global variables to easier names	  $title = $_POST['title'];	  $text1 = $_POST['text1'];	  $text2 = $_POST['text2'];			  //check if (title) field is empty then print error message.			  if(!$title){  //this means If the title is really empty.					 $error = "Error: News title is a required field. Please fill it.";				  				  }			  if(!$text1){  //this means If the title is really empty.					 $error1="Error: News text1 is a required field. Please fill it.";					 				 				}			   if(!$text2){  //this means If the title is really empty.					 $error2="Error: News text2 is a required field. Please fill it.";								  			   }// end of if	  else 	  {		 //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=index.php>";				  $done = "TRUE";	  }  }//end of if($submit).  // If the form has not been submitted, display it!if($done != "TRUE")  {//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"><?php echo $error ?>	  <br>	  Text1: <textarea name="text1"  rows="7" cols="30"></textarea><?php echo $error1 ?>	  <br>	  Text2: <textarea name="text2" rows="7" cols="30"></textarea><?php echo $error2 ?>	  <br>	  <input type="submit" name="submit" value="Add News">	  </form>	  <?  }//end of else    ?>

you'll need a seperate variable for each, a variable can't store multiple strings and automatically know which you want(try $title_error, $text1_error, $text2_error)
now it cannot give error and add news successfully even i left one or two fields empty now whts wrong? :)
Link to comment
Share on other sites

getting close, but now the "else" statement only works for the last error.Change this else:

	  else	  {		 //run the query which adds the data gathered from the form into the database

to this
	  if($error = "" && $error1 = "" && $error2 ="")	  {		 //run the query which adds the data gathered from the form into the database

Link to comment
Share on other sites

now it cannot give any error but it cannot show this message hank you! News added Successfully!<br>You'll be redirected to Home Page after (4) Seconds"now whts wrong??

getting close, but now the "else" statement only works for the last error.Change this else:to this
	  if($error = "" && $error1 = "" && $error2 ="")	  {		 //run the query which adds the data gathered from the form into the database

Link to comment
Share on other sites

still unsolved. now the news add easily without giving any error. if i left all fields blank but news add . now whts wrong??

if($error != "" || $error1 != "" || $error2 !="")my bad, havnt got breakfast in me yet :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...