0

I have below Page and Class, on entering some text and clicking on Go navigates to fetchData page but the parameter(here the text entered by User) is coming as blank, why is it blank? What to do if we need to get whatever text entered by user as parameter to another vg page?

<apex:page controller="InputTextClass">
    <apex:form >
        <apex:outputLabel value="Enter Text: "/>
        <apex:inputText value="{!enteredText}"/> &nbsp;&nbsp;
        <apex:commandButton value="Go" onclick="window.open('/apex/fetchData?text={!enteredText}')"/>
    </apex:form>
</apex:page>

public class InputTextClass 
{
    public String enteredText{set;get;}
}

Thanks

2 Answers 2

0

It's because when enteredText is initialised, it's null and you're never actually setting it to the value entered in the <apex:inputText>.

Apex

You'd be better off handling all of this in the class using PageReference.

I'd suggest changing your <apex:commandButton> to something like this:

<apex:commandButton value="Go" action="{!redirectUser}" />

And in your class, do something like this:

public PageReference redirectUser() {
    PageReference redirectPage = Page.fetchData; // Init the page
    redirectPage.setRedirect(true); // Set redirect
    redirectPage.getParameters().put('text', enteredText); // Add the parameter
    return redirectPage; // Redirect
}

This will then allow Apex to set the value and use it in the redirectUser method.

JavaScript

If you must use JavaScript (e.g. you're trying to open a new window with the URL) which I'm presuming you might actually be after here, you'd have to use JavaScript to get the value.

I'd suggest using the <apex:commandButton> to call a JavaScript function like below:

<apex:commandButton value="Go" onclick="redirectUserJavaScript()"/>

You'd also need to change your <apex:inputText />, you could remove the reference to the Apex variable:

<apex:inputText id="enteredText" />

You could even use pure HTML without the VF helper class:

<input id="enteredText" type="text" />

Then have a JavaScript function that gets the value:

<script type="text/javascript">
    redirectUserJavaScript() {
        var myValue = document.getElementById("enteredText").value;

        window.open('/apex/fetchData?text=' + myValue,'_blank');
    }
</script>

3
  • Hi Dan, Thanks for the response, the redirectUser() refreshes same page, what if I need to open it in a new tab or window?
    – Jaya
    Nov 23, 2016 at 8:59
  • @Jaya Funnily enough I was just writing about that when you commented. See my edits.
    – Dan Jones
    Nov 23, 2016 at 9:06
  • @Jaya Great. If it helped/worked be sure to mark it as accepted to close the question down and it'll help others in the future!
    – Dan Jones
    Nov 23, 2016 at 12:45
0

It is caused by order of execution. When you click button the variable enteredText is not yet set in controller - it have null value. Also values from controller are pass to Page ONLY on load. So you have to either create method in controller to redirect or collect value of field via JS.

I would recommend something like this

apex:page controller="InputTextClass">
<apex:form >
    <apex:outputLabel value="Enter Text: "/>
    <apex:inputText value="{!enteredText}"/> &nbsp;&nbsp;
    <apex:commandButton value="Go" action="{!redirect}"/>
</apex:form>

public class InputTextClass {
    public String enteredText{set;get;}

public PageReference redirect(){

   if(String.isBlank(enteredText)){
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));
       return null;
   }

    return new PageReference('/apex/fetchData?text=' + enteredText);
}}
6
  • This is a bit of a messier way of doing things. You're trying to redirect in both JavaScript and Apex.
    – Dan Jones
    Nov 23, 2016 at 8:50
  • Sorry - I did not removed orginal code from VF part... Thanks you noticed! :) Nov 23, 2016 at 8:52
  • Hi Artur, Thanks for the response, the redirect() refreshes same page, what if I need to open it in a new tab or window?
    – Jaya
    Nov 23, 2016 at 9:06
  • You just need to define target parameter on button, example target="_blank" Nov 23, 2016 at 9:07
  • I believe target parameter is not supported for button.
    – Jaya
    Nov 23, 2016 at 9:12

You must log in to answer this question.

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