Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\drupalup_simple_form\Form;
- use Drupal\Core\Form\FormBase;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Ajax\AjaxResponse;
- use Drupal\Core\Ajax\HtmlCommand;
- /**
- * Our simple form class.
- */
- class SimpleForm extends FormBase {
- /**
- * {@inheritdoc}
- */
- public function getFormId() {
- return 'drupalup_simple_form';
- }
- /**
- * {@inheritdoc}
- */
- public function buildForm(array $form, FormStateInterface $form_state) {
- $form['search'] = array(
- '#type' => 'textfield',
- '#title' => t('Search:'),
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => $this->t('Submit'),
- '#button_type' => 'primary',
- '#ajax' => array(
- 'callback' => [$this, 'submitForm'],
- 'event' => 'click',
- ),
- );
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- //public function submitForm(array &$form, FormStateInterface $form_state) {
- // drupal_set_message($this->t('@can_name ,Your application is being submitted!', array('@can_name' => $form_state->getValue('candidate_name'))));
- // foreach ($form_state->getValues($results) as $result) {
- // print_r($json_string= (string) $results->getBody());
- // }
- // }
- /* public function submitForm(array &$form, FormStateInterface $form_state) {
- * drupal_set_message(
- * $form_state->getValue($results)
- * );
- * }
- */
- public function submitForm(array &$form, FormStateInterface $form_state) {
- $url = 'http://systembevakningsagenten.se/api/json/1.0/searchProduct.json?query=';
- $term = $form_state->getValue('search');
- $query_url = $url. $term;
- $client = \Drupal::httpClient();
- $request = $client->get($query_url);
- $data = json_decode($request->getBody(), TRUE);
- $results = [];
- if (!empty($data) && !empty($data['items'])) {
- foreach ($data['items'] as $item ) {
- $results[] = $item['name'];
- }
- }
- }
- }
- ?>
- Message: <input type="text" id="message">
- <input type="submit" onclick='PostMessage()'>
- <script>
- function PostMessage() {
- var comment = document.getElementById('message').value;
- window.location.assign('http://systembevakningsagenten.se/api/json/1.0/searchProduct.json?query='+comment)
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement