Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts

    html form clear fields not working correctly

    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:
    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 the database but it still didn't work.
    Here is my query- I have pagination applied:
    Any suggestions would be greatly appreciated, thanks

    Code:
     <?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
    }
    ?>
    Last edited by daniel0816; 10-23-2013 at 03:38 PM.

  • #2
    wlf
    wlf is offline
    New Coder
    Join Date
    Aug 2012
    Posts
    61
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Let me try to help,

    You open the page, a query is executed and the results are shown in form inputs.

    You click a reset button with some JS behind that is supposed to clear one form input but it doesn't work.

    Is that it?
    A good programmer is someone who looks both ways before crossing a one-way street. Free hosting

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    PHP Code:
    if(isset($criteria)){
        if(
    $criteria==''){  // YOU ONLY GET HERE IF $criteria IS SET SO THIS NEVER EXECUTES 
    And your lucky it doesn't because it's an infinite loop. Remove it from your code.

    PHP Code:
    if(!mysql_num_rows($result)){
        
    $message "No matching record";
        echo 
    "<script language=\"javascript\" type=\"text/javascript\">
        alert('{$message}');
        location.href = '';
        </script>"
    ;
        return 
    false;

    Is also an infinite loop.

    I see nothing in the php that would cause the problem.
    But
    Code:
    function onFormSubmit()
    {
    	document.getElementById('cname').innerHTML='';
    }
    Might clear the field. A 'cname' sounds more like html than a value.
    Last edited by sunfighter; 10-23-2013 at 04:19 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by sunfighter View Post
    PHP Code:
    if(isset($criteria)){
        if(
    $criteria==''){  // YOU ONLY GET HERE IF $criteria IS SET SO THIS NEVER EXECUTES 
    And your lucky it doesn't because it's an infinite loop. Remove it from your code.
    This is incorrect. "" isn't the same as isset; isset is classified as:
    Determine if a variable is set and is not NULL.
    PHP Code:
    $var "";
    print isset(
    $var); // 1 
    So checking empty criteria is valid. I myself would use the empty() function instead of "", but you can certainly differentiate between isset and empty.

    Am I blinding something as well? I don't see a condition in PHP that ends up in an infinite loop.

    As for "This tells me that my query is still running so I tried to close the connection to the database but it still didn't work.", this is likely a misunderstanding of the mechanics of http communications. If your query is still running, all you will see is an awaiting response until either it completes, or the request times out. In a browser, that'd denoted by the loading bar. If your query is running, than the PHP code is not yet complete, and if the PHP code isn't complete than you haven't received a response yet. As a server side language, it cannot be used asynchronously with the client.

    This query is unusual as well. Its closer to what I'd think it should be, but it still doesn't make very much sense. You are merging two recordsets together, but the logic is that you will still always have both sets of properties available except that one side would be empty. That is not really the intention of the union query. Instead, you treat it as if its either/or, but you don't know what to do with it. The problem you have here is there are not quite exact fields to work with, but you can fudge all except the fore/surnames which can be concatenated:
    Code:
    SELECT
    	CUST_ID AS id,
    	CONCAT_WS(' ', CUST_Forename, CUST_Surname) AS name, 
    	CUST_Email AS email,
    	CUST_HomeNum AS primenum,
    	CUST_Mobile AS secnum,
    	CUST_AddressL1 AS addrl1,
    	CUST_AddressL2 AS addrl2,
    	CUST_AddressL3 AS addrl3,
    	CUST_Postcode AS postcode, 
    	'CUSTOMER' AS type
    FROM Customers
    WHERE
    	(CUST_Forename LIKE '%$criteria%') OR (CUST_Surname LIKE '%$criteria%')
    	OR (CUST_Postcode LIKE '%$criteria%')
    UNION
    SELECT 
    	COMP_ID,
    	COMP_Name,
    	COMP_Email,
    	COMP_PrimaryNum,
    	COMP_SecondaryNum,
    	COMP_AddressL1,
    	COMP_AddressL2,
    	COMP_AddressL3,
    	COMP_Postcode,
    	'COMPANY' AS type
    FROM Company
    WHERE
    	(COMP_Name LIKE '%$criteria%')
    LIMIT $offset, $rowsperpage
    Which when fetched will give records for: id, name, email, primenum, secnum, addrl1, addrl2, addrl3, postcode, and type. This is more what you'd do in a union so as to say "one of you have what I need, but I don't know which one".

    Don't forget that the first thing you will always do is shut off your javascript. You must verify that your problem doesn't lie with a client issue prior to a server issue. Always program as if client side is limited to minimum understanding of response. Javascript is fun and can help limit server requests, but it is unreliable as any direct means of control.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    Fou-Lu, I'll never remember that isset is different from ''. This has happened to me before and you see I haven't learned my lesson yet.

    When I ran the if($criteria==''){...} section before and again now with $criteria='' as input I got/get an alert box that will not go away.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    = isn't the same as ==.
    PHP Code:
    if ($var false)
    {
        
    // this will always execute

    It is *very* rare for an assignment to fail, so much so that I'd presume something went wrong with the hardware memory. So assignments are always true.

    Although that's still not PHP looping behaviour, that sounds to be an issue with the JS. Given what I see here, if you submit nothing, it will send you back to this page. It shouldn't continue though, the location I'd assume should wipe out the querystring, but perhaps I'm mistaken on the reading of the location.href? I'm not a JS writer so I can't be sure.

    Edit:
    Lol, I read too much into what you have as literal. Its not that you are changing the code to =, its that you are giving it the value of ''.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Yea 'wlf' that is correct, well it does work you can see it on screen being removed from the textbox but it gets put back in again automatically. But when I manually key in a new record it works fine

  • #8
    Regular Coder
    Join Date
    Aug 2013
    Posts
    141
    Thanks
    22
    Thanked 0 Times in 0 Posts
    got it thanks guys appreciate the input


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •