html form clear field not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

html form clear field not working

Post by sectionLeader123 »

I have a reset button which when clicked it clears one of the fields in the form. Here is javascript code to clear the field:

Code: Select all

function onFormSubmit()
{
	document.getElementById('cname').value=' ';
}
This seems to work but that particular field is one of many that holds results from a query and when I execute the query and the result is displayed on the form. I then click the reset button and the field gets cleared but the result for the field gets put back in automatically. This tells me that my query is still running so I tried to close the connection to database but it still didn't work.
Here is my query- I have pagination applied:
Any suggestions would be greatly appreciated, thanks

Code: Select all

<?php
session_start();
session_destroy();
$criteria = $_GET['sCriteria'];
if(isset($criteria))
{
	if($criteria=='')
	{
		$message = "Please Enter a Criteria";
		echo "<script language=\"javascript\" type=\"text/javascript\">
		alert('{$message}');
		location.href = ' ';
		</script>";
		return false;
	}
	include 'connect.php';
	$sql = "SELECT COUNT(*) FROM Customers
	WHERE
	(CUST_Forename LIKE '%$criteria%') OR (CUST_Surname LIKE '%$criteria%')
	OR (CUST_Postcode LIKE '%$criteria%')
	UNION
	SELECT COUNT(*) FROM Company
	WHERE (COMP_Name LIKE '%$criteria%')
	";
	$result=mysql_query($sql) or die(mysql_error());
	$result2=mysql_fetch_row($result);
	$numrows = $result2[0];
	$rowsperpage = 1;
	$totalpages = ceil($numrows / $rowsperpage);
	if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
	   // cast var as int
	   $currentpage = (int) $_GET['currentpage'];
	} else {
	   // default page num
	   $currentpage = 1;
	} // end if

	if ($currentpage > $totalpages) {
	   // set current page to last page
	   $currentpage = $totalpages;
	} // end if
	// if current page is less than first page...
	if ($currentpage < 1) {
	   // set current page to first page
	   $currentpage = 1;
	} // end if

	$offset = ($currentpage - 1) * $rowsperpage;

	$sql="	SELECT CUST_ID, CUST_Forename, CUST_Surname, 
	CUST_Email, CUST_Mobile, CUST_HomeNum, 
	CUST_AddressL1, CUST_AddressL2, CUST_AddressL3,
	CUST_Postcode, '' as COMP_ID, '' as COMP_Name, '' as COMP_Email, '' as COMP_PrimaryNum,
	'' as COMP_SecondaryNum, '' as COMP_AddressL1, '' as COMP_AddressL2, '' as COMP_AddressL3,
	'' as COMP_Postcode
	FROM Customers
	WHERE
	(CUST_Forename LIKE '%$criteria%') OR (CUST_Surname LIKE '%$criteria%')
	OR (CUST_Postcode LIKE '%$criteria%')
	UNION
	SELECT '' as CUST_ID, '' as CUST_Forename, '' as CUST_Surname, '' as CUST_Email,
	'' as CUST_Mobile, '' as CUST_HomeNum, '' as CUST_AddressL1, '' as CUST_AddressL2, '' as CUST_AddressL3,
	'' as CUST_Postcode,
	COMP_ID, COMP_Name, COMP_Email, COMP_PrimaryNum,
	COMP_SecondaryNum, COMP_AddressL1, COMP_AddressL2, COMP_AddressL3,
	COMP_Postcode
	FROM Company
	WHERE (COMP_Name LIKE '%$criteria%')
	LIMIT $offset, $rowsperpage
	";

	$result=mysql_query($sql) or die(mysql_error());
	if(!mysql_num_rows($result))
	{
		$message = "No matching record";
		echo "<script language=\"javascript\" type=\"text/javascript\">
		alert('{$message}');
		location.href = ' ';
		</script>";
		return false;
	}
	while($result2=mysql_fetch_assoc($result))
	{
		$CID = $result2['CUST_ID'];
		$cuEmail = $result2['CUST_Email'];
		$forename = $result2['CUST_Forename'];
		$surname = $result2['CUST_Surname'];
		$mobile = $result2['CUST_Mobile'];
		$homenum = $result2['CUST_HomeNum'];
		$add1 = $result2['CUST_AddressL1'];
		$add2 = $result2['CUST_AddressL2'];
		$add3 = $result2['CUST_AddressL3'];
		$pCode = $result2['CUST_Postcode'];
		$compID = $result2['COMP_ID'];
		$compName = $result2['COMP_Name'];
		$compEmail = $result2['COMP_Email'];
		$pNumber = $result2['COMP_PrimaryNum'];
		$sNumber = $result2['COMP_SecondaryNum'];
		$compAdd1 = $result2['COMP_AddressL1'];
		$compAdd2 = $result2['COMP_AddressL2'];
		$compAdd3 = $result2['COMP_AddressL3'];
		$compPostcode = $result2['COMP_Postcode'];
	}//end while
	$range = 3;

	// if not on page 1, don't show back links
	if ($currentpage > 1) {
	   // show First link to go back to page 1
		 echo "<a href=\"{$_SERVER['PHP_SELF']}?currentpage={1}&sCriteria={$criteria}\">First </a>";
	   // get previous page num
	   $prevpage = $currentpage - 1;
	   // show Previous link to go back to 1 page
	  echo "<a href=\"{$_SERVER['PHP_SELF']}?currentpage={$prevpage}&sCriteria={$criteria}\">Previous</a>";
	} // end if

	 // loop to show links to range of pages around current page
	for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
	   // if it's a valid page number...
	   if (($x > 0) && ($x <= $totalpages)) {
		  // if we're on current page...
		  if ($x == $currentpage) {
			 // 'highlight' it but don't make a link
			 echo " [<b>$x</b>] ";
		  // if not current page...
		  } else {
			 // make it a link
			  echo "<a href=\"{$_SERVER['PHP_SELF']}?currentpage={$x}&sCriteria={$criteria}\">$x</a>";
		  } // end else
	   } // end if 
	} // end for
					 
	// if not on last page, show forward and last page links        
	if ($currentpage != $totalpages) {
	   // get next page
	   $nextpage = $currentpage + 1;
		// echo forward link for next page 
	  echo "<a href=\"{$_SERVER['PHP_SELF']}?currentpage={$nextpage}&sCriteria={$criteria}\">Next </a>";
	   // echo forward link for lastpage
		 echo "<a href=\"{$_SERVER['PHP_SELF']}?currentpage={$totalpages}&sCriteria={$criteria}\">Last</a>";
	} // end if
}
?>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: html form clear field not working

Post by twinedev »

1. Your main question is about javascript, which works on client site (ie after server side sends out code from PHP), so porting to Javascript section may help more.

2. It would really help to have the relavant code, you don't have the code that shows how the form is built and all the other javascript that will be with it.

3. As for the PHP code, it is wide open for being hacked, both XSS and SQL injection.
Post Reply