0

I know it's kind oldy to use ASP (active server pages) in 2017 but my customer site is built in asp and he does not want to change it.

He asked me to do a form that you can see here http://www.preventys.net/shopquizz.asp

The form is working great.

My problem is with the postback function.

When postbacked, i need the window to go to the field checked and not return to the top of the page. You can for example go to the last record in the form and check yes or no, it will reload as postback, but you will be sent to the top of the page.

here after the complete code

<%

Dim header : header = getlang("FDC002")
setsess "currenturl" , "shopquizz.asp"
'
'dim customerid : CustCheckAdmin customerid
'
dim strsubject
dim customeradmin, custid
Dim sAction, dbtable
dim strcomment
dim qID
dim sx
'
dim qc,qrev
'
Dim StrQQ_(17)
Dim StrQR_(17)
Dim StrQL_(17)
'
Dim StrQREV_(17)
Dim StrQREV_VAL_(17)
'
Dim StrQuestionLabel(17)
Dim StrQuestionHelp(17)
Dim StrQtc(17), StrQtc_Total
'
Dim Serror_q(17)
Dim Serror_rev(17)

Dim StrQuizzResult, StrRevQuizzResult, StrTotalPoints
'

sAction = Request("Action")
if saction = "" then 
    sAction = Request("Action.x")
end if
'
Serror = ""
'
' AFFICHAGE DU SITE
ShopPageHeader "ContentPageContact"
'
putMyTitle "Répondez à notre questionnaire pour savoir si votre entreprise est conforme aux lois en vigueur"
'
if sAction = "" then
    DisplayForm
else
    ValidateData()
    if sError = "" then
        Add_Quizz_to_Database
        WriteInfo "<h3>Merci d'avoir répondu au questionnaire.</h3><h3>Vous recevrez dans quelques minutes un diagnostique de conformité de votre établissement.</h3>" , ""
        SendMailTo_Merchant_Customer qid
    else
        shopwriteError SError
        %><br /><%
        DisplayForm
    end if
end if
ShopPageTrailer
'
'*************************************************
'Display Form
'*************************************************
Sub DisplayForm()
        %>

        <form id="Form_ID" name="Form_ID" method="Post" action="<%=getsess("currenturl")%>">
            <table border="1" cellpadding="5" cellspacing="2" class="TableContact">     
                <%
                dim qConn, qDBC, qSql, qRs
                dim counter : counter = 1
                shopopendatabase qdbc
                qSQl="Select * from questionnaire order by qid ASC" 
                Set qrs = qdbc.execute(qsql)
                if not qrs.eof then
                    do while not qrs.eof
                        StrQuestionLabel(counter) = qrs("question_label")
                        StrQuestionHelp(counter) = qrs("question_help")

                        ' questions principales
                    %>
                    <tr>
                        <td align="cennter" valign="middle"><h3><%=qrs("qid")%></h3></td>
                        <td width="15" align="cennter" valign="middle" style="cursor:pointer;" onClick="__TOGGLESHOWHIDE('explain_<%=qrs("qid")%>');"><img title="Cliquez ici pour en savoir plus" src="icons/icon_orders_info.png" width="15" height="15" align="absmiddle" alt="" border="0"></td>
                        <td width="100%">
                            <%= Serror_q(counter)%>
                            <h3><%=StrQuestionLabel(counter)%></h3>
                            <input type="hidden" name="QL_<%=qrs("qid")%>" value="<%=StrQuestionLabel(counter)%>">
                        </td>
                        <td nowrap><input name="q_<%=qrs("qid")%>" type="radio" value="100"     <% if request("q_"& qrs("qid")) = "100" then%> checked <%end if%> onchange="__DOPOSTBACK();">Oui</td>
                        <td nowrap><input name="q_<%=qrs("qid")%>" type="radio" value="-100"    <% if request("q_"& qrs("qid")) = "-100" then%> checked <%end if%> onchange="__DOPOSTBACK();">Non</td>
                        <% 

                        ' personne concernés
                        Select case qrs("qid")
                            case "1","2","3","4","13","15","17" %>
                            <td>Tous concernés <input type="hidden" name="qtc_<%=qrs("qid")%>" value="1"></td>
                            <% case else %>
                            <td nowrap>
                                <select class="selectmultiple" name="qtc_<%=qrs("qid")%>"  onchange="__DOPOSTBACK();">
                                    <option value=''>Concerné ?</option>
                                    <option value="1" <% if request("qtc_"& qrs("qid")) = "1" then response.write "selected"%> >Oui</option>
                                    <option value="0" <% if request("qtc_"& qrs("qid")) = "0" then  response.write "selected"%> >Non</option>
                                </select>
                            </td>
                        <% end select %>
                    </tr>

                    <% 
                        'revision annuelle
                        Select case qrs("qid")
                        case "1","16","15"

                        case else

                        if request("q_"& qrs("qid")) = "100" then %>
                        <tr>
                            <td colspan="2">&nbsp;</td>
                            <td bgcolor="#eaeaea" colspan="4"><%= Serror_rev(counter)%>
                                <font size="2" color=""><b>Revision annuel à jour?</b></font>
                                    <input type="radio" name="qrev_<%=qrs("qid")%>" value="100" <% if request("qrev_"& qrs("qid")) = "100" then%> checked <%end if%> onchange="__DOPOSTBACK();">Oui
                                    &nbsp;
                                    <input type="radio" name="qrev_<%=qrs("qid")%>" value="-100" <% if request("qrev_"& qrs("qid")) = "-100" then%> checked <%end if%> onchange="__DOPOSTBACK();">Non
                                <% end if %>
                            </td>
                        </tr>
                        <% end select %>





                    <tr><td colspan="6"><div id="explain_<%=qrs("qid")%>" style="display:none;"><%=  StrQuestionHelp(counter) %></div></td></tr>

                    <%
                        counter = counter + 1
                    qrs.movenext
                    loop
                end if
                qrs.close
                set qrs=nothing
                shopclosedatabase qdbc
                %>
                </table>

                <table>
                <tr>
                    <td class="ContentContact"><h3>Veuillez recopier le code de sécurité qui s'affiche dans la case ci-contre</h3></td>
                    <td class="ContentContact"><% CreateSecurityImage "securitynumber" %></td>              
                </tr>
            </table>
            <div align="center"><br /><input class="Custsubmitbtn" type="submit" name="action" value="<%=getlang("FDC013")%>"></div>
        </form>
    <br />

    <script language="javascript">    
        var $ = function (_Id){return document.getElementById(_Id);}
        var theForm = document.forms['Form_ID'];    
        if (!theForm) {theForm = document.Form_ID;}

        var __DOPOSTBACK = function(){for (var i=0; i<(arguments.length); i=i+2){$(arguments[i]).value=arguments[i+1];}theForm.submit();}
        var __TOGGLESHOWHIDE = function(_obj){$(_obj).style.display=='none'?$(_obj).style.display='block':$(_obj).style.display='none';}
        var __HIDDENLISTOJECT = function(){
            for (var i=1; i<(arguments.length); i=i+2){$(arguments[i]).checked='';$(arguments[i+1]).style.display='none';}
            for (var i=1; i<(arguments.length); i=i+2){$(arguments[i]).checked='';$(arguments[i]).disabled='disabled';}

            if($(arguments[0]).style.display!='none')
            {
                $(arguments[1]).disabled='';
            }
            }
    </script>
    <%
End Sub
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
humer2000
  • 7
  • 3

2 Answers2

1

You can do a Achor Location Scroll, http://www.the-art-of-web.com/javascript/remove-anchor-links/#another-heading.

Put and Id for the radio buttons and pass event as parameter for onChange listener:

<td nowrap><input name="q_<%=qrs("qid")%>" id="id_<%=qrs("qid")%>" type="radio" value="100"     <% if request("q_"& qrs("qid")) = "100" then%> checked <%end if%> onchange="__DOPOSTBACK(event);">Oui</td>
<td nowrap><input name="q_<%=qrs("qid")%>" id="id_<%=qrs("qid")%>" type="radio" value="-100"    <% if request("q_"& qrs("qid")) = "-100" then%> checked <%end if%> onchange="__DOPOSTBACK(event);">Non</td>

so, in onChange handler you change location.href

var __DOPOSTBACK = 
  function(event){
    var formAction = "preventys.net/shopquizz.asp" + '#' + event.target.id;
    theForm.action = formAction;
    theForm.submit();
}

I think this could work for you.

  • Please can you precise how to add window.location.href = '#' + event.target.id in my function, i tried several combinations, i have js error. (shopquizz.asp:1273 Uncaught TypeError: Cannot set property 'value' of null at __DOPOSTBACK (shopquizz.asp:1273)) – humer2000 Jul 10 '17 at 18:05
  • Just like a wrote above. – Henrique Oecksler Bertoldi Jul 10 '17 at 18:07
  • i put this var __DOPOSTBACK = function(event){ window.location.href = '#' + event.target.id ; for (var i=0; i<(arguments.length); i=i+2){$(arguments[i]).value=arguments[i+1];} theForm.submit();} but i have a js error – humer2000 Jul 10 '17 at 18:08
  • try this: CHANGE window.location.href = '#' + event.target.id TO var formAction = theForm.action + '#' + event.target.id; theForm.action = formAction; – Henrique Oecksler Bertoldi Jul 10 '17 at 18:37
  • nope, still the same error. can you try to write the comple line of javascript this way i dont misstype it. – humer2000 Jul 10 '17 at 18:50
  • did you do that onchange="__DOPOSTBACK(event);"? – Henrique Oecksler Bertoldi Jul 10 '17 at 18:57
  • try to remove the for (for (var i=0; i<(arguments.length); i=i+2){$(arguments[i]).value=arguments[i+1];}) – Henrique Oecksler Bertoldi Jul 10 '17 at 18:59
  • yes sure. var __DOPOSTBACK = function(event){for (var i=0; i<(arguments.length); i=i+2){$(arguments[i]).value=arguments[i+1];}var formAction = theForm.action + '#' + event.target.id; theForm.action = formAction; theForm.submit();} – humer2000 Jul 10 '17 at 18:59
  • ok it seems to be working when i removed the var i=0 .... but when the page is reloaded the url is not correct . it goes to http://www.preventys.net/[object%20HTMLInputElement]#id_17, as u can see the id is correct but it replaces shopquizz.asp with object%20HTMLInputElement] – humer2000 Jul 10 '17 at 19:05
  • so change var formAction = theForm.action + '#' + event.target.id; to var formAction = theForm.baseURI + '#' + event.target.id; – Henrique Oecksler Bertoldi Jul 10 '17 at 19:06
  • it looks better, just a final touch. it's cumulating ids http://www.preventys.net/shopquizz.asp#id_17#id_17#id_14#id_10. how to clean id after postback – humer2000 Jul 10 '17 at 19:13
  • This is just a simple logic problem to solve. You can put "<%=getsess("currenturl")%>" (get value from asp) instead of theForm.action – Henrique Oecksler Bertoldi Jul 10 '17 at 19:19
  • Good. So Mark the answer as resolved. And all this "workaround" does't see to much a good code practice. But, works. – Henrique Oecksler Bertoldi Jul 10 '17 at 19:48
-1

You could use a classic ASP server-side function as found below:

https://stackoverflow.com/a/58054341/8624010

tb-mtg
  • 529
  • 8
  • 10
  • Link only answers are discouraged, particularly when the linked answer does not answer this question. – Jon P Sep 25 '19 at 00:52