Place a error message on the same page

I am trying to make a simple program that can add record to a database. However I came into a problem that when there is an error it will show it to another page.

I was wondering if it can be done to show error on the same page. The error message should only show then there is a problem with the inputs.

Name:[ ]
Address:[ ]
Phone:[ ]

Error:(error message should be here)

[Submit]

Will that be possible in PHP?
Thanks

Is the php on the same page?

Hi Richie,

Thank you so much for the quick reply.

Yes. On the same page.

On my example after clicking submit the error should show up in the above submit button. But if no error found, it will prompt one record added.

Thanks.

Maybe instead of ‘Error’ i will use ‘message to user’

Name:[ ]
Address:[ ]
Phone:[ ]

Message to user:(error message should be here)

[Submit]

Example message will be:

  1. Name already exist
  2. invalid phone number
  3. need to enter address field.
    and so on…

Thanks.

You have a couple of options:

  1. Use JavaScript to validate the form client side, so that the form isn’t submitted until the correct values are entered, then when it is submitted it can be submitted to the same or another page.

  2. Do the validation server side, submit the form to the same page then show the error message if there’s a problem or if there’s no problem, redirect it wherever you want after you’ve done the insert.

We need to see your code and possibly the whole page for a more detailed explanation.

xerxes: with all due respect, option 1 and 2 are compulsory and need to be both done. Not just one or the other. Client-side validation is all fine and good…until the user turns JS off.

xerxes: with all due respect, option 1 and 2 are compulsory and need to be both done. Not just one or the other. Client-side validation is all fine and good...until the user turns JS off.

I’m of the opinion that if a client is savvy enough to turn off JavaScript, he’s savvy enough to deal with a PHP error message. ;D

You’re still doing both sanitizations. The really bad advice is to tell people to do one or the other - they need to do the PHP one no matter what. Only the JS one is optional.

Thank you for all your comments. However, my last programming experience was year 1998( Cobol and Clipper '87). Hoping you heard those language.

I am now starting again to practice and do programming again so I am totally new in this area.

I do not know how to make a java script. :frowning:

If you want to see my code here is it. It is very very simple no security no nothing. Just a database encoding as a start.

[php]

Books
<tr>
	<td>Title:</td>
	<td><input type="text" name="title" value="None" /></td>
</tr>
<tr>
	<td>Author</td>
	<td><input type="text" name="author" value="None" /></td>
</tr>
<tr>
	<td>Publisher Name</td>
	<td><input type="text" name="name" value="None" /></td>
</tr>
<tr>
	<td>Copyright Year</td>
	<td><input type="text" name="copy" value="1999" /></td>
</tr>

<tr>
	<td>First Number</td>
	<td><input type="text" name="numb1" value="2" /></td>
</tr>

<tr>
	<td>Second Number</td>
	<td><input type="text" name="numb2" value="1" /></td>
</tr>

<tr>
	<td>&nbsp;</td>
	<td><input type="submit" name="submit" value="add" /></td>
</tr>
<?php if (isset($_POST['submit'])) { include 'db.php';//Connect to database $title=$_POST['title'] ; $author= $_POST['author'] ; $name=$_POST['name'] ; $copy=$_POST['copy'] ; $num1=$_POST['numb1']; $num2=$_POST['numb2']; $title_search = mysql_query("SELECT Title FROM books WHERE Title='$title' AND Author='$author'"); $tot_num = $_POST['numb1'] + $_POST['numb2'] * 100; Echo $tot_num; // if(mysql_num_rows($title_search) != 0) { // echo 'Record Already Exist!'; // } else { // mysql_query("INSERT INTO `books`(Title, Author, PublisherName, CopyrightYear) VALUES ( // '$title', '$author', '$name', '$copy')"); } mysql_close($conn); ?>
		<?php
		include("db.php");
		
			
		$result=mysql_query("SELECT * FROM books");
		
		while($test = mysql_fetch_array($result))
		{
			$id = $test['BookID'];	
			echo "<tr align='center'>";	
			echo"<td><font color='black'>" .$test['BookID']."</font></td>";
			echo"<td><font color='black'>" .$test['Title']."</font></td>";
			echo"<td><font color='black'>". $test['Author']. "</font></td>";
			echo"<td><font color='black'>". $test['PublisherName']. "</font></td>";
			echo"<td><font color='black'>". $test['CopyrightYear']. "</font></td>";	
			echo"<td> <a href ='view.php?BookID=$id'>Edit</a>";
			echo"<td> <a href ='del.php?BookID=$id'><center>Delete</center></a>";
								
			echo "</tr>";
		}
		mysql_close($conn);
		?>

[/php]

With the help of richie i manage to show the error message.

I am very sorry for the code I send was testing how to compute numbers from a $_POST and the database append was remarked.

Here is the code again

[php]

Books
<tr>
	<td>Title:</td>
	<td><input type="text" name="title" value="None" /></td>
</tr>
<tr>
	<td>Author</td>
	<td><input type="text" name="author" value="None" /></td>
</tr>
<tr>
	<td>Publisher Name</td>
	<td><input type="text" name="name" value="None" /></td>
</tr>
<tr>
	<td>Copyright Year</td>
	<td><input type="text" name="copy" value="1999" /></td>
</tr>

<tr>
	<td>First Number</td>
	<td><input type="text" name="numb1" value="2" /></td>
</tr>

<tr>
	<td>Second Number</td>
	<td><input type="text" name="numb2" value="1" /></td>
</tr>

<tr>
	<td>&nbsp;</td>
	<td><input type="submit" name="submit" value="add" /></td>
</tr>
<?php if (isset($_POST['submit'])) { include 'db.php';//Connect to database $title=$_POST['title'] ; $author= $_POST['author'] ; $name=$_POST['name'] ; $copy=$_POST['copy'] ; $num1=$_POST['numb1']; $num2=$_POST['numb2']; $title_search = mysql_query("SELECT Title FROM books WHERE Title='$title' AND Author='$author'"); $tot_num = $_POST['numb1'] + $_POST['numb2'] * 100; Echo $tot_num; if(mysql_num_rows($title_search) != 0) { echo 'Record Already Exist!'; } else { mysql_query("INSERT INTO `books`(Title, Author, PublisherName, CopyrightYear) VALUES ( '$title', '$author', '$name', '$copy')"); } mysql_close($conn); ?>
		<?php
		include("db.php");
		
			
		$result=mysql_query("SELECT * FROM books");
		
		while($test = mysql_fetch_array($result))
		{
			$id = $test['BookID'];	
			echo "<tr align='center'>";	
			echo"<td><font color='black'>" .$test['BookID']."</font></td>";
			echo"<td><font color='black'>" .$test['Title']."</font></td>";
			echo"<td><font color='black'>". $test['Author']. "</font></td>";
			echo"<td><font color='black'>". $test['PublisherName']. "</font></td>";
			echo"<td><font color='black'>". $test['CopyrightYear']. "</font></td>";	
			echo"<td> <a href ='view.php?BookID=$id'>Edit</a>";
			echo"<td> <a href ='del.php?BookID=$id'><center>Delete</center></a>";
								
			echo "</tr>";
		}
		mysql_close($conn);
		?>

[/php]

Need to make sure that math is right, remember the order of operations, unless php is literal (not sure on this subject).

That is true richie. sometimes my mistake are common. Like ; or ’ for example.

Thank you all for guiding me. Hoping some day I may contribute something here in the forum.

If you’re learning and new to PHP and possibly HTML, given that you’ve come from COBOL :D, there’s a couple of things I would suggest:

  1. Forget mysql_query it’s old and will be dropped from future versions of PHP at some point, so you may as well get to grips with the newer alternatives now, rather than having to re-learn them in the future and possibly update sites that you’ve built. Take a look at mysqli: http://www.php.net/manual/en/intro.mysqli.php or PDO: http://www.php.net/manual/en/intro.pdo.php. I like PDO because it can be used to connect to several different databases, not just MySQL.

  2. Have a look into how you split up the server side processing and the output to browser parts of your page/code. Typically purely server side code is placed above the HTML, with the exception of code that produces output, for example loops where you are displaying rows from a recordset, or array. For example:

[php]<?php
$errorMessage = ‘’;
if (isset($_POST[‘submit’])){
include ‘db.php’;//Connect to database
$title=$_POST[‘title’] ;
$author= $_POST[‘author’] ;
$name=$_POST[‘name’] ;
$copy=$_POST[‘copy’] ;
$num1=$_POST[‘numb1’];
$num2=$_POST[‘numb2’];
$title_search = mysql_query(“SELECT Title FROM books WHERE Title=’$title’ AND Author=’$author’”);
$tot_num = $_POST[‘numb1’] + $_POST[‘numb2’] * 100;
echo $tot_num;
if(mysql_num_rows($title_search) != 0) {
$errorMessage = ‘Record Already Exist!’;
}else {
mysql_query(“INSERT INTO books(Title, Author, PublisherName, CopyrightYear) VALUES (’$title’, ‘$author’, ‘$name’, ‘$copy’)”);
}
}
mysql_close($conn);
?>

Books <?php if ($errorMessage != ''){echo $errorMessage;} ?>
Title:
Author
Publisher Name
Copyright Year
First Number
Second Number
 
<?php include("db.php"); $result=mysql_query("SELECT * FROM books"); while($test = mysql_fetch_array($result)){ $id = $test['BookID']; echo ""; echo""; echo""; echo""; echo""; echo""; echo""; } mysql_close($conn); ?>
" .$test['BookID']."" .$test['Title']."". $test['Author']. "". $test['PublisherName']. "". $test['CopyrightYear']. " Edit"; echo" Delete"; echo "
[/php]
  1. I noticed a couple of syntax errors in your code, you had an unpaired curly brace:

[php]<?php
if (isset($_POST[‘submit’])){
include ‘db.php’;//Connect to database
$title=$_POST[‘title’] ;
$author= $_POST[‘author’] ;
$name=$_POST[‘name’] ;
$copy=$_POST[‘copy’] ;
$num1=$_POST[‘numb1’];
$num2=$_POST[‘numb2’];
$title_search = mysql_query(“SELECT Title FROM books WHERE Title=’$title’ AND Author=’$author’”);
$tot_num = $_POST[‘numb1’] + $_POST[‘numb2’] * 100;
Echo $tot_num;

if(mysql_num_rows($title_search) != 0) {
echo 'Record Already Exist!';
} else {
	 mysql_query("INSERT INTO `books`(Title, Author, PublisherName, CopyrightYear) VALUES ('$title',  '$author', '$name', '$copy')"); 
}
mysql_close($conn);

} // <-------------------------------------This one was missing
?>[/php]

Also you had: Echo $tot_num;, echo needs to be lower case.

What are you using to write the code? If you’re using Notepad or some other plain text editor you might want to try something like Notepad++ as this has syntax highlighting and also highlights pairs of curly braces and brackets. There are some reviews of and links to some free programming editors here: http://www.techsupportalert.com/best-free-programming-editor.htm

Hello xerxes,

Thanks for a very nice tip (notepad++) nice color just like on your forum site.

I did not know that you can put the PHP Code on the top of an HTML. As you know code before were top down to bottom reading. I got so much things to learn.

Once again thank you.

There’s nothing wrong with having it on the bottom, it has more to do with how it operates than organization. if you want the data to change on the screen when you hit submit, it needs to be on top. Plus, is a lot easier to troubleshoot when you’re not having to skip all over the place looking for code.

Thanks richei for the enlightenment.

Sponsor our Newsletter | Privacy Policy | Terms of Service