Рассмотрим пример когда надо сохранить данные (например, имя и фамилию) из HTML формы в базу данных клиентов (customer), для этого потребуется тип в базе данных, где будем хранить данные, серверный скрипт, который принимает данные объекта и сохраняет в БД.
А также клиентские скрипты на HTML c формой отправки и страницами ошибки/успехе добавления данных. Поехали.
1. Создаем новое приложение через Hive IDE (
http://ide.hivext.ru)
2. В Модель данных добавляем новый тип
customer. В тип добавляем два поля типа string:
first_name и
last_name.
3. В Скрипты добавляем Javascript(SSJS) скрипт и cохраняем с именем
customer/addjavascript:
hivext.local.SetHeader("Content-Type", "text/html");
var signature = hivext.local.GetDeviceSignature();
var user = {
name : hivext.local.GetParam("name"),
email : hivext.local.GetParam("email")
}
var response = hivext.data.base.CreateObject(appid, signature, "users", user);
if(response.result != 0) return hivext.local.Redirect("/failed.html");
return hivext.local.Redirect("/success.html");
4. В Ресурсы, в папку public_html добавляем необходимые файлы HTML. Добавить файлы можно через FTP или Hive IDE.
index.htmlhtml4strict:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<meta http-equiv="Cache-Control" content="no-cache, no-store, max-age=0, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="Fri, 01 Jan 1990 00:00:00 GMT" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="all" />
<title>HTML Form Example</title>
<body>
<b>Add new customer</b>
<form method="post" action="customer/add">
First name <input name="first_name" value="John" /><br />
Last name <input name="last_name" value="Rambo" /><br />
<input type="submit" value="Add" />
</form>
</body>
</html>
success.htmlhtml4strict:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<meta http-equiv="Cache-Control" content="no-cache, no-store, max-age=0, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="Fri, 01 Jan 1990 00:00:00 GMT" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="all" />
<title>HTML Form Example</title>
<body>
<h1>Customer saved success!</h1>
</body>
</html>
failed.htmlhtml4strict:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<meta http-equiv="Cache-Control" content="no-cache, no-store, max-age=0, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="Fri, 01 Jan 1990 00:00:00 GMT" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="all" />
<title>HTML Form Example</title>
<body>
<h1>Failed!</h1>
<a href="index.html">Try again</a>
</body>
</html>
5. Запускаем приложение и смотрим результат.