Supporting each other

Community forums

Welcome, Guest
Username: Password: Remember me
This is the place for questions about learning design and pedagogy; how to use different page types for different purposes.
  • Page:
  • 1

TOPIC:

Variable with name 6 years 3 months ago #5262

  • annem
  • annem's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 177
  • Thank you received: 0
Hi Xerte Community,
I would like to set a variable called Name to a static value of Anne and then wherever you want Anne to appear use [Name]. Then change the value of Name to something else and the new name will appear everywhere [Name] is used.

Please can someone give me instructions on what I add into the 'Variable' fields to be able to achieve this?

Thanks in advance
Anne

Please Inloggen or Create an account to join the conversation.

Variable with name 6 years 2 months ago #5315

  • Fay
  • Fay's Avatar
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 229
  • Thank you received: 92
Hi Anne

There's no easy way to do this that doesn't require adding some code. Do you want your students to enter the new value for 'name'? If so them you can do something like this:

In the field where you want the input field to be:
<p>My name is 
  <input id="nameField" name="name" type="text" />
  <button id="submitBtn" onclick="submitName()">Submit
  </button>
</p>

And then add the script optional property to the page with this in it:
for (var i=0; i<x_variables.length; i++) {
  if (x_variables[i].name == 'name') {
    nameVar = i;
    break;
  }
}

function submitName() {
  if ($('#nameField').val() != '') {
    x_variables[nameVar].value = $('#nameField').val();
  }
}

This will use the original value you set up for 'name' in all pages viewed before they press submit & the new value entered in the text field in all pages after they submit. However, if they return to a previously viewed page that includes [name] it will use the value of it before they changed it as the insertion of variables is only done on the first view of a page.

I hope this helps. I'd like to build in a better way of allowing variables to be changed by end users but am unlikely to have time for this for a while.

Fay

Please Inloggen or Create an account to join the conversation.

Variable with name 6 years 2 months ago #5316

  • Fay
  • Fay's Avatar
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 229
  • Thank you received: 92
Oh, and I should have said, in that example in the variables field you would just need to create a variable with the name of 'name' and a fixed value of 'Anne' and leave all the other fields blank.

Please Inloggen or Create an account to join the conversation.

Variable with name 6 years 2 months ago #5329

  • annem
  • annem's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 177
  • Thank you received: 0
Hi Fay,

This is really helpful and useful, thank you so much.

I notice on the 'Field' dialog box (when I double click on the 'submit' box in edit mode on the page, a text field dialog box is shown), that there are other 'type' options such as Email, Password, Search, Telephone Number.

My question for all on the forum....
So if I want a variable for email, for example, to be set up on a Learning Object, how do I adjust the HTML code and script for this? And how would I add this as a variable? Is this possible?

Thanks in advance for help with this.
Anne
Attachments:

Please Inloggen or Create an account to join the conversation.

Variable with name 6 years 2 months ago #5343

  • Fay
  • Fay's Avatar
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 229
  • Thank you received: 92
I've not used the email type before but it seems to work so that if you were creating a proper html form with the submitted data being sent somewhere else it would check that what was entered here was in the correct format for an email address. This wouldn't really apply to this scenario as we're not properly 'submitting' data from a form. I would just leave any fields you want to be completed as the 'text' type.

I suspect what you might want though is for users to be able to complete more than one field on a page and have them saved to different variables. You'll need slightly different code for this:

1. Create as many variables as you need, each with a unique name and a default fixed value that can be used if the form isn't completed.

2. Create your fields in the html in this format, making sure that the names of the inputs match the variable names (e.g. a variable called 'email' would be entered in the input with the name 'email'):
<div id="myForm">
  <p>Name: 
    <input name="name" type="text" />
  </p>
  <p>Email: 
    <input name="email" type="text" />
  </p>
  <p>Course: 
    <input name="course" type="text" />
  </p>
  <button id="submitBtn" onclick="submitForm('myForm')">Submit
  </button>
</div>

3. Then paste these functions in the script optional property:
function lookupVar(id) {
  for (var i=0; i<x_variables.length; i++) {
    if (x_variables[i].name == id) {
      return i;
      break;
    }
  }
  return null;
}

function submitForm(form) {
  var $inputs = $('#' + form).find('input');
  $inputs.each(function() {
    var id = $(this).attr('name');
    if ($(this).val() != '') {
      	var varIndex = lookupVar(id);
      	if (varIndex != null) {
          x_variables[varIndex].value = $(this).val();
        }
    }
  })
}

I hope this helps
Fay

Please Inloggen or Create an account to join the conversation.

Variable with name 5 years 10 months ago #5583

  • simonfokt
  • simonfokt's Avatar
  • Offline
  • New Member
  • New Member
  • Posts: 8
  • Thank you received: 1
This is really useful!

Can I have a follow up? Would it be possible to set up the 'submit' button in such a way that on press it would send the 'name', 'email', and 'course' values to a predetermined email address, perhaps with a note: 'this student started the test'?

Alternatively, could it add those values to some table which I could later access to see the details of all students who started the test?

Thanks!

Please Inloggen or Create an account to join the conversation.

  • Page:
  • 1
Moderators: ingdon
Time to create page: 0.051 seconds
Copyright © 2024 The Xerte Project.
Xerte logo Apereo logo OSI Logo

Search