Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » (solved) JavaScript and SilverStripe

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1414 Views
  • Beppo98
    Avatar
    Community Member
    19 Posts

    (solved) JavaScript and SilverStripe Link to this post

    Hello,

    I think I have a quite easy question but i don't know how I can implement this simple example in silverstripe.

    This is the HTML code:

    What is the question?
    <form name="Question1">
    <input name="rbQuestion1" type="radio">A) possible answer 1
    <input name="rbQuestion1" type="radio">B) possible answer 2
    <input name="rbQuestion1" type="radio">C) possible answer 3
    <input value="answer" onclick="checkQuestion" type="submit">
    </form>

    And I want to check in the function "checkQuestion" if the choosed answer is the right answer.

    function checkQuestion()
    {
       var QuestionChecked = false;

       for (var i=0; i<window.document.Question1.length; i++)
       {
          if (window.document.Question1.checked)
          {
             QuestionChecked = true;
          }
       }
       
       if (QuestionChecked )
       {
          if(window.document.Question1.elements[1].checked==true)
          {
             alert("right!");
          }
          else
          {
             alert("wrong!");
          }
       }
       else
       {
          alert("Please choose an existing answer!");
       }   
    }


    How I can realize this in silverstripe?

    Regards Beppo

  • dompie
    Avatar
    Community Member
    88 Posts

    Re: (solved) JavaScript and SilverStripe Link to this post

    1) You can create a form (see http://doc.silverstripe.org/recipes:forms or http://doc.silverstripe.org/tutorial:3-forms) and a form validation. Might feel like huge overhead if you're new to SS, but very helpful when forms get bigger.

    2) If the answer is not submitted to the server, it is sufficient to include your javascript in the init() function in your page controller. (see http://doc.silverstripe.org/requirements)

  • Beppo98
    Avatar
    Community Member
    19 Posts

    Re: (solved) JavaScript and SilverStripe Link to this post

    Hey dompie,

    thank you very much for your answer. I tried to solve my problem with the suggestion number 2. Up to now I included the following code in the init-method of my Page.php:

    Requirements::javascript("mysite/javascript/checkQuestion.js");

    But now I don't know how I can implement the HTML-Code, because if I try to include the posted code in the SilverStripe editor, the editor delets a few things like the submit button and so on. Or do I also need your posted number 1 to realize what I want?

    Best Regards, Beppo

  • Beppo98
    Avatar
    Community Member
    19 Posts

    Re: (solved) JavaScript and SilverStripe Link to this post

    Hello,

    can anybody help me with my problem? I know I am a beginner but I would be very thankful if anybody can give me a few tipps.

    Regards Beppo

  • dompie
    Avatar
    Community Member
    88 Posts

    Re: (solved) JavaScript and SilverStripe Link to this post

    You might want to read the Documentation first. After this some things should become clear.

  • Beppo98
    Avatar
    Community Member
    19 Posts

    Re: (solved) JavaScript and SilverStripe Link to this post

    I solved this problem with creating a new page type and in this page type I direct implemented the JavaScript code.

    Best regards, Beppo

    1414 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.