This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

exsoft.org40482
exsoft.org40482

Hi. Please example of code for sending form via Ajax.

oskar.villani40843
oskar.villani40843

Hi,

There is a good example given in the default installation.

  • See the page AJAX framework. Look at markup and code section.
  • See the partial "calcresult"

Anyhow, here is a more brief howto example based on the above
Step 1: place your form code in the markup area like this

<form data-request="onTest" data-request-update="calcresult: '#result'">
<input type="text" name="value1">
<input type="text" name="value2">
<button type="submit" data-attach-loading>Calculate</button>
</form>
<div id="result">
{% partial "calcresult" %}
</div>

  • The data-request="onTest" within the form tag is required for the php code. But you can use a name you prefer, but it has to start with "on..." (so "onHavingFun" would do it too)
  • the data-request-update="calcresult: '#result' " is required for place where the results have to be shown.
  • The div with id="result" is where the results are displayed
  • The name "calcresult" in the form tag refers to a partial called with: twig {% partial "calcresult" %} It is called for display (but you can choose any other name too)

Step 2: place your php code inside the code section. Notice: you have to write your code inside a function named as described in the form tag (eg "onTest" or "onHavingFun")

function onTest() {
$value1 = input('value1');
$value2 = input('value2');
$this['result'] = "Your input was: " . $value1 . " and " . $value2;
}

Step 3: finally create a partial named calcresult an place the following twig code into the markup section:
{% if result %}
{{ result }}
{% endif %}

The twig expression {{ result }} displays what is defined in the php code as variable $this['result']

Got it?

Cheers!
Oskar

P.S. sorry for the missing idents and bad formatting of the souce code sections. To my opinion the markup implemented here is a little bit complacated and the preview does not fit the final view.

Last updated

exsoft.org40482
exsoft.org40482

Very good! Big thx! Everything is clear!

oskar.villani40843
exsoft.org40482
exsoft.org40482

oskar.villani40843, may be you help me more? how to send POST inputs to Mail without Ajax?

https://pastebin.com/UvXqmzzk

Last updated

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.