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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

(solved) JavaScript and SilverStripe


Go to End


6 Posts   1990 Views

Avatar
Beppo98

Community Member, 19 Posts

23 December 2010 at 9:04am

Edited: 23/12/2010 9:05am

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

Community Member, 88 Posts

23 December 2010 at 9:45am

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)

Avatar
Beppo98

Community Member, 19 Posts

25 December 2010 at 1:06am

Edited: 25/12/2010 1:07am

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

Community Member, 19 Posts

5 January 2011 at 10:51am

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

Community Member, 88 Posts

11 January 2011 at 5:26am

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

Avatar
Beppo98

Community Member, 19 Posts

21 March 2011 at 12:24am

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

Best regards, Beppo