0

Allende wrote this for me in answer to an earlier question and I'm trying to figure out how I add an extra piece of code, so that if the user enters a number higher than the total number of records, it throws up an error. I know I can use SQL RecordCount to find the number of records, but I have no idea how to integrate it into Allende's script. As with the script, the error doesn't need to show until the form is submitted.

$(document).ready(function(){           
 $('form').on("submit",function(){
         var tempArray=[];            
         var exists=0;
         $("input[type='text'][name^='PositionNumber']").each(function(){
             exists = tempArray.indexOf($(this).val());             
             if (exists>=0){                    
                 return false;//break the loop
             }
             tempArray.push($(this).val());                  
         });

         //after you can use "exist" to check if duplicated and retrieve the value to cancel the submit
         if (exists>=0){                   
             alert("You have used the number " + tempArray[exists] +" more than once.\r\nPlease correct the error and resubmit.");                
         } else{
             //alert("no duplicated value:");
             return true;
         }

        return false;            
 }); 
});

Thanks in advance for the help and advice.

Regards

Pb

Community
  • 1
  • 1
  • If you mean the recordcount property of an ADO Recordset then the code would be executed server side. Your jquery stuff wouldn't see the ASP code, it would just see the number it outputs. Could you indicate in your code where you want this number to go? – John Mar 15 '14 at 11:47
  • Hi John. Thanks for the reply. The problem is, I'm not sure where the code would go. I'm not even sure how to get the RecordCount score in to the code. Could I put the RecordCount total in to a hidden field within the form and use that as, say, `var rc=("input[type='hidden'][name^='countTotal']")`, then call it in the error section somehow? I'm not sure how that would be done right now either. –  Mar 15 '14 at 12:42
  • I can help you with the ASP side, but not with the jquery I'm afraid. One of the reasons I started using ASP - about 15 years ago - was to avoid having to use Javascript :) – John Mar 15 '14 at 13:45
  • Could [Allende's](http://stackoverflow.com/users/462889/allende) script have been written in classic ASP then? –  Mar 15 '14 at 13:52
  • If Allende's script is what you posted in your question it is definitely jquery. Jquery scripts almost always begin with `$(document).ready`. This page shows you how to use the ado recordcount property. http://www.w3schools.com/ado/prop_rs_recordcount.asp . If you place the code in their example at the very beginning of the page, before the opening `` tag, then you can add `<%=i%>` anywhere in your page and it will be displayed client side as the number of rows. – John Mar 15 '14 at 18:27
  • I can sort the ASP bit, it's the JQuery I know nothing about. –  Mar 15 '14 at 22:43
  • Could you post a link to the earlier question you mention - it might make sense of what that script is supposed to be doing? – John Mar 16 '14 at 00:35
  • Here's the link. http://stackoverflow.com/questions/21768606/generate-an-error-if-number-is-repeated-in-a-form –  Mar 16 '14 at 10:40
  • That link makes things much clearer, here's my answer – John Mar 16 '14 at 12:57

2 Answers2

0

Yes, one option would be to have another hidden field and modify your jquery, but a more elegant solution would be to make it impossible to enter an invalid value in the first place. Instead of having a text field to input the position you could have a select which only goes up to the number of records.

The ASP would look like this

<select name="positionnumber">
<% 
For i = 1 To rsmenulist.recordcount 
  response.write "<option>" & i & "</option>"
Next
%>
</select>

clent side this would look like

<select>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>

(assuming of course there were 4 records.

If you wanted the current value to show as selected when the page loads you could modify it as follows.

<select name="positionnumber">
<%     
For i = 1 To rsmenulist.recordcount 
if i = cint(rsmenulist("recordposition")) then 
 selected = " selected" 
else
 selected = ""
end if
  response.write "<option" & selected & ">" & i & "</option>"
Next
%>
</select>

You've clearly been using Dreamweaver to write your asp code. At the end of the day it works, which is the main thing, but DW produces very bloated code, and when you need to do something for which DW doesn't have a wizard working out where to put your own modifications can be a nightmare If you have time (which I realise can be a problem) then it's worth finding a good tutorial, you'll discover how to do in 2 lines what DW does in 20+.

One thing which DW typically does is to close one code block (with "%>") and then open another one on the following line "<%" with no client side code in between. If you want to be nice to your webserver then it's worth merging those into a single code block. I'm particularly talking about the section at the end of the page where you close all your recordsets

John
  • 4,658
  • 2
  • 14
  • 23
  • Hi John. Thanks for the code. In fact, I know my agent's preference is a select box but the client has no opinion either way, so he'll be a happy bunny. Just as an aside, I know about the bloat of DW and I'll be taking up your advice on the tutorial. Again, thanks for the help. –  Mar 16 '14 at 13:52
  • Hi again John. The ` –  Mar 16 '14 at 15:19
  • Is the value of your recordcount showing as - 1. (stick `<%=rsmenulist.recordcount %>` somewhere in your page to find out. If it is -1 then see this page. http://classicasp.aspfaq.com/general/why-does-recordcount-return-as-1.html – John Mar 16 '14 at 15:39
  • Yep, getting a -1. I'll check out the link. –  Mar 16 '14 at 15:45
  • So we'll need to figure out in your DW code how to change the cursor type from 0 to 1. NB I've just tweaked my answer. The reason being that the value of the string 'selected' needs to go back to "" after the selected option – John Mar 16 '14 at 15:56
  • Cheers John. I'm going to show the current record position separately so I'm using your first piece of code. Just trying a couple of ideas put forward in the code from the link. –  Mar 16 '14 at 16:09
  • I've added the page code above as it's more lines that can be added here. –  Mar 16 '14 at 16:14
  • I've hit a problem. You're using MySQL, and presumably the MyODBC driver. Changing the cursor type has no effect, It only seems to work with Access or SQL Server. This means you'll probably need another way of counting your rows – John Mar 16 '14 at 16:33
  • I know that `SELECT COUNT(*) FROM tblContent` shows the total number of records available in table but I'm not sure if there is a way to use that info. I've created a recordset using `SELECT COUNT(*) AS totalRecords` and I've tried using it in different ways but no joy. –  Mar 16 '14 at 17:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49839/discussion-between-john-and-martysmartypants) – John Mar 16 '14 at 17:07
0

Here's the SQL code that gets the records for the form:

SELECT tblContent.*, tblMainMenu.MainMenuName, tblSubMenu.SubMenuName, tblSubMenu.SubMenuID
FROM (tblContent LEFT JOIN tblMainMenu ON tblContent.MainMenuID = tblMainMenu.MainMenuID) LEFT JOIN tblSubMenu ON tblContent.SubMenuID = tblSubMenu.SubMenuID
WHERE tblContent.SubMenuID = smID AND tblContent.DisplayRecord =1
ORDER BY tblContent.PositionNumber

Here's the code that displays it on the page (with John's code added):

<% IF Request.ServerVariables("QUERY_STRING") <> "" THEN %>
<h3><span style="font-size:small">Order/Re-order records for: </span><%=(rsContent.Fields.Item("SubMenuName").Value)%></h3>
<form action="record-order-modify.asp" method="post" class="recordPosition">
<%=rsRecordCount.RecordCount %>
<% 
While ((rptContent__numRows <> 0) AND (NOT rsContent.EOF)) 
%>
  <table width="100%" id="records">
    <tr>
      <td align="left" valign="top" name="ContentTitle" colspan="2"><h2><%=(rsContent.Fields.Item("ContentTitle").Value)%></h2><input type="hidden" value="<%=(rsContent.Fields.Item("ContentID").Value)%>" name="ContentID<%=counter%>">
      <%=(rsContent.Fields.Item("ContentData").Value)%>...
      </td>
      </tr>
      <tr>
      <td width="240"><label>Current Record Position:</label>&nbsp;&nbsp;<input type="text" class="recordPosition" value="<%=(rsContent.Fields.Item("PositionNumber").Value)%>"></td>
      <td align="right"><label>New Position: <small class="brown" style="text-transform:none">(You may change it here)</small></label>&nbsp;&nbsp;
        <select name="PositionNumber" class="recordPosition">
        <% 
        For i = 1 To rsContent.RecordCount 
        Response.Write "<option>" & i & "</option>"
        Next
        %>
        </select>
      </td>
     </tr>
  </table>
  <hr>
  <% 
  rptContent__index=rptContent__index+1
  rptContent__numRows=rptContent__numRows-1
  rsContent.MoveNext()
Wend
%>
<table align="center" class="positionButtons">
<tr>
<td width="50%" align="right"><input name="Submit" type="submit" value="Update Positions"></td>
<td width="50%" align="left"><input name="Reset" type="reset" value="Reset All Changes" tabindex="<%=counter%>"></td>
</tr>
</table>
<input type="hidden" name="action" value="update">
<input type="hidden" name="counter" value="<%=counter%>">
</form>
<% ELSE %>
<h3>Select a listing to order/re-order using the list on the left.</h3>
<% END IF %>
  • As per my earlier comment I don't think you can use recordcount because the MyODBC 5.1 driver only seems to support the forward only cursor type. One possible workaround is to use a subquery in your sql so that you can have the total number of rows in each row in your recordset. Here's an example - be careful with brackets! `select (select count(id) from tablename as virtual) as totalrows, tablename.* from tablename` – John Mar 16 '14 at 16:47
  • @MartySmartyPants Is this an answer or an extension of your question? If it's the latter you should edit the original question not add a new answer. – user692942 Mar 18 '14 at 00:45
  • It wasn't a direct answer to the question but an answer to the problem (if that makes sense) which is why I chose it. I'm new to StackOverFlow so I'm not completely 100% with the rules and requirements. –  Mar 18 '14 at 09:53