2

I'm having some problems with some javascript / visualforce integration.

What i'd like to do is when the user clicks a button do some javscript error checking calling and if it fails, display a pop up and stay on the page without refreshing (i.e. i don't want to lose the client side stuff). I have the error checking working,and i prevent the page from submiting. i'm not sure if i did it right- i'm adding an action to the command button and adding an onSubmit method to the form.

If it passes- generate a string from that check method and pass it to the apex controller. I'm stuck on this too.

<apex:page extensions="customizationExtension" standardController="Customization__c" standardcontroller="Customization__c"><apex:messages />
    <script>

    function check(){
     var flag = false;
    var successCodes = "start";
    // do a bunch of error checking.


    if (flag=== true){
        alert("Please make a selection for each field");
        return false;
    }

    // <---------------------        Send the success Codes to controller??
      return true; 
};


    </script>
    <apex:form onsubmit="check()">
        <apex:pageblock mode="maindetail" title="{!$ObjectType.Customization__c.label} Detail">
         <apex:pageBlockButtons >
            <apex:commandButton action="{!mySave}" value="Generate" onclick="return check();" />   
         </apex:pageBlockButtons>


        </apex:pageblock>
    </apex:form>
</apex:page>

Apex:

    public pagereference mySave(){

        system.debug(test);
        PageReference p = new PageReference('/001');
        return p;
    }
1
  • When working in JavaScript you need to always keep an eye on your browser's JavaScript console and consider learning about your browser's other development tools. That should tell you when you click that the equals in "return=check();" is unexpected. That code should be "return check();". Watch out for other JavaScript syntax errors too.
    – Keith C
    Apr 7, 2014 at 17:18

1 Answer 1

1

This is the Ideal Case for using ActionFunction

Create an action function and call that via JavaScript

 <script>

function check(){
 var flag = false;
var successCodes = "start";
// do a bunch of error checking.


if (flag=== true){
    alert("Please make a selection for each field");
    return false;
}

// <---------------------        Send the success Codes to controller??
  callsave();//Call Action Function SFDC

};

</script>

<apex:form onsubmit="check()">
    <apex:pageblock mode="maindetail" title="{!$ObjectType.Customization__c.label} Detail">
     <apex:pageBlockButtons >
        <apex:commandButton action="{!mySave}" value="Generate" onclick="return=check();" />   
     </apex:pageBlockButtons>
 <!-- Define the JavaScript function sayHello-->
    <apex:actionFunction name="callsave" action="{!mySave}" />

    </apex:pageblock>
</apex:form>

http://www.infallibletechie.com/2012/10/calling-controller-method-using.html

3
  • can you provide some further details? I can't get the syntax to work Apr 7, 2014 at 17:36
  • So ideally you can call the actionfunction in Javascript .The name of the actionfunction has to be called in the javascript and the action function will call the action via Action attribute .Its that simple and basic of actionFunction Apr 7, 2014 at 17:50
  • Added a blog link that should explain you as well.. Apr 7, 2014 at 17:53

You must log in to answer this question.

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