0

I'm developping a CRUD with AMPScript.

I don't know how to display a message in PopUp like alert("This is a primary Key, it must be unique")

    <!doctype html>
    <html>   
    <head>
    <!--
    %%[
    Set @FormSubmit = RequestParameter("formsubmit")
    If @FormSubmit == "true" Then
     Set @CodePrimary = RequestParameter("PrimaryCode")
     Set @CodeSecondary = RequestParameter("SecondaryCode")

If RowCount(LookupRows"DataExtentionTest","Primary Code",@CodePrimary))>0 Then 

Set  @FormSubmit = "false" 

]%%
    <script >
        alert("The PrimaryCode %%=v(@CodePrimary)=%% already exists in DataExtension !");
    </script>

%%[

Else       
    Set @Insert = InsertData("DataExtentionTest","Primary Code",@ CodePrimary," Secondary Code ",@ CodeSecondary)
    EndIf
    ]%%
    -->
    <script >
    function validate() {
     if ( %%[RowCount(LookupRows"DataExtentionTest","Primary Code",@ CodePrimary))>0 ]%% ){
            alert("This is a primary Key, it must be unique ");
            return false;
        }   }
    </script>
    </head>
    <!--%%[If @FormSubmit == "true" AND RowCount(LookupRows"DataExtentionTest","Primary Code",@ CodePrimary))== 0 Then]%%-->
    <body onload="closebrowser();window.opener.location.reload(false);">
    <!--%%[Else]%%-->
    <body>
    <!--%%[EndIf]%%-->
    <form name="form1" method="post" action="" onsubmit="return validate();">
    <table>
    <tr>
    <td>  Primary Code : </td>
    <td> 
    <input type="hidden" name="formsubmit" value="true"/>
    <input name=" PrimaryCode " type="text" id=" PrimaryCode" />
    </td>
    </tr>
    <tr>
    <td>  Secondary Code : </td>
    <td> 
    <input name=" SecondaryCode " type="text" id=" SecondaryCode " />
    </td>
    </tr>
    <tr>
            <td align="center">
                <input type="submit" value="Submit"/>
            </td>
            <td align="center">
                <input type="button" value="Annuler" onClick=" return closebrowser()"/>
            </td>
        </tr>
        </table>
    </form>
    </body>
    </html>

Need some help please !!

Regards Yazid

3
  • What's the current output of the page? Feb 24, 2016 at 21:53
  • You have a missing parentheses in the rowcount line. Feb 24, 2016 at 21:54
  • Hello Adam, Thank you for your response. After submited my Form, if the CodePrimary already exists en DataExtension, it display an Error message : Violation of PRIMARY KEY constraint 'PK_15AACD1EEA484E98B48B74EBE68F453B'. Cannot insert duplicate key in object 'C7211327.DataExtentionTest'. The duplicate key value is (055). The statement has been terminated.
    – y.akki
    Feb 25, 2016 at 18:19

2 Answers 2

1

Not sure about alert but best option is use Output/OutputLine option to debug your code. e:g

    %%[ Var @text
    Set @text = "Example Text"
    Output(v(@text)) ]%%
1
  • I tried both of option, but unfortunatly, it's stii doesn't work. I need to display the Message in PoP Up something like (Alert("Code already exists")). I think it's possible to put an alert message JS inside an IF Statement AMPSCRIPT.
    – y.akki
    Feb 25, 2016 at 18:22
0

validate() gets called when you submit the form, it never returns false because the AMPScript in the if statement is evaluated when the page loads since it is server-side (not after post back). After the form posts, it will evaluate that AMPScript so it could potentially be false but there is nothing to call validate again after post back.

4
  • Hello Mac, You're right, So, do you have any solution to adapt my code ?
    – y.akki
    Feb 29, 2016 at 8:59
  • Within the block where @FormSubmit == "true", put code so it prints out the entire client-side script block that displays the alert: v('<script>alert("This is a primary Key, it must be unique ");'</script>)
    – Mac
    Feb 29, 2016 at 12:33
  • Actually, i display an alert, but the Form still submited, i lose all setted records ( Primary Code and Secondary code ).
    – y.akki
    Mar 2, 2016 at 13:23
  • i put the new Code !!
    – y.akki
    Mar 2, 2016 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .