Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

(solved) JavaScript and SilverStripe


Reply

6 Posts   1423 Views

Avatar
Beppo98

23 December 2010 at 9:04am (Last edited: 23 December 2010 9:05am), Community Member, 19 Posts

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

Avatar
dompie

23 December 2010 at 9:45am Community Member, 88 Posts

1) You can create a form (see [url]http://doc.silverstripe.org/recipes:forms[/url] or [url]http://doc.silverstripe.org/tutorial:3-forms[/url]) 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 [url]http://doc.silverstripe.org/requirements[/url])

Avatar
Beppo98

25 December 2010 at 1:06am (Last edited: 25 December 2010 1:07am), Community Member, 19 Posts

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

Avatar
Beppo98

5 January 2011 at 10:51am Community Member, 19 Posts

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

Avatar
dompie

11 January 2011 at 5:26am Community Member, 88 Posts

You might want to read the [url=http://doc.silverstripe.org/tutorial:1-building-a-basic-site]Documentation[/url] first. After this some things should become clear.

Avatar
Beppo98

21 March 2011 at 12:24am Community Member, 19 Posts

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

Best regards, Beppo